Skip to content

Commit d929aa7

Browse files
authored
[3.10] bpo-47004: Sync with importlib_metadata 4.11.3. (GH-31854). (GH-31857)
(cherry picked from commit b1e2868) Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
1 parent 25962e4 commit d929aa7

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

Doc/library/importlib.metadata.rst

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ Python packages or modules::
264264

265265
.. versionadded:: 3.10
266266

267+
.. _distributions:
267268

268269
Distributions
269270
=============

Lib/importlib/metadata/__init__.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ class EntryPoint(
128128
See `the packaging docs on entry points
129129
<https://packaging.python.org/specifications/entry-points/>`_
130130
for more information.
131+
132+
>>> ep = EntryPoint(
133+
... name=None, group=None, value='package.module:attr [extra1, extra2]')
134+
>>> ep.module
135+
'package.module'
136+
>>> ep.attr
137+
'attr'
138+
>>> ep.extras
139+
['extra1', 'extra2']
131140
"""
132141

133142
pattern = re.compile(
@@ -176,7 +185,7 @@ def attr(self):
176185
@property
177186
def extras(self):
178187
match = self.pattern.match(self.value)
179-
return list(re.finditer(r'\w+', match.group('extras') or ''))
188+
return re.findall(r'\w+', match.group('extras') or '')
180189

181190
def _for(self, dist):
182191
self.dist = dist
@@ -200,6 +209,25 @@ def __reduce__(self):
200209
)
201210

202211
def matches(self, **params):
212+
"""
213+
EntryPoint matches the given parameters.
214+
215+
>>> ep = EntryPoint(group='foo', name='bar', value='bing:bong [extra1, extra2]')
216+
>>> ep.matches(group='foo')
217+
True
218+
>>> ep.matches(name='bar', value='bing:bong [extra1, extra2]')
219+
True
220+
>>> ep.matches(group='foo', name='other')
221+
False
222+
>>> ep.matches()
223+
True
224+
>>> ep.matches(extras=['extra1', 'extra2'])
225+
True
226+
>>> ep.matches(module='bing')
227+
True
228+
>>> ep.matches(attr='bong')
229+
True
230+
"""
203231
attrs = (getattr(self, param) for param in params)
204232
return all(map(operator.eq, params.values(), attrs))
205233

@@ -650,7 +678,7 @@ def _read_dist_info_reqs(self):
650678

651679
def _read_egg_info_reqs(self):
652680
source = self.read_text('requires.txt')
653-
return source and self._deps_from_requires_text(source)
681+
return None if source is None else self._deps_from_requires_text(source)
654682

655683
@classmethod
656684
def _deps_from_requires_text(cls, source):
@@ -752,7 +780,6 @@ def __new__(cls, root):
752780

753781
def __init__(self, root):
754782
self.root = root
755-
self.base = os.path.basename(self.root).lower()
756783

757784
def joinpath(self, child):
758785
return pathlib.Path(self.root, child)

Lib/test/test_importlib/test_metadata_api.py

+10
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ def test_requires_egg_info(self):
222222
assert len(deps) == 2
223223
assert any(dep == 'wheel >= 1.0; python_version >= "2.7"' for dep in deps)
224224

225+
def test_requires_egg_info_empty(self):
226+
fixtures.build_files(
227+
{
228+
'requires.txt': '',
229+
},
230+
self.site_dir.joinpath('egginfo_pkg.egg-info'),
231+
)
232+
deps = requires('egginfo-pkg')
233+
assert deps == []
234+
225235
def test_requires_dist_info(self):
226236
deps = requires('distinfo-pkg')
227237
assert len(deps) == 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Apply bugfixes from importlib_metadata 4.11.3, including bugfix for
2+
EntryPoint.extras, which was returning match objects and not the extras
3+
strings.

0 commit comments

Comments
 (0)