Skip to content

Commit 1816266

Browse files
Merge pull request #266 from python/bugfix/bpo-42382
2 parents 877d351 + c19a2d0 commit 1816266

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
v3.3.0
2+
======
3+
4+
* * #265: ``EntryPoint`` objects now expose a ``.dist`` object
5+
referencing the ``Distribution`` when constructed from a
6+
Distribution.
7+
18
v3.2.0
29
======
310

importlib_metadata/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from importlib import import_module
2525
from importlib.abc import MetaPathFinder
2626
from itertools import starmap
27-
from typing import Any, List, TypeVar, Union
27+
from typing import Any, List, Optional, TypeVar, Union
2828

2929

3030
__all__ = [
@@ -85,6 +85,8 @@ class EntryPoint(
8585
following the attr, and following any extras.
8686
"""
8787

88+
dist: Optional['Distribution'] = None
89+
8890
def load(self):
8991
"""Load the entry point from its definition. If only a module
9092
is indicated by the value, return that module. Otherwise,
@@ -112,19 +114,27 @@ def extras(self):
112114

113115
@classmethod
114116
def _from_config(cls, config):
115-
return [
117+
return (
116118
cls(name, value, group)
117119
for group in config.sections()
118120
for name, value in config.items(group)
119-
]
121+
)
120122

121123
@classmethod
122124
def _from_text(cls, text):
123125
config = ConfigParser(delimiters='=')
124126
# case sensitive: https://stackoverflow.com/q/1611799/812183
125127
config.optionxform = str
126128
config.read_string(text)
127-
return EntryPoint._from_config(config)
129+
return cls._from_config(config)
130+
131+
@classmethod
132+
def _from_text_for(cls, text, dist):
133+
return (ep._for(dist) for ep in cls._from_text(text))
134+
135+
def _for(self, dist):
136+
self.dist = dist
137+
return self
128138

129139
def __iter__(self):
130140
"""
@@ -282,14 +292,19 @@ def metadata(self) -> PackageMetadata:
282292
)
283293
return email.message_from_string(text)
284294

295+
@property
296+
def name(self):
297+
"""Return the 'Name' metadata for the distribution package."""
298+
return self.metadata['Name']
299+
285300
@property
286301
def version(self):
287302
"""Return the 'Version' metadata for the distribution package."""
288303
return self.metadata['Version']
289304

290305
@property
291306
def entry_points(self):
292-
return EntryPoint._from_text(self.read_text('entry_points.txt'))
307+
return list(EntryPoint._from_text_for(self.read_text('entry_points.txt'), self))
293308

294309
@property
295310
def files(self):

tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ def test_entry_points(self):
6969
self.assertEqual(ep.value, 'mod:main')
7070
self.assertEqual(ep.extras, [])
7171

72+
def test_entry_points_distribution(self):
73+
entries = dict(entry_points()['entries'])
74+
for entry in ("main", "ns:sub"):
75+
ep = entries[entry]
76+
self.assertIn(ep.dist.name, ('distinfo-pkg', 'egginfo-pkg'))
77+
self.assertEqual(ep.dist.version, "1.0.0")
78+
7279
def test_metadata_for_this_package(self):
7380
md = metadata('egginfo-pkg')
7481
assert md['author'] == 'Steven Ma'

0 commit comments

Comments
 (0)