From 344a6ffc612eec611592e7686264ced72f64da5a Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 24 Jun 2022 22:18:07 -0400 Subject: [PATCH] Refactor Distribution.from_name to avoid return in loop and unnecessary None sentinel. --- importlib_metadata/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 29ce1175..5c52a6d3 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -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