Skip to content

Commit 55c6070

Browse files
committed
Refactored parsing and handling of EntryPoint.value.
1 parent c10bdf3 commit 55c6070

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

importlib_metadata/__init__.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from importlib import import_module
2828
from importlib.abc import MetaPathFinder
2929
from itertools import starmap
30-
from re import Match
3130
from typing import Any, cast
3231

3332
from . import _meta
@@ -135,6 +134,12 @@ def valid(line: str):
135134
return line and not line.startswith('#')
136135

137136

137+
class _EntryPointMatch(types.SimpleNamespace):
138+
module: str
139+
attr: str
140+
extras: str
141+
142+
138143
class EntryPoint:
139144
"""An entry point as defined by Python packaging conventions.
140145
@@ -187,28 +192,27 @@ def load(self) -> Any:
187192
is indicated by the value, return that module. Otherwise,
188193
return the named object.
189194
"""
190-
match = cast(Match, self.pattern.match(self.value))
191-
module = import_module(match.group('module'))
192-
attrs = filter(None, (match.group('attr') or '').split('.'))
195+
module = import_module(self.module)
196+
attrs = filter(None, (self.attr or '').split('.'))
193197
return functools.reduce(getattr, attrs, module)
194198

195199
@property
196200
def module(self) -> str:
197-
match = self.pattern.match(self.value)
198-
assert match is not None
199-
return match.group('module')
201+
return self._match.module
200202

201203
@property
202204
def attr(self) -> str:
203-
match = self.pattern.match(self.value)
204-
assert match is not None
205-
return match.group('attr')
205+
return self._match.attr
206206

207207
@property
208208
def extras(self) -> list[str]:
209+
return re.findall(r'\w+', self._match.extras or '')
210+
211+
@property
212+
def _match(self) -> _EntryPointMatch:
209213
match = self.pattern.match(self.value)
210214
assert match is not None
211-
return re.findall(r'\w+', match.group('extras') or '')
215+
return _EntryPointMatch(**match.groupdict())
212216

213217
def _for(self, dist):
214218
vars(self).update(dist=dist)

0 commit comments

Comments
 (0)