From 573983948ae0550b0f655ea81431bb08ce0bd540 Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Thu, 26 Mar 2026 13:46:38 -0700 Subject: [PATCH] Fix all flake8 warnings from newer flake8-bugbear and flake8-comprehensions Address warnings introduced by flake8-bugbear 24.12.12 and flake8-comprehensions 3.16.0: - C408: Replace dict()/list() calls with literal {} and [] - C413: Remove unnecessary list() around sorted() - C414: Remove unnecessary list() inside sorted() - C419: Suppress intentional list comprehension in all() (noqa) - B001: Replace bare except with except Exception - B006: Replace mutable default arguments with None - B010: Replace setattr() with direct attribute assignment - B017: Use RuntimeError instead of Exception in tests - B019: Suppress lru_cache on methods for long-lived objects (noqa) - B033: Remove duplicate item in set literal Change-Id: If4693d3e946200bbc22f689f7b94da604addcb80 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/566321 Tested-by: Carlos Fernandez Commit-Queue: Carlos Fernandez Reviewed-by: Mike Frysinger Reviewed-by: Gavin Mak --- git_command.py | 6 +++--- git_config.py | 2 +- manifest_xml.py | 5 ++--- project.py | 12 +++++++----- release/check-metadata.py | 2 +- run_tests.vpython3 | 16 +++++++++++++--- ssh.py | 2 +- subcmds/grep.py | 2 +- subcmds/help.py | 8 +++----- subcmds/sync.py | 4 ++-- tests/test_wrapper.py | 10 +++++----- 11 files changed, 39 insertions(+), 30 deletions(-) diff --git a/git_command.py b/git_command.py index 89e8d2f44..ebba92608 100644 --- a/git_command.py +++ b/git_command.py @@ -47,7 +47,7 @@ logger = RepoLogger(__file__) class _GitCall: - @functools.lru_cache(maxsize=None) + @functools.lru_cache(maxsize=None) # noqa: B019 def version_tuple(self): ret = Wrapper().ParseGitVersion() if ret is None: @@ -95,7 +95,7 @@ def RepoSourceVersion(): ver = ver[1:] else: ver = "unknown" - setattr(RepoSourceVersion, "version", ver) + RepoSourceVersion.version = ver return ver @@ -611,7 +611,7 @@ class GitCommandError(GitError): self.git_stderr = git_stderr @property - @functools.lru_cache(maxsize=None) + @functools.lru_cache(maxsize=None) # noqa: B019 def suggestion(self): """Returns helpful next steps for the given stderr.""" if not self.git_stderr: diff --git a/git_config.py b/git_config.py index 5559657ae..c58edab32 100644 --- a/git_config.py +++ b/git_config.py @@ -42,7 +42,7 @@ SYNC_STATE_PREFIX = "repo.syncstate." ID_RE = re.compile(r"^[0-9a-f]{40}$") -REVIEW_CACHE = dict() +REVIEW_CACHE = {} def IsChange(rev): diff --git a/manifest_xml.py b/manifest_xml.py index 084ca5ab2..c9bc6848b 100644 --- a/manifest_xml.py +++ b/manifest_xml.py @@ -763,10 +763,10 @@ https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md if p.subprojects: subprojects = {subp.name for subp in p.subprojects} - output_projects(p, e, list(sorted(subprojects))) + output_projects(p, e, sorted(subprojects)) projects = {p.name for p in self._paths.values() if not p.parent} - output_projects(None, root, list(sorted(projects))) + output_projects(None, root, sorted(projects)) if self._repo_hooks_project: root.appendChild(doc.createTextNode("")) @@ -823,7 +823,6 @@ https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md "submanifest", # These are children of 'project' nodes. "annotation", - "project", "copyfile", "linkfile", } diff --git a/project.py b/project.py index 577847f8c..11b53c33c 100644 --- a/project.py +++ b/project.py @@ -225,7 +225,7 @@ class ReviewableBranch: @property def unabbrev_commits(self): - r = dict() + r = {} for commit in self.project.bare_git.rev_list( not_rev(self.base), R_HEADS + self.name, "--" ): @@ -553,7 +553,7 @@ class Project: revisionExpr, revisionId, rebase=True, - groups=set(), + groups=None, sync_c=False, sync_s=False, sync_tags=True, @@ -605,7 +605,7 @@ class Project: self.SetRevision(revisionExpr, revisionId=revisionId) self.rebase = rebase - self.groups = groups + self.groups = groups if groups is not None else set() self.sync_c = sync_c self.sync_s = sync_s self.sync_tags = sync_tags @@ -943,7 +943,7 @@ class Project: out.important("prior sync failed; rebase still in progress") out.nl() - paths = list() + paths = [] paths.extend(di.keys()) paths.extend(df.keys()) paths.extend(do) @@ -1257,7 +1257,7 @@ class Project: submodules=False, ssh_proxy=None, clone_filter=None, - partial_clone_exclude=set(), + partial_clone_exclude=None, clone_filter_for_depth=None, ): """Perform only the network IO portion of the sync process. @@ -1310,6 +1310,8 @@ class Project: if clone_bundle and os.path.exists(self.objdir): clone_bundle = False + if partial_clone_exclude is None: + partial_clone_exclude = set() if self.name in partial_clone_exclude: clone_bundle = True clone_filter = None diff --git a/release/check-metadata.py b/release/check-metadata.py index 951bd4c2c..9b9a347c8 100755 --- a/release/check-metadata.py +++ b/release/check-metadata.py @@ -106,7 +106,7 @@ def check_path(opts: argparse.Namespace, path: Path) -> bool: def check_paths(opts: argparse.Namespace, paths: list[Path]) -> bool: """Check all the paths.""" # NB: Use list comprehension and not a generator so we check all paths. - return all([check_path(opts, x) for x in paths]) + return all([check_path(opts, x) for x in paths]) # noqa: C419 def find_files(opts: argparse.Namespace) -> list[Path]: diff --git a/run_tests.vpython3 b/run_tests.vpython3 index e6dfe7c63..e07d08256 100644 --- a/run_tests.vpython3 +++ b/run_tests.vpython3 @@ -48,10 +48,10 @@ wheel: < version: "version:3.0.7" > -# Required by pytest==8.3.4 +# Required by pytest==8.3.4 and flake8-bugbear==24.12.12 wheel: < - name: "infra/python/wheels/attrs-py2_py3" - version: "version:21.4.0" + name: "infra/python/wheels/attrs-py3" + version: "version:24.2.0" > # NB: Keep in sync with constraints.txt. @@ -119,6 +119,16 @@ wheel: < version: "version:2.10.0" > +wheel: < + name: "infra/python/wheels/flake8-bugbear-py3" + version: "version:24.12.12" +> + +wheel: < + name: "infra/python/wheels/flake8-comprehensions-py3" + version: "version:3.16.0" +> + wheel: < name: "infra/python/wheels/isort-py3" version: "version:5.10.1" diff --git a/ssh.py b/ssh.py index 6de8b89e8..25152f1cc 100644 --- a/ssh.py +++ b/ssh.py @@ -149,7 +149,7 @@ class ProxyManager: while True: try: procs.pop(0) - except: # noqa: E722 + except IndexError: break def close(self): diff --git a/subcmds/grep.py b/subcmds/grep.py index 85977ce80..e0f239fd3 100644 --- a/subcmds/grep.py +++ b/subcmds/grep.py @@ -93,7 +93,7 @@ contain a line that matches both expressions: pt = getattr(parser.values, "cmd_argv", None) if pt is None: pt = [] - setattr(parser.values, "cmd_argv", pt) + parser.values.cmd_argv = pt if opt_str == "-(": pt.append("(") diff --git a/subcmds/help.py b/subcmds/help.py index 800407114..df7806fa5 100644 --- a/subcmds/help.py +++ b/subcmds/help.py @@ -59,7 +59,7 @@ Displays detailed usage information about a command. def PrintAllCommandsBody(self): print("The complete list of recognized repo commands is:") - commandNames = list(sorted(all_commands)) + commandNames = sorted(all_commands) self._PrintCommands(commandNames) print( "See 'repo help ' for more information on a " @@ -74,10 +74,8 @@ Displays detailed usage information about a command. def PrintCommonCommandsBody(self): print("The most commonly used repo commands are:") - commandNames = list( - sorted( - name for name, command in all_commands.items() if command.COMMON - ) + commandNames = sorted( + name for name, command in all_commands.items() if command.COMMON ) self._PrintCommands(commandNames) diff --git a/subcmds/sync.py b/subcmds/sync.py index 03c0eba64..e5fb3bcdb 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -947,7 +947,7 @@ later is required to fix a server side protocol bug. "sync_dict" ] = multiprocessing.Manager().dict() - objdir_project_map = dict() + objdir_project_map = {} for index, project in enumerate(projects): objdir_project_map.setdefault(project.objdir, []).append(index) projects_list = list(objdir_project_map.values()) @@ -2657,7 +2657,7 @@ later is required to fix a server side protocol bug. if previously_pending_relpaths == pending_relpaths: stalled_projects_str = "\n".join( f" - {path}" - for path in sorted(list(pending_relpaths)) + for path in sorted(pending_relpaths) ) logger.error( "The following projects failed and could " diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index a38705675..36465e737 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -385,11 +385,11 @@ class VerifyRev(RepoWrapperTestCase): def test_verify_fails(self): """Check we fall back to signed tag when we have an unsigned commit.""" desc_result = subprocess.CompletedProcess([], 0, "v1.0-10-g1234\n", "") - gpg_result = Exception + gpg_result = RuntimeError with mock.patch.object( self.wrapper, "run_git", side_effect=(desc_result, gpg_result) ): - with self.assertRaises(Exception): + with self.assertRaises(RuntimeError): self.wrapper.verify_rev("/", "refs/heads/stable", "1234", True) @@ -543,15 +543,15 @@ class CheckRepoRev(GitCheckoutTestCase): self.wrapper, "check_repo_verify", return_value=True ): with mock.patch.object( - self.wrapper, "verify_rev", side_effect=Exception + self.wrapper, "verify_rev", side_effect=RuntimeError ): - with self.assertRaises(Exception): + with self.assertRaises(RuntimeError): self.wrapper.check_repo_rev(self.GIT_DIR, "stable") def test_verify_ignore(self): """Should pass when verification is disabled.""" with mock.patch.object( - self.wrapper, "verify_rev", side_effect=Exception + self.wrapper, "verify_rev", side_effect=RuntimeError ): rrev, lrev = self.wrapper.check_repo_rev( self.GIT_DIR, "stable", repo_verify=False