Skip to content

Commit

Permalink
Add '_compat.repair_extras' to repair extras from older, broken impor…
Browse files Browse the repository at this point in the history
…tlib metadata implementations.
  • Loading branch information
jaraco committed Mar 13, 2022
1 parent dd2e326 commit 7245f07
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
8 changes: 4 additions & 4 deletions nspektr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
from jaraco.context import suppress
from jaraco.functools import apply

from ._compat import metadata
from ._compat import metadata, repair_extras


def resolve(req: Requirement):
"""
>>> resolve(Requirement('pytest<3'))
Traceback (most recent call last):
...
importlib_metadata.PackageNotFoundError: No package metadata was found for pytest<3
importlib.metadata.PackageNotFoundError: No package metadata was found for pytest<3
"""
dist = metadata.distribution(req.name)
if not req.specifier.contains(Version(dist.version), prereleases=True):
raise metadata.PackageNotFoundError(str(req))
dist.extras = req.extras # type: ignore
dist.extras = repair_extras(req.extras) # type: ignore
return dist


Expand Down Expand Up @@ -124,6 +124,6 @@ def check(ep):
...
ValueError: ('Unable to resolve all dependencies',...
"""
missing = list(unsatisfied(find_dependencies(ep.dist, ep.extras)))
missing = list(unsatisfied(find_dependencies(ep.dist, repair_extras(ep.extras))))
if missing:
raise ValueError("Unable to resolve all dependencies", missing)
21 changes: 18 additions & 3 deletions nspektr/_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
if False:
# bugfix from 4.11.2 is required
import contextlib


try:
import importlib.metadata as metadata
else:
except ImportError:
import importlib_metadata as metadata # type: ignore # noqa: F401


def repair_extras(extras):
"""
Repair extras that appear as match objects.
python/importlib_metadata#369 revealed a flaw in the EntryPoint
implementation. This function wraps the extras to ensure
they are proper strings even on older implementations.
"""
with contextlib.suppress(AttributeError):
return list(item.group(0) for item in extras)
return extras
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ install_requires =
jaraco.functools
more_itertools
packaging
importlib_metadata >= 4.11.2
importlib_metadata; python_version < "3.8"

[options.packages.find]
exclude =
Expand Down

0 comments on commit 7245f07

Please sign in to comment.