Skip to content

Commit

Permalink
Merge branch 'bugfix/86-compatibility' into 'master'
Browse files Browse the repository at this point in the history
Add compatibility for early Python 3.8 beta releases

Closes #86

See merge request python-devs/importlib_metadata!94
  • Loading branch information
jaraco committed Sep 16, 2019
2 parents f06bd39 + c04c4b4 commit 08a3782
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def from_name(cls, name):
metadata cannot be found.
"""
for resolver in cls._discover_resolvers():
dists = resolver(DistributionFinder.Context(name=name))
context = DistributionFinder.Context(name=name)
dists = cls._maybe_bind(resolver, context)
dist = next(dists, None)
if dist is not None:
return dist
Expand All @@ -200,10 +201,24 @@ def discover(cls, **kwargs):
raise ValueError("cannot accept context and kwargs")
context = context or DistributionFinder.Context(**kwargs)
return itertools.chain.from_iterable(
resolver(context)
cls._maybe_bind(resolver, context)
for resolver in cls._discover_resolvers()
)

@staticmethod
def _maybe_bind(resolver, context):
"""
Only bind the context to the resolver if as a callable,
the resolver accepts the context parameter.
Workaround for
https://gitlab.com/python-devs/importlib_metadata/issues/86
"""
try: # pragma: nocover
return resolver(context)
except TypeError: # pragma: nocover
return resolver(name=context.name, path=context.path)

@staticmethod
def at(path):
"""Return a Distribution for the indicated metadata path
Expand Down
8 changes: 8 additions & 0 deletions importlib_metadata/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
importlib_metadata NEWS
=========================

0.23
====
* Added a compatibility shim to prevent failures on beta releases
of Python before the signature changed to accept the
"context" parameter on find_distributions. This workaround
will have a limited lifespan, not to extend beyond release of
Python 3.8 final.

0.22
====
* Renamed ``package`` parameter to ``distribution_name``
Expand Down

0 comments on commit 08a3782

Please sign in to comment.