Skip to content

Commit bd5cac5

Browse files
s0undt3chjaraco
authored andcommitted
Make sure each EntryPoint carries it's Distribution information
1 parent 96cc328 commit bd5cac5

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

importlib_metadata/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def name(self):
5353

5454

5555
class EntryPoint(
56-
PyPy_repr, collections.namedtuple('EntryPointBase', 'name value group')
56+
PyPy_repr, collections.namedtuple('EntryPointBase', 'dist name value group')
5757
):
5858
"""An entry point as defined by Python packaging conventions.
5959
@@ -109,20 +109,20 @@ def extras(self):
109109
return list(re.finditer(r'\w+', match.group('extras') or ''))
110110

111111
@classmethod
112-
def _from_config(cls, config):
112+
def _from_config(cls, dist, config):
113113
return [
114-
cls(name, value, group)
114+
cls(dist, name, value, group)
115115
for group in config.sections()
116116
for name, value in config.items(group)
117117
]
118118

119119
@classmethod
120-
def _from_text(cls, text):
120+
def _from_text(cls, dist, text):
121121
config = ConfigParser(delimiters='=')
122122
# case sensitive: https://stackoverflow.com/q/1611799/812183
123123
config.optionxform = str
124124
config.read_string(text)
125-
return EntryPoint._from_config(config)
125+
return EntryPoint._from_config(dist, config)
126126

127127
def __iter__(self):
128128
"""
@@ -261,14 +261,19 @@ def metadata(self):
261261
)
262262
return email.message_from_string(text)
263263

264+
@property
265+
def name(self):
266+
"""Return the 'Name' metadata for the distribution package."""
267+
return self.metadata['Name']
268+
264269
@property
265270
def version(self):
266271
"""Return the 'Version' metadata for the distribution package."""
267272
return self.metadata['Version']
268273

269274
@property
270275
def entry_points(self):
271-
return EntryPoint._from_text(self.read_text('entry_points.txt'))
276+
return EntryPoint._from_text(self, self.read_text('entry_points.txt'))
272277

273278
@property
274279
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.assertEqual(ep.dist.name, "distinfo-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)