Skip to content

Commit b1e2868

Browse files
authored
bpo-47004: Sync with importlib_metadata 4.11.3. (python#31854)
1 parent c99ac3c commit b1e2868

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
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

+39-18
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ class EntryPoint(DeprecatedTuple):
152152
See `the packaging docs on entry points
153153
<https://packaging.python.org/specifications/entry-points/>`_
154154
for more information.
155+
156+
>>> ep = EntryPoint(
157+
... name=None, group=None, value='package.module:attr [extra1, extra2]')
158+
>>> ep.module
159+
'package.module'
160+
>>> ep.attr
161+
'attr'
162+
>>> ep.extras
163+
['extra1', 'extra2']
155164
"""
156165

157166
pattern = re.compile(
@@ -203,7 +212,7 @@ def attr(self):
203212
@property
204213
def extras(self):
205214
match = self.pattern.match(self.value)
206-
return list(re.finditer(r'\w+', match.group('extras') or ''))
215+
return re.findall(r'\w+', match.group('extras') or '')
207216

208217
def _for(self, dist):
209218
vars(self).update(dist=dist)
@@ -221,6 +230,25 @@ def __iter__(self):
221230
return iter((self.name, self))
222231

223232
def matches(self, **params):
233+
"""
234+
EntryPoint matches the given parameters.
235+
236+
>>> ep = EntryPoint(group='foo', name='bar', value='bing:bong [extra1, extra2]')
237+
>>> ep.matches(group='foo')
238+
True
239+
>>> ep.matches(name='bar', value='bing:bong [extra1, extra2]')
240+
True
241+
>>> ep.matches(group='foo', name='other')
242+
False
243+
>>> ep.matches()
244+
True
245+
>>> ep.matches(extras=['extra1', 'extra2'])
246+
True
247+
>>> ep.matches(module='bing')
248+
True
249+
>>> ep.matches(attr='bong')
250+
True
251+
"""
224252
attrs = (getattr(self, param) for param in params)
225253
return all(map(operator.eq, params.values(), attrs))
226254

@@ -292,21 +320,15 @@ def wrapped(self, *args, **kwargs):
292320
self._warn()
293321
return getattr(super(), method_name)(*args, **kwargs)
294322

295-
return wrapped
296-
297-
for method_name in [
298-
'__setitem__',
299-
'__delitem__',
300-
'append',
301-
'reverse',
302-
'extend',
303-
'pop',
304-
'remove',
305-
'__iadd__',
306-
'insert',
307-
'sort',
308-
]:
309-
locals()[method_name] = _wrap_deprecated_method(method_name)
323+
return method_name, wrapped
324+
325+
locals().update(
326+
map(
327+
_wrap_deprecated_method,
328+
'__setitem__ __delitem__ append reverse extend pop remove '
329+
'__iadd__ insert sort'.split(),
330+
)
331+
)
310332

311333
def __add__(self, other):
312334
if not isinstance(other, tuple):
@@ -660,7 +682,7 @@ def _read_dist_info_reqs(self):
660682

661683
def _read_egg_info_reqs(self):
662684
source = self.read_text('requires.txt')
663-
return source and self._deps_from_requires_text(source)
685+
return pass_none(self._deps_from_requires_text)(source)
664686

665687
@classmethod
666688
def _deps_from_requires_text(cls, source):
@@ -765,7 +787,6 @@ def __new__(cls, root):
765787

766788
def __init__(self, root):
767789
self.root = root
768-
self.base = os.path.basename(self.root).lower()
769790

770791
def joinpath(self, child):
771792
return pathlib.Path(self.root, child)

Lib/test/test_importlib/test_metadata_api.py

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

223+
def test_requires_egg_info_empty(self):
224+
fixtures.build_files(
225+
{
226+
'requires.txt': '',
227+
},
228+
self.site_dir.joinpath('egginfo_pkg.egg-info'),
229+
)
230+
deps = requires('egginfo-pkg')
231+
assert deps == []
232+
223233
def test_requires_dist_info(self):
224234
deps = requires('distinfo-pkg')
225235
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)