Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff rules for comprehensions and performance #168

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions micropip/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def install(
if isinstance(requirements, str):
requirements = [requirements]

fetch_kwargs = dict()
fetch_kwargs = {}

if credentials:
fetch_kwargs["credentials"] = credentials
Expand Down Expand Up @@ -84,10 +84,9 @@ async def install(
)

# Now install PyPI packages
for wheel in transaction.wheels:
# detect whether the wheel metadata is from PyPI or from custom location
# wheel metadata from PyPI has SHA256 checksum digest.
wheel_promises.append(wheel.install(wheel_base))
# detect whether the wheel metadata is from PyPI or from custom location
# wheel metadata from PyPI has SHA256 checksum digest.
wheel_promises.extend(wheel.install(wheel_base) for wheel in transaction.wheels)

await asyncio.gather(*wheel_promises)

Expand Down
3 changes: 1 addition & 2 deletions micropip/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def format_row(values, widths, filler=""):

rows.append(format_row(headers, col_width))
rows.append(format_row([""] * len(col_width), col_width, filler="-"))
for line in table:
rows.append(format_row(line, col_width))
rows.extend(format_row(line, col_width) for line in table)

return "\n".join(rows)

Expand Down
8 changes: 4 additions & 4 deletions micropip/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ async def gather_requirements(
self,
requirements: list[str] | list[Requirement],
) -> None:
requirement_promises = []
for requirement in requirements:
requirement_promises.append(self.add_requirement(requirement))
requirement_promises = [
self.add_requirement(requirement) for requirement in requirements
]

await asyncio.gather(*requirement_promises)

Expand Down Expand Up @@ -132,7 +132,7 @@ def eval_marker(e: dict[str, str]) -> bool:
# self.ctx_extras is empty and hence the eval_marker() function
# will not be called at all.
if not req.marker.evaluate(self.ctx) and not any(
[eval_marker(e) for e in self.ctx_extras]
eval_marker(e) for e in self.ctx_extras
):
return
# Is some version of this package is already installed?
Expand Down
3 changes: 2 additions & 1 deletion micropip/wheelinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class WheelInfo:
_metadata: Metadata | None = None # Wheel metadata.
_requires: list[Requirement] | None = None # List of requirements.

# Path to the .dist-info directory. This is only available after extracting the wheel, i.e. after calling `extract()`.
# Path to the .dist-info directory.
# This is only available after extracting the wheel, i.e. after calling `extract()`.
_dist_info: Path | None = None

def __post_init__(self):
Expand Down
18 changes: 12 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,23 @@ build-backend = "setuptools.build_meta"
write_to = "micropip/_version.py"

[tool.ruff]
line-length = 122
exclude = ["micropip/externals"]
line-length = 120
lint.select = [
"E", # pycodestyles
"W", # pycodestyles
"F", # pyflakes
"B", # bugbear
"UP", # pyupgrade
"C4", # flake8-comprehensions
"C90", # mccabe code complexity
"E", # pycodestyle errors
"F", # pyflakes
"G", # flake8-logging-format
"I", # isort
"PERF", # perflint
"PGH", # pygrep-hooks
"G", # flake8-logging-format
"UP", # pyupgrade
"W", # pycodestyle whitespace
]
lint.flake8-comprehensions.allow-dict-calls-with-keyword-arguments = true
lint.mccabe.max-complexity = 13
target-version = "py312"

[tool.ruff.lint.isort]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ async def test_install_with_credentials(selenium):
fetch_response_mock = MagicMock()

async def myfunc():
return json.dumps(dict())
return json.dumps({})

fetch_response_mock.string.side_effect = myfunc

Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_Metadata_requires(metadata_path, extras, expected):
m = Metadata(metadata)

reqs = m.requires(extras)
reqs_set = set([r.name for r in reqs])
reqs_set = {r.name for r in reqs}
assert reqs_set == set(expected)


Expand Down
Loading