Skip to content

Commit 4c2b5ae

Browse files
committed
Refactored parsing and handling of EntryPoint.value.
1 parent 45e8bde commit 4c2b5ae

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

importlib_metadata/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from importlib import import_module
2727
from importlib.abc import MetaPathFinder
2828
from itertools import starmap
29-
from typing import Any, Iterable, List, Mapping, Match, Optional, Set, cast
29+
from typing import Any, Iterable, List, Mapping, Optional, Set, cast
3030

3131
from . import _meta
3232
from ._collections import FreezableDefaultDict, Pair
@@ -133,6 +133,12 @@ def valid(line: str):
133133
return line and not line.startswith('#')
134134

135135

136+
class _EntryPointMatch(types.SimpleNamespace):
137+
module: str
138+
attr: str
139+
extras: str
140+
141+
136142
class EntryPoint:
137143
"""An entry point as defined by Python packaging conventions.
138144
@@ -185,28 +191,27 @@ def load(self) -> Any:
185191
is indicated by the value, return that module. Otherwise,
186192
return the named object.
187193
"""
188-
match = cast(Match, self.pattern.match(self.value))
189-
module = import_module(match.group('module'))
190-
attrs = filter(None, (match.group('attr') or '').split('.'))
194+
module = import_module(self.module)
195+
attrs = filter(None, (self.attr or '').split('.'))
191196
return functools.reduce(getattr, attrs, module)
192197

193198
@property
194199
def module(self) -> str:
195-
match = self.pattern.match(self.value)
196-
assert match is not None
197-
return match.group('module')
200+
return self._match.module
198201

199202
@property
200203
def attr(self) -> str:
201-
match = self.pattern.match(self.value)
202-
assert match is not None
203-
return match.group('attr')
204+
return self._match.attr
204205

205206
@property
206207
def extras(self) -> List[str]:
208+
return re.findall(r'\w+', self._match.extras or '')
209+
210+
@property
211+
def _match(self) -> _EntryPointMatch:
207212
match = self.pattern.match(self.value)
208213
assert match is not None
209-
return re.findall(r'\w+', match.group('extras') or '')
214+
return _EntryPointMatch(**match.groupdict())
210215

211216
def _for(self, dist):
212217
vars(self).update(dist=dist)

0 commit comments

Comments
 (0)