Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 Doc/library/importlib.metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Python packages or modules::

.. versionadded:: 3.10

.. _distributions:

Distributions
=============
Expand Down
33 changes: 30 additions & 3 deletions 33 Lib/importlib/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ class EntryPoint(
See `the packaging docs on entry points
<https://packaging.python.org/specifications/entry-points/>`_
for more information.

>>> ep = EntryPoint(
... name=None, group=None, value='package.module:attr [extra1, extra2]')
>>> ep.module
'package.module'
>>> ep.attr
'attr'
>>> ep.extras
['extra1', 'extra2']
"""

pattern = re.compile(
Expand Down Expand Up @@ -176,7 +185,7 @@ def attr(self):
@property
def extras(self):
match = self.pattern.match(self.value)
return list(re.finditer(r'\w+', match.group('extras') or ''))
return re.findall(r'\w+', match.group('extras') or '')

def _for(self, dist):
self.dist = dist
Expand All @@ -200,6 +209,25 @@ def __reduce__(self):
)

def matches(self, **params):
"""
EntryPoint matches the given parameters.

>>> ep = EntryPoint(group='foo', name='bar', value='bing:bong [extra1, extra2]')
>>> ep.matches(group='foo')
True
>>> ep.matches(name='bar', value='bing:bong [extra1, extra2]')
True
>>> ep.matches(group='foo', name='other')
False
>>> ep.matches()
True
>>> ep.matches(extras=['extra1', 'extra2'])
True
>>> ep.matches(module='bing')
True
>>> ep.matches(attr='bong')
True
"""
attrs = (getattr(self, param) for param in params)
return all(map(operator.eq, params.values(), attrs))

Expand Down Expand Up @@ -650,7 +678,7 @@ def _read_dist_info_reqs(self):

def _read_egg_info_reqs(self):
source = self.read_text('requires.txt')
return source and self._deps_from_requires_text(source)
return None if source is None else self._deps_from_requires_text(source)

@classmethod
def _deps_from_requires_text(cls, source):
Expand Down Expand Up @@ -752,7 +780,6 @@ def __new__(cls, root):

def __init__(self, root):
self.root = root
self.base = os.path.basename(self.root).lower()

def joinpath(self, child):
return pathlib.Path(self.root, child)
Expand Down
10 changes: 10 additions & 0 deletions 10 Lib/test/test_importlib/test_metadata_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ def test_requires_egg_info(self):
assert len(deps) == 2
assert any(dep == 'wheel >= 1.0; python_version >= "2.7"' for dep in deps)

def test_requires_egg_info_empty(self):
fixtures.build_files(
{
'requires.txt': '',
},
self.site_dir.joinpath('egginfo_pkg.egg-info'),
)
deps = requires('egginfo-pkg')
assert deps == []

def test_requires_dist_info(self):
deps = requires('distinfo-pkg')
assert len(deps) == 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Apply bugfixes from importlib_metadata 4.11.3, including bugfix for
EntryPoint.extras, which was returning match objects and not the extras
strings.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.