Skip to content

Commit

Permalink
Refactor Distribution.from_name to avoid return in loop and unnecessa…
Browse files Browse the repository at this point in the history
…ry None sentinel.
  • Loading branch information
jaraco committed Jun 25, 2022
1 parent d3fe031 commit 344a6ff
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,13 @@ def from_name(cls, name):
:raises PackageNotFoundError: When the named package's distribution
metadata cannot be found.
"""
for resolver in cls._discover_resolvers():
dists = resolver(DistributionFinder.Context(name=name))
dist = next(iter(dists), None)
if dist is not None:
return dist
else:
dists = itertools.chain.from_iterable(
resolver(DistributionFinder.Context(name=name))
for resolver in cls._discover_resolvers()
)
try:
return next(dists)
except StopIteration:
raise PackageNotFoundError(name)

@classmethod
Expand Down

0 comments on commit 344a6ff

Please sign in to comment.