From 48bdf1f4954d69e4dd9db6648ce04df4e7425498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Fri, 13 Nov 2026 10:18:15 +0100 Subject: [PATCH] Ruff upgrade and fixes --- .pre-commit-config.yaml | 4 +- src/oca_github_bot/commands.py | 2 +- src/oca_github_bot/cron.py | 2 +- src/oca_github_bot/manifest.py | 4 +- src/oca_github_bot/process.py | 2 + .../tasks/label_modified_addons.py | 4 +- .../tasks/mention_maintainer.py | 6 +- src/oca_github_bot/tasks/merge_bot.py | 2 +- .../tasks/tag_ready_to_merge.py | 7 +-- src/oca_github_bot/utils.py | 2 +- src/oca_github_bot/version_branch.py | 4 +- .../on_pr_open_label_new_contributor.py | 1 - tests/common.py | 4 +- tests/test_manifest.py | 2 +- tests/test_mention_maintainer.py | 4 +- tests/test_migration_issue_bot.py | 56 ++++++++++++------- tests/test_utils.py | 8 +-- 17 files changed, 62 insertions(+), 52 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8db51f4e..1744591c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,8 +26,8 @@ repos: - id: mixed-line-ending args: ["--fix=lf"] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.8 + rev: v0.17.5 hooks: - id: ruff - args: [--fix, --exit-non-zero-on-fix] + args: [--fix, --exit-non-zero-on-fix, --unsafe-fixes] - id: ruff-format diff --git a/src/oca_github_bot/commands.py b/src/oca_github_bot/commands.py index b648754e..0edfb150 100644 --- a/src/oca_github_bot/commands.py +++ b/src/oca_github_bot/commands.py @@ -76,7 +76,7 @@ def delay(self, org, repo, pr, username, dry_run=False): class BotCommandMerge(BotCommand): bumpversion_mode = None - bumpversion_mode_list = ["major", "minor", "patch", "nobump"] + bumpversion_mode_list = ("major", "minor", "patch", "nobump") def parse_options(self, options): if not options: diff --git a/src/oca_github_bot/cron.py b/src/oca_github_bot/cron.py index 708722b3..0dead2c7 100644 --- a/src/oca_github_bot/cron.py +++ b/src/oca_github_bot/cron.py @@ -20,7 +20,7 @@ "task": "oca_github_bot.tasks.main_branch_bot." "main_branch_bot_all_repos", "args": (org,), - "kwargs": dict(build_wheels=True), + "kwargs": {"build_wheels": True}, "schedule": crontab(hour="2", minute="30"), }, "tag_ready_to_merge": { diff --git a/src/oca_github_bot/manifest.py b/src/oca_github_bot/manifest.py index 3621e9ac..e81162a5 100644 --- a/src/oca_github_bot/manifest.py +++ b/src/oca_github_bot/manifest.py @@ -279,14 +279,14 @@ def _get_manifest_from_api(gh_repo, addon, branch, manifest_file): path = f"{addon}/{manifest_file}" try: file_contents = gh_repo.file_contents(path, ref=branch) - except Exception as e: + except Exception as e: # noqa: BLE001 _logger.debug("Could not read %s@%s via GitHub API: %s", path, branch, e) return None if file_contents is None: return None try: return parse_manifest(file_contents.decoded) - except Exception as e: + except Exception as e: # noqa: BLE001 _logger.warning( "Failed to parse manifest %s@%s from GitHub API: %s", path, branch, e ) diff --git a/src/oca_github_bot/process.py b/src/oca_github_bot/process.py index 1d3e9cd4..0a61d0e7 100644 --- a/src/oca_github_bot/process.py +++ b/src/oca_github_bot/process.py @@ -17,6 +17,7 @@ def check_call(cmd, cwd, log_error=True, extra_cmd_args=False, env=None): cmd += extra_cmd_args cp = subprocess.run( cmd, + check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, @@ -34,6 +35,7 @@ def check_call(cmd, cwd, log_error=True, extra_cmd_args=False, env=None): def check_output(cmd, cwd, log_error=True): cp = subprocess.run( cmd, + check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, diff --git a/src/oca_github_bot/tasks/label_modified_addons.py b/src/oca_github_bot/tasks/label_modified_addons.py index 318e70ab..9fcdbdd6 100644 --- a/src/oca_github_bot/tasks/label_modified_addons.py +++ b/src/oca_github_bot/tasks/label_modified_addons.py @@ -46,9 +46,7 @@ def _label_modified_addons(gh, org, repo, pr, dry_run): if is_main_branch_bot_branch(target_branch): new_labels.add(f"series:{target_branch}") new_labels |= { - x - for x in issue_label_names - if not (x.startswith("mod:") or x.startswith("series:")) + x for x in issue_label_names if not (x.startswith(("mod:", "series:"))) } if not dry_run and new_labels != set(issue_label_names): diff --git a/src/oca_github_bot/tasks/mention_maintainer.py b/src/oca_github_bot/tasks/mention_maintainer.py index 4657ed88..3264e71b 100644 --- a/src/oca_github_bot/tasks/mention_maintainer.py +++ b/src/oca_github_bot/tasks/mention_maintainer.py @@ -43,7 +43,7 @@ def mention_maintainer(org, repo, pr, dry_run=False): modified_addons_maintainers = set() for modified_addon in modified_addon_dirs: - addon_maintainers = maintainers_dict.get(modified_addon, list()) + addon_maintainers = maintainers_dict.get(modified_addon, []) modified_addons_maintainers.update(addon_maintainers) pr_opener = gh_pr.user.login @@ -67,7 +67,7 @@ def mention_maintainer(org, repo, pr, dry_run=False): def get_mention(maintainers): """Get a comment mentioning all the `maintainers`.""" - maintainers_mentions = list(map(lambda m: "@" + m, maintainers)) + maintainers_mentions = ["@" + m for m in maintainers] mentions_comment = "" if maintainers_mentions: mentions_comment = ( @@ -90,7 +90,7 @@ def get_maintainers(addon_dirs): :return: Dictionary {'addon_dir': } """ - addon_maintainers_dict = dict() + addon_maintainers_dict = {} for addon_dir in addon_dirs: maintainers = get_manifest(addon_dir).get("maintainers", []) addon_maintainers_dict.setdefault(addon_dir, maintainers) diff --git a/src/oca_github_bot/tasks/merge_bot.py b/src/oca_github_bot/tasks/merge_bot.py index f80096cb..4a7b832c 100644 --- a/src/oca_github_bot/tasks/merge_bot.py +++ b/src/oca_github_bot/tasks/merge_bot.py @@ -405,7 +405,7 @@ def _get_commit_success(org, repo, pr, gh_commit): @task() @switchable("merge_bot") def merge_bot_status(org, repo, merge_bot_branch, sha): - with contextlib.suppress(github.BranchNotFoundError): + with contextlib.suppress(github.BranchNotFoundError): # noqa: SIM117 with github.temporary_clone(org, repo, merge_bot_branch) as clone_dir: head_sha = github.git_get_head_sha(cwd=clone_dir) if head_sha != sha: diff --git a/src/oca_github_bot/tasks/tag_ready_to_merge.py b/src/oca_github_bot/tasks/tag_ready_to_merge.py index 25e199c0..64868275 100644 --- a/src/oca_github_bot/tasks/tag_ready_to_merge.py +++ b/src/oca_github_bot/tasks/tag_ready_to_merge.py @@ -1,7 +1,7 @@ # Copyright (c) ACSONE SA/NV 2018 # Distributed under the MIT License (http://opensource.org/licenses/MIT). -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from .. import github from ..config import MIN_PR_AGE, switchable @@ -26,7 +26,7 @@ def tag_ready_to_merge(org, repo=None, dry_run=False): """Add the ``ready to merge`` tag to all PRs where conditions are met.""" with github.login() as gh: - max_created = datetime.utcnow() - timedelta(days=MIN_PR_AGE) + max_created = datetime.now(tz=UTC) - timedelta(days=MIN_PR_AGE) query = [ "type:pr", "state:open", @@ -42,8 +42,7 @@ def tag_ready_to_merge(org, repo=None, dry_run=False): for issue in gh.search_issues(" ".join(query)): if dry_run: _logger.info( - f"DRY-RUN add {LABEL_READY_TO_MERGE} " - f"label to PR {issue.html_url}" + f"DRY-RUN add {LABEL_READY_TO_MERGE} label to PR {issue.html_url}" ) else: _logger.info(f"add {LABEL_READY_TO_MERGE} label to PR {issue.html_url}") diff --git a/src/oca_github_bot/utils.py b/src/oca_github_bot/utils.py index deab086d..cad843ec 100644 --- a/src/oca_github_bot/utils.py +++ b/src/oca_github_bot/utils.py @@ -57,7 +57,7 @@ def compute_module_label_name(module_name: str) -> str: if len(label_name) > _MAX_LABEL_SIZE: module_hash = hashlib.sha256(bytes(module_name, "utf-8")).hexdigest() label_name = ( - f"{label_name[:(_MAX_LABEL_SIZE - (_HASH_SIZE + 1))]}" + f"{label_name[: (_MAX_LABEL_SIZE - (_HASH_SIZE + 1))]}" f" {module_hash[:_HASH_SIZE]}" ) return label_name diff --git a/src/oca_github_bot/version_branch.py b/src/oca_github_bot/version_branch.py index be1da6de..ad36c46f 100644 --- a/src/oca_github_bot/version_branch.py +++ b/src/oca_github_bot/version_branch.py @@ -21,9 +21,7 @@ def is_supported_main_branch(branch_name, min_version=None): if not ODOO_VERSION_RE.match(branch_name): return False branch_version = version.parse(branch_name) - if min_version and branch_version < version.parse(min_version): - return False - return True + return not (min_version and branch_version < version.parse(min_version)) def is_main_branch_bot_branch(branch_name): diff --git a/src/oca_github_bot/webhooks/on_pr_open_label_new_contributor.py b/src/oca_github_bot/webhooks/on_pr_open_label_new_contributor.py index b98c929f..f7fe8dce 100644 --- a/src/oca_github_bot/webhooks/on_pr_open_label_new_contributor.py +++ b/src/oca_github_bot/webhooks/on_pr_open_label_new_contributor.py @@ -15,4 +15,3 @@ async def on_pr_open_label_new_contributor(event, gh, *args, **kwargs): if the author has less than 4 commits in OCA repositories. """ # TODO - pass diff --git a/tests/common.py b/tests/common.py index 2fe04ab7..acc27956 100644 --- a/tests/common.py +++ b/tests/common.py @@ -34,9 +34,9 @@ def commit_addon(git_clone, addon_name): @contextmanager def set_config(**kwargs): saved = {} - for key in kwargs: + for key, value in kwargs.items(): saved[key] = getattr(config, key) - setattr(config, key, kwargs[key]) + setattr(config, key, value) try: yield finally: diff --git a/tests/test_manifest.py b/tests/test_manifest.py index e1a40902..dea9280d 100644 --- a/tests/test_manifest.py +++ b/tests/test_manifest.py @@ -180,7 +180,7 @@ def test_git_modified_addons_merge_base(git_clone): subprocess.check_call(["git", "commit", "-m", "[BOT] add addon2"], cwd=git_clone) assert git_modified_addons(git_clone, "origin/master") == ({"addon2"}, False) # create addon1 on a new branch - subprocess.check_call(["git", "checkout", "-b" "addon1"], cwd=git_clone) + subprocess.check_call(["git", "checkout", "-b", "addon1"], cwd=git_clone) addon1_dir = git_clone / "addon1" addon1_dir.mkdir() (addon1_dir / "__manifest__.py").write_text("{'name': 'addon1'}") diff --git a/tests/test_mention_maintainer.py b/tests/test_mention_maintainer.py index 24441a26..5de7edc7 100644 --- a/tests/test_mention_maintainer.py +++ b/tests/test_mention_maintainer.py @@ -69,7 +69,7 @@ def test_multi_maintainer_one_mention(git_clone, mocker): github_mock = mocker.patch("oca_github_bot.tasks.mention_maintainer.github") github_mock.temporary_clone.return_value.__enter__.return_value = str(git_clone) - addon_dirs = list() + addon_dirs = [] addon_names = ["addon1", "addon2"] themaintainer = "themaintainer" for addon_name in addon_names: @@ -96,7 +96,7 @@ def test_pr_by_maintainer_no_mention(git_clone, mocker, pr_opener): pr_mock = github_mock.login.return_value.__enter__.return_value.pull_request pr_mock.return_value.user.login = pr_opener - addon_dirs = list() + addon_dirs = [] addon_names = ["addon1", "addon2"] for addon_name in addon_names: addon_dir = make_addon(git_clone, addon_name, maintainers=["themaintainer"]) diff --git a/tests/test_migration_issue_bot.py b/tests/test_migration_issue_bot.py index 5fb8f439..1bbb920e 100644 --- a/tests/test_migration_issue_bot.py +++ b/tests/test_migration_issue_bot.py @@ -38,34 +38,48 @@ def test_set_lines_issue(gh): body_transformation = [ ( - "Issue with list but not the module\n" - "- [ ] a_module_1 - By @legalsylvain - #1\n" - "- [ ] z_module_1 - By @pedrobaeza - #2", - f"Issue with list but not the module\n" - f"- [ ] a_module_1 - By @legalsylvain - #1\n" - f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" - f"- [ ] z_module_1 - By @pedrobaeza - #2", + ( + "Issue with list but not the module\n" + "- [ ] a_module_1 - By @legalsylvain - #1\n" + "- [ ] z_module_1 - By @pedrobaeza - #2" + ), + ( + f"Issue with list but not the module\n" + f"- [ ] a_module_1 - By @legalsylvain - #1\n" + f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ), ), ( - f"Issue with list containing the module\n" - f"- [x] {module} - By @legalsylvain - #1\n" - f"- [ ] z_module_1 - By @pedrobaeza - #2", - f"Issue with list containing the module\n" - f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" - f"- [ ] z_module_1 - By @pedrobaeza - #2", + ( + f"Issue with list containing the module\n" + f"- [x] {module} - By @legalsylvain - #1\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ), + ( + f"Issue with list containing the module\n" + f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ), ), ( - f"Issue with list containing the module with no PR\n" - f"- [x] {module}\n" - f"- [ ] z_module_1 - By @pedrobaeza - #2", - f"Issue with list containing the module with no PR\n" - f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" - f"- [ ] z_module_1 - By @pedrobaeza - #2", + ( + f"Issue with list containing the module with no PR\n" + f"- [x] {module}\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ), + ( + f"Issue with list containing the module with no PR\n" + f"- [x] {module} - By @{gh_pr_user_login} - #{gh_pr_number}\n" + f"- [ ] z_module_1 - By @pedrobaeza - #2" + ), ), ( "Issue with no list", - f"Issue with no list\n" - f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}", + ( + f"Issue with no list\n" + f"- [ ] {module} - By @{gh_pr_user_login} - #{gh_pr_number}" + ), ), ] for old_body, new_body_expected in body_transformation: diff --git a/tests/test_utils.py b/tests/test_utils.py index 6b28b236..73b81d8f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -33,7 +33,7 @@ def test_retry_on_exception_raise_match(): def func_that_raises(): nonlocal counter counter += 1 - raise Exception("something") + raise Exception("something") # noqa: TRY002 max_retries = 2 sleep_time = 0.1 @@ -45,7 +45,7 @@ def func_that_raises(): max_retries=max_retries, sleep_time=sleep_time, ) - except Exception as e: + except Exception as e: # noqa: BLE001 assert "something" in str(e) assert counter == max_retries + 1 @@ -56,7 +56,7 @@ def test_retry_on_exception_raise_no_match(): def func_that_raises(): nonlocal counter counter += 1 - raise Exception("somestuff") + raise Exception("somestuff") # noqa: TRY002 max_retries = 2 sleep_time = 0.1 @@ -68,7 +68,7 @@ def func_that_raises(): max_retries=max_retries, sleep_time=sleep_time, ) - except Exception as e: + except Exception as e: # noqa: BLE001 assert "somestuff" in str(e) assert counter == 1