Skip to content

Commit 9c92bd1

Browse files
committed
Try this py39 compatability shim from importlib_metadata
1 parent 56ccd52 commit 9c92bd1

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

pipenv/environment.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pipenv.patched.pip._vendor.packaging.utils import canonicalize_name
2222
from pipenv.patched.pip._vendor.packaging.version import parse as parse_version
2323
from pipenv.utils import console
24+
from pipenv.utils.dependencies import normalized_name
2425
from pipenv.utils.fileutils import normalize_path, temp_path
2526
from pipenv.utils.funktools import chunked, unnest
2627
from pipenv.utils.indexes import prepare_pip_source_args
@@ -493,7 +494,7 @@ def get_distributions(self) -> Generator[importlib.metadata.Distribution, None,
493494
def find_egg(self, egg_dist: importlib.metadata.Distribution) -> str:
494495
"""Find an egg by name in the given environment"""
495496
site_packages = self.libdir[1]
496-
search_filename = f"{egg_dist.name}.egg-link"
497+
search_filename = f"{normalized_name(egg_dist)}.egg-link"
497498
try:
498499
user_site = site.getusersitepackages()
499500
except AttributeError:
@@ -528,7 +529,7 @@ def get_installed_packages(self) -> list[importlib.metadata.Distribution]:
528529
packages = [
529530
pkg
530531
for pkg in workingset
531-
if self.dist_is_in_project(pkg) and pkg.name != "python"
532+
if self.dist_is_in_project(pkg) and normalized_name(pkg) != "python"
532533
]
533534
return packages
534535

@@ -556,7 +557,8 @@ def get_package_info(
556557

557558
with self.get_finder() as finder:
558559
for dist in packages:
559-
all_candidates = finder.find_all_candidates(dist.name)
560+
name = normalized_name(dist)
561+
all_candidates = finder.find_all_candidates(name)
560562
if not self.pipfile.get("pre", finder.allow_all_prereleases):
561563
# Remove prereleases
562564
all_candidates = [
@@ -567,9 +569,7 @@ def get_package_info(
567569

568570
if not all_candidates:
569571
continue
570-
candidate_evaluator = finder.make_candidate_evaluator(
571-
project_name=dist.name
572-
)
572+
candidate_evaluator = finder.make_candidate_evaluator(project_name=name)
573573
best_candidate_result = candidate_evaluator.compute_best_candidate(
574574
all_candidates
575575
)
@@ -627,7 +627,7 @@ def get_package_requirements(self, pkg=None):
627627

628628
packages = self.get_installed_packages()
629629
if pkg:
630-
packages = [p for p in packages if p.name == pkg]
630+
packages = [p for p in packages if normalized_name(p) == pkg]
631631

632632
try:
633633
tree = PackageDAG.from_pkgs(packages)
@@ -707,14 +707,15 @@ def is_installed(self, pkgname):
707707
:rtype: bool
708708
"""
709709

710-
return any(d for d in self.get_distributions() if d.name == pkgname)
710+
return any(d for d in self.get_distributions() if normalized_name(d) == pkgname)
711711

712712
def is_satisfied(self, req: InstallRequirement):
713713
match = next(
714714
iter(
715715
d
716716
for d in self.get_distributions()
717-
if req.name and canonicalize_name(d.name) == canonicalize_name(req.name)
717+
if req.name
718+
and canonicalize_name(normalized_name(d)) == canonicalize_name(req.name)
718719
),
719720
None,
720721
)

pipenv/utils/dependencies.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import zipfile
99
from contextlib import contextmanager
1010
from functools import lru_cache
11+
from importlib.metadata import Distribution
1112
from pathlib import Path
1213
from tempfile import NamedTemporaryFile, TemporaryDirectory
1314
from typing import Any, AnyStr, Dict, List, Mapping, Optional, Sequence, Union
@@ -50,6 +51,18 @@
5051
from .markers import PipenvMarkers
5152

5253

54+
def normalized_name(dist: Distribution) -> Optional[str]:
55+
"""Borrowed from importlib_metadata/_py39compat.py
56+
Honor name normalization for distributions that don't provide ``_normalized_name``.
57+
"""
58+
try:
59+
return dist._normalized_name
60+
except AttributeError:
61+
from . import Prepared # -> delay to prevent circular imports.
62+
63+
return Prepared.normalize(getattr(dist, "name", None) or dist.metadata["Name"])
64+
65+
5366
def get_version(pipfile_entry):
5467
if str(pipfile_entry) == "{}" or is_star(pipfile_entry):
5568
return ""

0 commit comments

Comments
 (0)