Skip to content

Commit f215c5e

Browse files
authored
Merge pull request #13660 from notatallshaw/assume-packaging-Versions-on-python-3.11+
Don't convert versions to strings when using importlib.metadata backend
2 parents e18a82f + bb0d5f5 commit f215c5e

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

news/13660.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When using the ``importlib.metadata`` backend, pip skips redundant work
2+
converting version objects to strings and back to version objects.

src/pip/_internal/index/package_finder.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pip._vendor.packaging import specifiers
1919
from pip._vendor.packaging.tags import Tag
2020
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
21-
from pip._vendor.packaging.version import InvalidVersion, _BaseVersion
21+
from pip._vendor.packaging.version import InvalidVersion, Version, _BaseVersion
2222
from pip._vendor.packaging.version import parse as parse_version
2323

2424
from pip._internal.exceptions import (
@@ -28,6 +28,7 @@
2828
UnsupportedWheel,
2929
)
3030
from pip._internal.index.collector import LinkCollector, parse_links
31+
from pip._internal.metadata import select_backend
3132
from pip._internal.models.candidate import InstallationCandidate
3233
from pip._internal.models.format_control import FormatControl
3334
from pip._internal.models.link import Link
@@ -458,14 +459,19 @@ def get_applicable_candidates(
458459
allow_prereleases = self._allow_all_prereleases or None
459460
specifier = self._specifier
460461

461-
# We turn the version object into a str here because otherwise
462-
# when we're debundled but setuptools isn't, Python will see
463-
# packaging.version.Version and
462+
# When using the pkg_resources backend we turn the version object into
463+
# a str here because otherwise when we're debundled but setuptools isn't,
464+
# Python will see packaging.version.Version and
464465
# pkg_resources._vendor.packaging.version.Version as different
465466
# types. This way we'll use a str as a common data interchange
466467
# format. If we stop using the pkg_resources provided specifier
467468
# and start using our own, we can drop the cast to str().
468-
candidates_and_versions = [(c, str(c.version)) for c in candidates]
469+
if select_backend().NAME == "pkg_resources":
470+
candidates_and_versions: list[
471+
tuple[InstallationCandidate, str | Version]
472+
] = [(c, str(c.version)) for c in candidates]
473+
else:
474+
candidates_and_versions = [(c, c.version) for c in candidates]
469475
versions = set(
470476
specifier.filter(
471477
(v for _, v in candidates_and_versions),

0 commit comments

Comments
 (0)