Skip to content

Commit 4fe96a4

Browse files
committed
Move code specific to backport to compat module
1 parent 2044db1 commit 4fe96a4

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

importlib_metadata/__init__.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import collections
1919

2020
from . import _meta
21-
from .compat import py39, py311
21+
from .compat import py39, py311, stdlib
2222
from ._collections import FreezableDefaultDict, Pair
2323
from ._compat import (
2424
NullFinder,
@@ -30,16 +30,10 @@
3030

3131
from contextlib import suppress
3232
from importlib import import_module
33-
from importlib import metadata as _legacy
3433
from importlib.abc import MetaPathFinder
3534
from itertools import starmap
3635
from typing import Any, Iterable, List, Mapping, Match, Optional, Set, cast
3736

38-
if sys.version_info >= (3, 10):
39-
from importlib.metadata import PackageMetadata as _legacy_Metadata
40-
else:
41-
from email.message import Message as _legacy_Metadata
42-
4337

4438
__all__ = [
4539
'Distribution',
@@ -382,7 +376,7 @@ def locate_file(self, path: str | os.PathLike[str]) -> SimplePath:
382376
"""
383377

384378
@classmethod
385-
def from_name(cls, name: str) -> Distribution | _legacy.Distribution:
379+
def from_name(cls, name: str) -> stdlib._DistributionOrLegacy:
386380
"""Return the Distribution for the given package name.
387381
388382
:param name: The name of the distribution package to search for.
@@ -402,7 +396,7 @@ def from_name(cls, name: str) -> Distribution | _legacy.Distribution:
402396
@classmethod
403397
def discover(
404398
cls, *, context: Optional[DistributionFinder.Context] = None, **kwargs
405-
) -> Iterable[Distribution | _legacy.Distribution]:
399+
) -> Iterable[stdlib._DistributionOrLegacy]:
406400
"""Return an iterable of Distribution objects for all packages.
407401
408402
Pass a ``context`` or pass keyword arguments for constructing
@@ -948,7 +942,7 @@ def _name_from_stem(stem):
948942
return name
949943

950944

951-
def distribution(distribution_name: str) -> Distribution | _legacy.Distribution:
945+
def distribution(distribution_name: str) -> stdlib._DistributionOrLegacy:
952946
"""Get the ``Distribution`` instance for the named package.
953947
954948
:param distribution_name: The name of the distribution package as a string.
@@ -957,15 +951,15 @@ def distribution(distribution_name: str) -> Distribution | _legacy.Distribution:
957951
return Distribution.from_name(distribution_name)
958952

959953

960-
def distributions(**kwargs) -> Iterable[Distribution | _legacy.Distribution]:
954+
def distributions(**kwargs) -> Iterable[stdlib._DistributionOrLegacy]:
961955
"""Get all ``Distribution`` instances in the current environment.
962956
963957
:return: An iterable of ``Distribution`` instances.
964958
"""
965959
return Distribution.discover(**kwargs)
966960

967961

968-
def metadata(distribution_name: str) -> _meta.PackageMetadata | _legacy_Metadata:
962+
def metadata(distribution_name: str) -> stdlib._PackageMetadataOrLegacy:
969963
"""Get the metadata for the named package.
970964
971965
:param distribution_name: The name of the distribution package to query.
@@ -1010,7 +1004,7 @@ def entry_points(**params) -> EntryPoints:
10101004

10111005
def files(
10121006
distribution_name: str,
1013-
) -> Optional[List[PackagePath] | List[_legacy.PackagePath]]:
1007+
) -> Optional[stdlib._List_PackagePathOrLegacy]:
10141008
"""Return a list of files for the named package.
10151009
10161010
:param distribution_name: The name of the distribution package to query.

importlib_metadata/compat/stdlib.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Compatibility layer with stdlib.
3+
Only needed when distributing via PyPI/3rd-party package.
4+
"""
5+
6+
import sys
7+
from typing import TYPE_CHECKING, List, Union
8+
9+
if TYPE_CHECKING:
10+
# Avoid circular imports
11+
12+
from importlib import metadata as _legacy
13+
14+
from typing_extensions import TypeAlias
15+
16+
from .. import Distribution, PackagePath, _meta
17+
18+
if sys.version_info >= (3, 10):
19+
from importlib.metadata import PackageMetadata as _legacy_Metadata
20+
else:
21+
from email.message import Message as _legacy_Metadata
22+
23+
_PackageMetadataOrLegacy: TypeAlias = Union[_legacy_Metadata, _meta.PackageMetadata]
24+
_DistributionOrLegacy: TypeAlias = Union[_legacy.Distribution, Distribution]
25+
_List_PackagePathOrLegacy: TypeAlias = Union[
26+
List[_legacy.PackagePath], List[PackagePath]
27+
]

0 commit comments

Comments
 (0)