Skip to content

Commit 344a6ff

Browse files
committed
Refactor Distribution.from_name to avoid return in loop and unnecessary None sentinel.
1 parent d3fe031 commit 344a6ff

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

importlib_metadata/__init__.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,13 @@ def from_name(cls, name):
557557
:raises PackageNotFoundError: When the named package's distribution
558558
metadata cannot be found.
559559
"""
560-
for resolver in cls._discover_resolvers():
561-
dists = resolver(DistributionFinder.Context(name=name))
562-
dist = next(iter(dists), None)
563-
if dist is not None:
564-
return dist
565-
else:
560+
dists = itertools.chain.from_iterable(
561+
resolver(DistributionFinder.Context(name=name))
562+
for resolver in cls._discover_resolvers()
563+
)
564+
try:
565+
return next(dists)
566+
except StopIteration:
566567
raise PackageNotFoundError(name)
567568

568569
@classmethod

0 commit comments

Comments
 (0)