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 <carlosfsanz@meta.com>
Commit-Queue: Carlos Fernandez <carlosfsanz@meta.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Carlos Fernandez
2026-03-26 13:46:38 -07:00
committed by LUCI
parent 3f3c681a02
commit 573983948a
11 changed files with 39 additions and 30 deletions
+3 -3
View File
@@ -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:
+1 -1
View File
@@ -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):
+2 -3
View File
@@ -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",
}
+7 -5
View File
@@ -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
+1 -1
View File
@@ -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]:
+13 -3
View File
@@ -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"
+1 -1
View File
@@ -149,7 +149,7 @@ class ProxyManager:
while True:
try:
procs.pop(0)
except: # noqa: E722
except IndexError:
break
def close(self):
+1 -1
View File
@@ -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("(")
+3 -5
View File
@@ -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 <command>' 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)
+2 -2
View File
@@ -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 "
+5 -5
View File
@@ -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