Skip to content

Commit ad7c371

Browse files
committed
Make 'dist' a single, optional attribute on an EntryPoint.
1 parent bd5cac5 commit ad7c371

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

importlib_metadata/__init__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from importlib import import_module
2424
from importlib.abc import MetaPathFinder
2525
from itertools import starmap
26+
from typing import Optional
2627

2728

2829
__all__ = [
@@ -53,7 +54,7 @@ def name(self):
5354

5455

5556
class EntryPoint(
56-
PyPy_repr, collections.namedtuple('EntryPointBase', 'dist name value group')
57+
PyPy_repr, collections.namedtuple('EntryPointBase', 'name value group')
5758
):
5859
"""An entry point as defined by Python packaging conventions.
5960
@@ -83,6 +84,8 @@ class EntryPoint(
8384
following the attr, and following any extras.
8485
"""
8586

87+
dist: Optional['Distribution'] = None
88+
8689
def load(self):
8790
"""Load the entry point from its definition. If only a module
8891
is indicated by the value, return that module. Otherwise,
@@ -109,20 +112,28 @@ def extras(self):
109112
return list(re.finditer(r'\w+', match.group('extras') or ''))
110113

111114
@classmethod
112-
def _from_config(cls, dist, config):
113-
return [
114-
cls(dist, name, value, group)
115+
def _from_config(cls, config):
116+
return (
117+
cls(name, value, group)
115118
for group in config.sections()
116119
for name, value in config.items(group)
117-
]
120+
)
118121

119122
@classmethod
120-
def _from_text(cls, dist, text):
123+
def _from_text(cls, text):
121124
config = ConfigParser(delimiters='=')
122125
# case sensitive: https://stackoverflow.com/q/1611799/812183
123126
config.optionxform = str
124127
config.read_string(text)
125-
return EntryPoint._from_config(dist, config)
128+
return cls._from_config(config)
129+
130+
@classmethod
131+
def _from_text_for(cls, text, dist):
132+
return (ep._for(dist) for ep in cls._from_text(text))
133+
134+
def _for(self, dist):
135+
self.dist = dist
136+
return self
126137

127138
def __iter__(self):
128139
"""
@@ -273,7 +284,7 @@ def version(self):
273284

274285
@property
275286
def entry_points(self):
276-
return EntryPoint._from_text(self, self.read_text('entry_points.txt'))
287+
return list(EntryPoint._from_text_for(self.read_text('entry_points.txt'), self))
277288

278289
@property
279290
def files(self):

0 commit comments

Comments
 (0)