Skip to content

Commit 5db54f3

Browse files
authored
Merge pull request #12246 from cclauss/ruff-rules-C4-and-PERF
2 parents 7c8425b + af43e13 commit 5db54f3

21 files changed

+93
-102
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repos:
2323

2424
- repo: https://github.com/astral-sh/ruff-pre-commit
2525
# Ruff version.
26-
rev: v0.0.270
26+
rev: v0.0.286
2727
hooks:
2828
- id: ruff
2929

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ruff rules ASYNC,C4,C90,PERF,PLE,PLR for minor optimizations and to set upper limits on code complexity.

pyproject.toml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ webencodings = "https://github.com/SimonSapin/python-webencodings/raw/master/LIC
7474

7575
[tool.ruff]
7676
extend-exclude = [
77+
"_vendor",
7778
"./build",
7879
".scratch",
79-
"_vendor",
8080
"data",
8181
]
8282
ignore = [
@@ -88,21 +88,38 @@ ignore = [
8888
]
8989
line-length = 88
9090
select = [
91+
"ASYNC",
9192
"B",
93+
"C4",
94+
"C90",
9295
"E",
9396
"F",
94-
"W",
9597
"G",
96-
"ISC",
9798
"I",
99+
"ISC",
100+
"PERF",
101+
"PLE",
102+
"PLR0",
103+
"W",
98104
]
99105

100-
[tool.ruff.per-file-ignores]
101-
"noxfile.py" = ["G"]
102-
"tests/*" = ["B011"]
103-
104106
[tool.ruff.isort]
105107
# We need to explicitly make pip "first party" as it's imported by code in
106108
# the docs and tests directories.
107109
known-first-party = ["pip"]
108110
known-third-party = ["pip._vendor"]
111+
112+
[tool.ruff.mccabe]
113+
max-complexity = 33 # default is 10
114+
115+
[tool.ruff.per-file-ignores]
116+
"noxfile.py" = ["G"]
117+
"src/pip/_internal/*" = ["PERF203"]
118+
"tests/*" = ["B011"]
119+
"tests/unit/test_finder.py" = ["C414"]
120+
121+
[tool.ruff.pylint]
122+
max-args = 15 # default is 5
123+
max-branches = 28 # default is 12
124+
max-returns = 13 # default is 6
125+
max-statements = 134 # default is 50

src/pip/_internal/cache.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,10 @@ def _get_candidates(self, link: Link, canonical_package_name: str) -> List[Any]:
7878
if can_not_cache:
7979
return []
8080

81-
candidates = []
8281
path = self.get_path_for_link(link)
8382
if os.path.isdir(path):
84-
for candidate in os.listdir(path):
85-
candidates.append((candidate, path))
86-
return candidates
83+
return [(candidate, path) for candidate in os.listdir(path)]
84+
return []
8785

8886
def get_path_for_link(self, link: Link) -> str:
8987
"""Return a directory to store cached items in for link."""

src/pip/_internal/cli/autocompletion.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ def autocomplete() -> None:
7171

7272
for opt in subcommand.parser.option_list_all:
7373
if opt.help != optparse.SUPPRESS_HELP:
74-
for opt_str in opt._long_opts + opt._short_opts:
75-
options.append((opt_str, opt.nargs))
74+
options += [
75+
(opt_str, opt.nargs) for opt_str in opt._long_opts + opt._short_opts
76+
]
7677

7778
# filter out previously specified options from available options
7879
prev_opts = [x.split("=")[0] for x in cwords[1 : cword - 1]]

src/pip/_internal/commands/cache.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from optparse import Values
44
from typing import Any, List
55

6-
import pip._internal.utils.filesystem as filesystem
76
from pip._internal.cli.base_command import Command
87
from pip._internal.cli.status_codes import ERROR, SUCCESS
98
from pip._internal.exceptions import CommandError, PipError
9+
from pip._internal.utils import filesystem
1010
from pip._internal.utils.logging import getLogger
1111

1212
logger = getLogger(__name__)
@@ -151,14 +151,8 @@ def format_for_human(self, files: List[str]) -> None:
151151
logger.info("\n".join(sorted(results)))
152152

153153
def format_for_abspath(self, files: List[str]) -> None:
154-
if not files:
155-
return
156-
157-
results = []
158-
for filename in files:
159-
results.append(filename)
160-
161-
logger.info("\n".join(sorted(results)))
154+
if files:
155+
logger.info("\n".join(sorted(files)))
162156

163157
def remove_cache_items(self, options: Values, args: List[Any]) -> None:
164158
if len(args) > 1:

src/pip/_internal/commands/debug.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,9 @@ def show_tags(options: Values) -> None:
134134

135135

136136
def ca_bundle_info(config: Configuration) -> str:
137-
levels = set()
138-
for key, _ in config.items():
139-
levels.add(key.split(".")[0])
140-
137+
# Ruff misidentifies config as a dict.
138+
# Configuration does not have support the mapping interface.
139+
levels = {key.split(".", 1)[0] for key, _ in config.items()} # noqa: PERF102
141140
if not levels:
142141
return "Not specified"
143142

src/pip/_internal/commands/list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def output_package_listing_columns(
297297

298298
# Create and add a separator.
299299
if len(data) > 0:
300-
pkg_strings.insert(1, " ".join(map(lambda x: "-" * x, sizes)))
300+
pkg_strings.insert(1, " ".join("-" * x for x in sizes))
301301

302302
for val in pkg_strings:
303303
write_output(val)

src/pip/_internal/locations/_distutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def distutils_scheme(
8989
# finalize_options(); we only want to override here if the user
9090
# has explicitly requested it hence going back to the config
9191
if "install_lib" in d.get_option_dict("install"):
92-
scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
92+
scheme.update({"purelib": i.install_lib, "platlib": i.install_lib})
9393

9494
if running_under_virtualenv():
9595
if home:

src/pip/_internal/models/installation_report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _install_req_to_dict(cls, ireq: InstallRequirement) -> Dict[str, Any]:
3636
}
3737
if ireq.user_supplied and ireq.extras:
3838
# For top level requirements, the list of requested extras, if any.
39-
res["requested_extras"] = list(sorted(ireq.extras))
39+
res["requested_extras"] = sorted(ireq.extras)
4040
return res
4141

4242
def to_dict(self) -> Dict[str, Any]:

0 commit comments

Comments
 (0)