Skip to content

Commit

Permalink
Ruff rules for comprehensions and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Dec 16, 2024
1 parent 10b9b69 commit 77abfb8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 37 deletions.
33 changes: 16 additions & 17 deletions micropip/externals/mousebender/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import html.parser
import urllib.parse
import warnings
from typing import Any, Dict, List, Optional, Union, Literal, TypeAlias, TypedDict
from typing import Any, Dict, Literal, TypeAlias, TypedDict # noqa: UP035 mypy needs `Dict`

import packaging.utils


ACCEPT_JSON_V1 = "application/vnd.pypi.simple.v1+json"


Expand Down Expand Up @@ -38,17 +37,17 @@ class UnsupportedMIMEType(Exception):
_Meta_1_1 = TypedDict("_Meta_1_1", {"api-version": Literal["1.1"]})


_HashesDict: TypeAlias = Dict[str, str]
_HashesDict: TypeAlias = Dict[str, str] # noqa: UP006,UP040 mypy is not yet ready to for `type` and `dict`.

_OptionalProjectFileDetails_1_0 = TypedDict(
"_OptionalProjectFileDetails_1_0",
{
"requires-python": str,
"dist-info-metadata": Union[bool, _HashesDict],
"dist-info-metadata": bool | _HashesDict,
"gpg-sig": bool,
"yanked": Union[bool, str],
"yanked": bool | str,
# PEP-714
"core-metadata": Union[bool, _HashesDict],
"core-metadata": bool | _HashesDict,
},
total=False,
)
Expand All @@ -66,13 +65,13 @@ class ProjectFileDetails_1_0(_OptionalProjectFileDetails_1_0):
"_OptionalProjectFileDetails_1_1",
{
"requires-python": str,
"dist-info-metadata": Union[bool, _HashesDict],
"dist-info-metadata": bool | _HashesDict,
"gpg-sig": bool,
"yanked": Union[bool, str],
"yanked": bool | str,
# PEP 700
"upload-time": str,
# PEP 714
"core-metadata": Union[bool, _HashesDict],
"core-metadata": bool | _HashesDict,
},
total=False,
)
Expand Down Expand Up @@ -103,13 +102,13 @@ class ProjectDetails_1_1(TypedDict):
name: packaging.utils.NormalizedName
files: list[ProjectFileDetails_1_1]
# PEP 700
versions: List[str]
versions: list[str]


ProjectDetails: TypeAlias = Union[ProjectDetails_1_0, ProjectDetails_1_1]
ProjectDetails: TypeAlias = ProjectDetails_1_0 | ProjectDetails_1_1 # noqa: UP040 mypy is not yet ready to for `type`.


def _check_version(tag: str, attrs: Dict[str, Optional[str]]) -> None:
def _check_version(tag: str, attrs: dict[str, str | None]) -> None:
if (
tag == "meta"
and attrs.get("name") == "pypi:repository-version"
Expand All @@ -126,11 +125,11 @@ def _check_version(tag: str, attrs: Dict[str, Optional[str]]) -> None:

class _ArchiveLinkHTMLParser(html.parser.HTMLParser):
def __init__(self) -> None:
self.archive_links: List[Dict[str, Any]] = []
self.archive_links: list[dict[str, Any]] = []
super().__init__()

def handle_starttag(
self, tag: str, attrs_list: list[tuple[str, Optional[str]]]
self, tag: str, attrs_list: list[tuple[str, str | None]]
) -> None:
attrs = dict(attrs_list)
_check_version(tag, attrs)
Expand All @@ -149,7 +148,7 @@ def handle_starttag(
_, _, raw_filename = parsed_url.path.rpartition("/")
filename = urllib.parse.unquote(raw_filename)
url = urllib.parse.urlunparse((*parsed_url[:5], ""))
args: Dict[str, Any] = {"filename": filename, "url": url}
args: dict[str, Any] = {"filename": filename, "url": url}
# PEP 503:
# The URL SHOULD include a hash in the form of a URL fragment with the
# following syntax: #<hashname>=<hashvalue> ...
Expand Down Expand Up @@ -210,7 +209,7 @@ def from_project_details_html(html: str, name: str) -> ProjectDetails_1_0:
"""
parser = _ArchiveLinkHTMLParser()
parser.feed(html)
files: List[ProjectFileDetails_1_0] = []
files: list[ProjectFileDetails_1_0] = []
for archive_link in parser.archive_links:
details: ProjectFileDetails_1_0 = {
"filename": archive_link["filename"],
Expand All @@ -237,4 +236,4 @@ def from_project_details_html(html: str, name: str) -> ProjectDetails_1_0:
"meta": {"api-version": "1.0"},
"name": packaging.utils.canonicalize_name(name),
"files": files,
}
}
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
17 changes: 11 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ build-backend = "setuptools.build_meta"
write_to = "micropip/_version.py"

[tool.ruff]
line-length = 122
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

0 comments on commit 77abfb8

Please sign in to comment.