Skip to content

Commit 3d88026

Browse files
authored
Merge pull request #13585 from notatallshaw/remove-remove-unnecessary-canonicalize-name-calls
Minor: Remove unnecessary `canonicalize_name` calls and validate via `NormalizedName` type
2 parents 76fc136 + e8cc9e9 commit 3d88026

File tree

13 files changed

+41
-23
lines changed

13 files changed

+41
-23
lines changed

src/pip/_internal/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def get(
143143
wheel = Wheel(wheel_name)
144144
except InvalidWheelFilename:
145145
continue
146-
if canonicalize_name(wheel.name) != canonical_package_name:
146+
if wheel.name != canonical_package_name:
147147
logger.debug(
148148
"Ignoring cached wheel %s for %s as it "
149149
"does not match the expected distribution name %s.",

src/pip/_internal/index/package_finder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from pip._vendor.packaging import specifiers
1919
from pip._vendor.packaging.tags import Tag
20-
from pip._vendor.packaging.utils import canonicalize_name
20+
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
2121
from pip._vendor.packaging.version import InvalidVersion, _BaseVersion
2222
from pip._vendor.packaging.version import parse as parse_version
2323

@@ -127,7 +127,7 @@ class LinkEvaluator:
127127
def __init__(
128128
self,
129129
project_name: str,
130-
canonical_name: str,
130+
canonical_name: NormalizedName,
131131
formats: frozenset[str],
132132
target_python: TargetPython,
133133
allow_yanked: bool,
@@ -201,7 +201,7 @@ def evaluate_link(self, link: Link) -> tuple[LinkType, str]:
201201
LinkType.format_invalid,
202202
"invalid wheel filename",
203203
)
204-
if canonicalize_name(wheel.name) != self._canonical_name:
204+
if wheel.name != self._canonical_name:
205205
reason = f"wrong project name (not {self.project_name})"
206206
return (LinkType.different_project, reason)
207207

src/pip/_internal/metadata/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import functools
55
import os
66
import sys
7-
from typing import Literal, Protocol, cast
7+
from typing import TYPE_CHECKING, Literal, Protocol, cast
88

99
from pip._internal.utils.deprecation import deprecated
1010
from pip._internal.utils.misc import strtobool
1111

1212
from .base import BaseDistribution, BaseEnvironment, FilesystemWheel, MemoryWheel, Wheel
1313

14+
if TYPE_CHECKING:
15+
from pip._vendor.packaging.utils import NormalizedName
16+
1417
__all__ = [
1518
"BaseDistribution",
1619
"BaseEnvironment",
@@ -131,7 +134,9 @@ def get_directory_distribution(directory: str) -> BaseDistribution:
131134
return select_backend().Distribution.from_directory(directory)
132135

133136

134-
def get_wheel_distribution(wheel: Wheel, canonical_name: str) -> BaseDistribution:
137+
def get_wheel_distribution(
138+
wheel: Wheel, canonical_name: NormalizedName
139+
) -> BaseDistribution:
135140
"""Get the representation of the specified wheel's distribution metadata.
136141
137142
This returns a Distribution instance from the chosen backend based on

src/pip/_internal/network/lazy_wheel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Any
1212
from zipfile import BadZipFile, ZipFile
1313

14-
from pip._vendor.packaging.utils import canonicalize_name
14+
from pip._vendor.packaging.utils import NormalizedName
1515
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
1616

1717
from pip._internal.metadata import BaseDistribution, MemoryWheel, get_wheel_distribution
@@ -23,7 +23,9 @@ class HTTPRangeRequestUnsupported(Exception):
2323
pass
2424

2525

26-
def dist_from_wheel_url(name: str, url: str, session: PipSession) -> BaseDistribution:
26+
def dist_from_wheel_url(
27+
name: NormalizedName, url: str, session: PipSession
28+
) -> BaseDistribution:
2729
"""Return a distribution object from the given wheel URL.
2830
2931
This uses HTTP range requests to only fetch the portion of the wheel
@@ -37,7 +39,7 @@ def dist_from_wheel_url(name: str, url: str, session: PipSession) -> BaseDistrib
3739
wheel = MemoryWheel(zf.name, zf) # type: ignore
3840
# After context manager exit, wheel.name
3941
# is an invalid file by intention.
40-
return get_wheel_distribution(wheel, canonicalize_name(name))
42+
return get_wheel_distribution(wheel, name)
4143

4244

4345
class LazyZipOverHTTP:

src/pip/_internal/operations/prepare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def _fetch_metadata_using_lazy_wheel(
444444
return None
445445

446446
wheel = Wheel(link.filename)
447-
name = canonicalize_name(wheel.name)
447+
name = wheel.name
448448
logger.info(
449449
"Obtaining dependency information from %s %s",
450450
name,

src/pip/_internal/resolution/resolvelib/candidates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def __init__(
283283
assert ireq.link == link
284284
if ireq.link.is_wheel and not ireq.link.is_file:
285285
wheel = Wheel(ireq.link.filename)
286-
wheel_name = canonicalize_name(wheel.name)
286+
wheel_name = wheel.name
287287
assert name == wheel_name, f"{name!r} != {wheel_name!r} for wheel"
288288
# Version may not be present for PEP 508 direct URLs
289289
if version is not None:

src/pip/_internal/wheel_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _get_cache_dir(
118118
def _verify_one(req: InstallRequirement, wheel_path: str) -> None:
119119
canonical_name = canonicalize_name(req.name or "")
120120
w = Wheel(os.path.basename(wheel_path))
121-
if canonicalize_name(w.name) != canonical_name:
121+
if w.name != canonical_name:
122122
raise InvalidWheelFilename(
123123
f"Wheel has unexpected file name: expected {canonical_name!r}, "
124124
f"got {w.name!r}",

tests/lib/wheel.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from zipfile import ZipFile
2323

24+
from pip._vendor.packaging.utils import canonicalize_name
2425
from pip._vendor.requests.structures import CaseInsensitiveDict
2526

2627
from pip._internal.metadata import BaseDistribution, MemoryWheel, get_wheel_distribution
@@ -281,7 +282,9 @@ def as_zipfile(self) -> ZipFile:
281282

282283
def as_distribution(self, name: str) -> BaseDistribution:
283284
stream = BytesIO(self.as_bytes())
284-
return get_wheel_distribution(MemoryWheel(self._name, stream), name)
285+
return get_wheel_distribution(
286+
MemoryWheel(self._name, stream), canonicalize_name(name)
287+
)
285288

286289

287290
def make_wheel(

tests/unit/metadata/test_metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from pip._vendor.packaging.utils import NormalizedName
9+
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
1010

1111
from pip._internal.metadata import (
1212
BaseDistribution,
@@ -102,7 +102,7 @@ def test_metadata_dict(tmp_path: Path) -> None:
102102
"""
103103
wheel_path = make_wheel(name="pkga", version="1.0.1").save_to_dir(tmp_path)
104104
wheel = FilesystemWheel(wheel_path)
105-
dist = get_wheel_distribution(wheel, "pkga")
105+
dist = get_wheel_distribution(wheel, canonicalize_name("pkga"))
106106
metadata_dict = dist.metadata_dict
107107
assert metadata_dict["name"] == "pkga"
108108
assert metadata_dict["version"] == "1.0.1"

tests/unit/test_finder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pip._vendor.packaging.specifiers import SpecifierSet
88
from pip._vendor.packaging.tags import Tag
9+
from pip._vendor.packaging.utils import canonicalize_name
910
from pip._vendor.packaging.version import parse as parse_version
1011

1112
import pip._internal.utils.compatibility_tags
@@ -481,7 +482,7 @@ def make_test_link_evaluator(self, formats: Iterable[str]) -> LinkEvaluator:
481482
target_python = TargetPython()
482483
return LinkEvaluator(
483484
project_name="pytest",
484-
canonical_name="pytest",
485+
canonical_name=canonicalize_name("pytest"),
485486
formats=frozenset(formats),
486487
target_python=target_python,
487488
allow_yanked=True,

0 commit comments

Comments
 (0)