Skip to content

Commit 08a3782

Browse files
committed
Merge branch 'bugfix/86-compatibility' into 'master'
Add compatibility for early Python 3.8 beta releases Closes #86 See merge request python-devs/importlib_metadata!94
2 parents f06bd39 + c04c4b4 commit 08a3782

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

importlib_metadata/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def from_name(cls, name):
178178
metadata cannot be found.
179179
"""
180180
for resolver in cls._discover_resolvers():
181-
dists = resolver(DistributionFinder.Context(name=name))
181+
context = DistributionFinder.Context(name=name)
182+
dists = cls._maybe_bind(resolver, context)
182183
dist = next(dists, None)
183184
if dist is not None:
184185
return dist
@@ -200,10 +201,24 @@ def discover(cls, **kwargs):
200201
raise ValueError("cannot accept context and kwargs")
201202
context = context or DistributionFinder.Context(**kwargs)
202203
return itertools.chain.from_iterable(
203-
resolver(context)
204+
cls._maybe_bind(resolver, context)
204205
for resolver in cls._discover_resolvers()
205206
)
206207

208+
@staticmethod
209+
def _maybe_bind(resolver, context):
210+
"""
211+
Only bind the context to the resolver if as a callable,
212+
the resolver accepts the context parameter.
213+
214+
Workaround for
215+
https://gitlab.com/python-devs/importlib_metadata/issues/86
216+
"""
217+
try: # pragma: nocover
218+
return resolver(context)
219+
except TypeError: # pragma: nocover
220+
return resolver(name=context.name, path=context.path)
221+
207222
@staticmethod
208223
def at(path):
209224
"""Return a Distribution for the indicated metadata path

importlib_metadata/docs/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
importlib_metadata NEWS
33
=========================
44

5+
0.23
6+
====
7+
* Added a compatibility shim to prevent failures on beta releases
8+
of Python before the signature changed to accept the
9+
"context" parameter on find_distributions. This workaround
10+
will have a limited lifespan, not to extend beyond release of
11+
Python 3.8 final.
12+
513
0.22
614
====
715
* Renamed ``package`` parameter to ``distribution_name``

0 commit comments

Comments
 (0)