Skip to content

Commit b58d47f

Browse files
committed
code review
1 parent 791e45a commit b58d47f

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

importlib_metadata/__init__.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ._collections import FreezableDefaultDict, Pair
2121
from ._compat import (
2222
NullFinder,
23+
StrPath,
2324
install,
2425
pypy_partial,
2526
)
@@ -31,9 +32,7 @@
3132
from importlib import import_module
3233
from importlib.abc import MetaPathFinder
3334
from itertools import starmap
34-
from typing import Iterator, List, Mapping, Optional, Set, Union, cast
35-
36-
StrPath = Union[str, "os.PathLike[str]"]
35+
from typing import Iterable, List, Mapping, Optional, Set, cast
3736

3837
__all__ = [
3938
'Distribution',
@@ -124,8 +123,8 @@ def read(text, filter_=None):
124123
yield Pair(name, value)
125124

126125
@staticmethod
127-
def valid(line: str) -> bool:
128-
return bool(line) and not line.startswith('#')
126+
def valid(line: str):
127+
return line and not line.startswith('#')
129128

130129

131130
class DeprecatedTuple:
@@ -388,19 +387,19 @@ def from_name(cls, name: str) -> "Distribution":
388387
if not name:
389388
raise ValueError("A distribution name is required.")
390389
try:
391-
return next(cls.discover(name=name))
390+
return next(iter(cls.discover(name=name)))
392391
except StopIteration:
393392
raise PackageNotFoundError(name)
394393

395394
@classmethod
396-
def discover(cls, **kwargs) -> Iterator["Distribution"]:
397-
"""Return an iterator of Distribution objects for all packages.
395+
def discover(cls, **kwargs) -> Iterable["Distribution"]:
396+
"""Return an iterable of Distribution objects for all packages.
398397
399398
Pass a ``context`` or pass keyword arguments for constructing
400399
a context.
401400
402401
:context: A ``DistributionFinder.Context`` object.
403-
:return: Iterator of Distribution objects for all packages.
402+
:return: Iterable of Distribution objects for all packages.
404403
"""
405404
context = kwargs.pop('context', None)
406405
if context and kwargs:
@@ -411,7 +410,7 @@ def discover(cls, **kwargs) -> Iterator["Distribution"]:
411410
)
412411

413412
@staticmethod
414-
def at(path: StrPath) -> "PathDistribution":
413+
def at(path: StrPath) -> "Distribution":
415414
"""Return a Distribution for the indicated metadata path
416415
417416
:param path: a string or path-like object
@@ -638,11 +637,11 @@ def path(self) -> List[str]:
638637
return vars(self).get('path', sys.path)
639638

640639
@abc.abstractmethod
641-
def find_distributions(self, context=Context()) -> Iterator[Distribution]:
640+
def find_distributions(self, context=Context()) -> Iterable[Distribution]:
642641
"""
643642
Find distributions.
644643
645-
Return an iterator of all Distribution instances capable of
644+
Return an iterable of all Distribution instances capable of
646645
loading the metadata for packages matching the ``context``,
647646
a DistributionFinder.Context instance.
648647
"""
@@ -775,11 +774,11 @@ class MetadataPathFinder(NullFinder, DistributionFinder):
775774

776775
def find_distributions(
777776
self, context=DistributionFinder.Context()
778-
) -> Iterator["PathDistribution"]:
777+
) -> Iterable["PathDistribution"]:
779778
"""
780779
Find distributions.
781780
782-
Return an iterator of all Distribution instances capable of
781+
Return an iterable of all Distribution instances capable of
783782
loading the metadata for packages matching ``context.name``
784783
(or all names if ``None`` indicated) along the paths in the list
785784
of directories ``context.path``.
@@ -863,10 +862,10 @@ def distribution(distribution_name) -> Distribution:
863862
return Distribution.from_name(distribution_name)
864863

865864

866-
def distributions(**kwargs) -> Iterator[Distribution]:
865+
def distributions(**kwargs) -> Iterable[Distribution]:
867866
"""Get all ``Distribution`` instances in the current environment.
868867
869-
:return: An iterator of ``Distribution`` instances.
868+
:return: An iterable of ``Distribution`` instances.
870869
"""
871870
return Distribution.discover(**kwargs)
872871

@@ -927,7 +926,7 @@ def requires(distribution_name) -> Optional[List[str]]:
927926
"""
928927
Return a list of requirements for the named package.
929928
930-
:return: An iterator of requirements, suitable for
929+
:return: An iterable of requirements, suitable for
931930
packaging.requirement.Requirement.
932931
"""
933932
return distribution(distribution_name).requires

importlib_metadata/_compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import os
12
import sys
23
import platform
34

5+
from typing import Union
6+
47

58
__all__ = ['install', 'NullFinder', 'Protocol']
69

@@ -70,3 +73,10 @@ def pypy_partial(val):
7073
"""
7174
is_pypy = platform.python_implementation() == 'PyPy'
7275
return val + is_pypy
76+
77+
78+
if sys.version_info >= (3, 9):
79+
StrPath = Union[str, os.PathLike[str]]
80+
else:
81+
# PathLike is only subscriptable at runtime in 3.9+
82+
StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover

0 commit comments

Comments
 (0)