From 971a56a486f375a0f71594503e1b3634ef3fbbe9 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 24 Aug 2024 22:44:31 +0300 Subject: [PATCH 1/6] Apply ruff/Perflint rule PERF401 PERF401 Use a list comprehension to create a transformed list --- src/wheel/metadata.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/wheel/metadata.py b/src/wheel/metadata.py index b8098fa8..90d7a585 100644 --- a/src/wheel/metadata.py +++ b/src/wheel/metadata.py @@ -88,16 +88,14 @@ def safe_name(name: str) -> str: def requires_to_requires_dist(requirement: Requirement) -> str: """Return the version specifier for a requirement in PEP 345/566 fashion.""" if requirement.url: - return " @ " + requirement.url + return f" @ {requirement.url}" - requires_dist: list[str] = [] - for spec in requirement.specifier: - requires_dist.append(spec.operator + spec.version) + if requirement.specifier: + return " " + ",".join( + sorted(spec.operator + spec.version for spec in requirement.specifier) + ) - if requires_dist: - return " " + ",".join(sorted(requires_dist)) - else: - return "" + return "" def convert_requirements(requirements: list[str]) -> Iterator[str]: From cbb0258054a69cfd3b0abe650cef9fc4b95e96e1 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:00:33 +0300 Subject: [PATCH 2/6] Apply ruff/flake8-pie rule PIE810 PIE810 Call `endswith` once with a `tuple` --- src/wheel/macosx_libfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wheel/macosx_libfile.py b/src/wheel/macosx_libfile.py index abdfc9ed..26d780a1 100644 --- a/src/wheel/macosx_libfile.py +++ b/src/wheel/macosx_libfile.py @@ -436,7 +436,7 @@ def calculate_macosx_platform_tag(archive_root: StrPath, platform_tag: str) -> s versions_dict: dict[str, tuple[int, int]] = {} for dirpath, _dirnames, filenames in os.walk(archive_root): for filename in filenames: - if filename.endswith(".dylib") or filename.endswith(".so"): + if filename.endswith((".dylib", ".so")): lib_path = os.path.join(dirpath, filename) min_ver = extract_macosx_min_system_version(lib_path) if min_ver is not None: From 1f7b13bcfdff2c233a68b6a7d3151e484bcb567b Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:02:58 +0300 Subject: [PATCH 3/6] Apply ruff/Pylint rule PLC0208 PLC0208 Use a sequence type instead of a `set` when iterating over values --- tests/test_bdist_wheel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bdist_wheel.py b/tests/test_bdist_wheel.py index fcb2dfc4..b16393d5 100644 --- a/tests/test_bdist_wheel.py +++ b/tests/test_bdist_wheel.py @@ -171,7 +171,7 @@ def test_licenses_override(dummy_dist, monkeypatch, tmp_path, config_file, confi ) with WheelFile("dist/dummy_dist-1.0-py2.py3-none-any.whl") as wf: license_files = { - "dummy_dist-1.0.dist-info/" + fname for fname in {"DUMMYFILE", "LICENSE"} + "dummy_dist-1.0.dist-info/" + fname for fname in ("DUMMYFILE", "LICENSE") } assert set(wf.namelist()) == DEFAULT_FILES | license_files From f63006b4465db4a6c763996e42cb9ff5cfea24fd Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:05:03 +0300 Subject: [PATCH 4/6] Apply ruff/flake8-type-checking rule TCH003 TCH003 Move standard library import into a type-checking block --- src/wheel/cli/tags.py | 5 ++++- src/wheel/macosx_libfile.py | 2 +- src/wheel/metadata.py | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wheel/cli/tags.py b/src/wheel/cli/tags.py index 88da72e9..69478b03 100644 --- a/src/wheel/cli/tags.py +++ b/src/wheel/cli/tags.py @@ -3,11 +3,14 @@ import email.policy import itertools import os -from collections.abc import Iterable from email.parser import BytesParser +from typing import TYPE_CHECKING from ..wheelfile import WheelFile +if TYPE_CHECKING: + from collections.abc import Iterable + def _compute_tags(original_tags: Iterable[str], new_tags: str | None) -> set[str]: """Add or replace tags. Supports dot-separated tags""" diff --git a/src/wheel/macosx_libfile.py b/src/wheel/macosx_libfile.py index 26d780a1..f321c1fd 100644 --- a/src/wheel/macosx_libfile.py +++ b/src/wheel/macosx_libfile.py @@ -43,10 +43,10 @@ import ctypes import os import sys -from io import BufferedIOBase from typing import TYPE_CHECKING if TYPE_CHECKING: + from io import BufferedIOBase from typing import Union StrPath = Union[str, os.PathLike[str]] diff --git a/src/wheel/metadata.py b/src/wheel/metadata.py index 90d7a585..8f64b6ff 100644 --- a/src/wheel/metadata.py +++ b/src/wheel/metadata.py @@ -9,12 +9,14 @@ import os.path import re import textwrap -from email.message import Message from email.parser import Parser -from typing import Generator, Iterable, Iterator, Literal +from typing import TYPE_CHECKING, Generator, Iterable, Iterator, Literal from .vendored.packaging.requirements import Requirement +if TYPE_CHECKING: + from email.message import Message + def _nonblank(str: str) -> bool | Literal[""]: return str and not str.startswith("#") From f7c740bc057eb402ad4de307e0a57dd15e5edc70 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:08:09 +0300 Subject: [PATCH 5/6] Apply ruff/flynt rule FLY002 FLY002 Consider f-string instead of string join --- src/wheel/_bdist_wheel.py | 2 +- src/wheel/cli/convert.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wheel/_bdist_wheel.py b/src/wheel/_bdist_wheel.py index 3cc7165e..55ffa5ce 100644 --- a/src/wheel/_bdist_wheel.py +++ b/src/wheel/_bdist_wheel.py @@ -473,7 +473,7 @@ def write_wheelfile( for impl in impl_tag.split("."): for abi in abi_tag.split("."): for plat in plat_tag.split("."): - msg["Tag"] = "-".join((impl, abi, plat)) + msg["Tag"] = f"{impl}-{abi}-{plat}" wheelfile_path = os.path.join(wheelfile_base, "WHEEL") log.info(f"creating {wheelfile_path}") diff --git a/src/wheel/cli/convert.py b/src/wheel/cli/convert.py index 157a4a80..fc7f6e5b 100644 --- a/src/wheel/cli/convert.py +++ b/src/wheel/cli/convert.py @@ -231,7 +231,7 @@ def wininst2wheel(path: str, dest_dir: str) -> None: # CPython-specific. if arch != "any": pyver = pyver.replace("py", "cp") - wheel_name = "-".join((dist_info, pyver, abi, arch)) + wheel_name = f"{dist_info}-{pyver}-{abi}-{arch}" if root_is_purelib: bw = bdist_wheel(Distribution()) else: From c95b95bb214b0a704abdb739acc5ab4b5a3097c8 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:28:38 +0300 Subject: [PATCH 6/6] Enforce more ruff rules --- pyproject.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 6cf58061..e981b4e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,11 +96,16 @@ extend-exclude = ["src/wheel/vendored"] [tool.ruff.lint] extend-select = [ "B", # flake8-bugbear + "FLY", # flynt "G", # flake8-logging-format "I", # isort "ISC", # flake8-implicit-str-concat + "PERF", # Perflint "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PLC", # Pylint "RUF100", # unused noqa (yesqa) + "TCH", # flake8-type-checking "UP", # pyupgrade "W", # pycodestyle warnings ]