Skip to content

Commit 65d65e1

Browse files
committed
Workaround missing distribution name
1 parent 718cdef commit 65d65e1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

importlib_metadata/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from importlib import import_module
3030
from importlib.abc import MetaPathFinder
3131
from itertools import starmap
32-
from typing import List, Mapping, Optional, Tuple, Union
32+
from typing import List, Mapping, Optional, Tuple, Union, cast
3333

3434

3535
__all__ = [
@@ -1031,12 +1031,19 @@ def version(distribution_name):
10311031

10321032
def _compat_normalized_name(dist: Distribution) -> Optional[str]:
10331033
"""
1034-
Compatibility layer that honor name normalization for distributions
1034+
Compatibility shim to honor name normalization for distributions
10351035
that don't provide ``_normalized_name``
10361036
(as in ``importlib.metadata`` for Python 3.8/3.9).
10371037
"""
1038-
normalized = getattr(dist, '_normalized_name', None)
1039-
return normalized or Prepared.normalize(getattr(dist, "name", ""))
1038+
try:
1039+
return dist._normalized_name
1040+
except AttributeError:
1041+
if "PathDistribution" in dist.__class__.__name__:
1042+
dist_ = cast(PathDistribution, dist) # old implementation
1043+
return PathDistribution(dist_._path)._normalized_name
1044+
elif hasattr(dist, "name"):
1045+
return Prepared.normalize(dist.name)
1046+
raise
10401047

10411048

10421049
_unique = functools.partial(

tests/test_integration.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,26 @@ def setUp(self):
5656
super().setUp()
5757

5858
def _meta_path_finder(self):
59-
from importlib.metadata import DistributionFinder, PathDistribution
59+
from importlib.metadata import (
60+
Distribution,
61+
DistributionFinder,
62+
PathDistribution,
63+
)
6064
from importlib.util import spec_from_file_location
6165

6266
path = pathlib.Path(self.site_dir)
6367

68+
class CustomDistribution(Distribution):
69+
def __init__(self, name, path):
70+
self.name = name
71+
self._path_distribution = PathDistribution(path)
72+
73+
def read_text(self, filename):
74+
return self._path_distribution.read_text(filename)
75+
76+
def locate_file(self, path):
77+
return self._path_distribution.locate_file(path)
78+
6479
class CustomFinder:
6580
@classmethod
6681
def find_spec(cls, fullname, _path=None, _target=None):
@@ -72,6 +87,8 @@ def find_spec(cls, fullname, _path=None, _target=None):
7287
def find_distributions(self, context=DistributionFinder.Context()):
7388
for dist_info in path.glob("*.dist-info"):
7489
yield PathDistribution(dist_info)
90+
name, _, _ = str(dist_info).partition("-")
91+
yield CustomDistribution(name + "_custom", dist_info)
7592

7693
return CustomFinder
7794

@@ -85,8 +102,11 @@ def test_compatibility_with_old_stdlib_path_distribution(self):
85102

86103
assert list(distributions())
87104
assert distribution("distinfo_pkg")
105+
assert distribution("distinfo_pkg_custom")
88106
assert version("distinfo_pkg") > "0"
107+
assert version("distinfo_pkg_custom") > "0"
89108
assert list(metadata("distinfo_pkg"))
109+
assert list(metadata("distinfo_pkg_custom"))
90110
assert list(entry_points(group="entries"))
91111

92112

0 commit comments

Comments
 (0)