diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 0933d433..9a673358 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -33,9 +33,7 @@ from ._compat import ( NullFinder, install, - localize_dist, - localize_metadata, - localize_package_path, + localize, ) from ._functools import apply, compose, method_cache, pass_none from ._itertools import always_iterable, bucket, unique_everseen @@ -412,7 +410,7 @@ def locate_file(self, path: str | os.PathLike[str]) -> SimplePath: """ @classmethod - @apply(localize_dist) + @apply(localize.dist) def from_name(cls, name: str) -> Distribution: """Return the Distribution for the given package name. @@ -431,7 +429,7 @@ def from_name(cls, name: str) -> Distribution: raise PackageNotFoundError(name) @classmethod - @apply(functools.partial(map, localize_dist)) + @apply(functools.partial(map, localize.dist)) def discover( cls, *, context: Optional[DistributionFinder.Context] = None, **kwargs ) -> Iterable[Distribution]: @@ -479,7 +477,7 @@ def _discover_resolvers(): return filter(None, declared) @property - @apply(localize_metadata) + @apply(localize.metadata) def metadata(self) -> _meta.PackageMetadata: """Return the parsed metadata for this Distribution. @@ -530,7 +528,7 @@ def entry_points(self) -> EntryPoints: return EntryPoints._from_text_for(self.read_text('entry_points.txt'), self) @property - @apply(pass_none(compose(list, functools.partial(map, localize_package_path)))) + @apply(pass_none(compose(list, functools.partial(map, localize.package_path)))) def files(self) -> Optional[List[PackagePath]]: """Files in this distribution. diff --git a/importlib_metadata/_compat/__init__.py b/importlib_metadata/_compat/__init__.py index 04311fb9..01356d69 100644 --- a/importlib_metadata/_compat/__init__.py +++ b/importlib_metadata/_compat/__init__.py @@ -1,14 +1,5 @@ -from __future__ import annotations - -import email.message -import importlib.metadata import platform import sys -import warnings -from typing import cast - -import importlib_metadata._adapters - __all__ = ['install', 'NullFinder'] @@ -63,40 +54,3 @@ def pypy_partial(val): """ is_pypy = platform.python_implementation() == 'PyPy' return val + is_pypy - - -def localize_dist( - dist: importlib_metadata.Distribution | importlib.metadata.Distribution, -) -> importlib_metadata.Distribution: - """ - Ensure dist is an :class:`importlib_metadata.Distribution`. - """ - if isinstance(dist, importlib_metadata.Distribution): - return dist - if isinstance(dist, importlib.metadata.PathDistribution): - return importlib_metadata.PathDistribution( - cast(importlib_metadata._meta.SimplePath, dist._path) - ) - # workaround for when pytest has replaced importlib_metadata - # https://github.com/python/importlib_metadata/pull/505#issuecomment-2344329001 - if dist.__class__.__module__ != 'importlib_metadata': - warnings.warn(f"Unrecognized distribution subclass {dist.__class__}") - return cast(importlib_metadata.Distribution, dist) - - -def localize_metadata( - input: importlib_metadata._adapters.Message | email.message.Message, -) -> importlib_metadata._adapters.Message: - if isinstance(input, importlib_metadata._adapters.Message): - return input - return importlib_metadata._adapters.Message(input) - - -def localize_package_path( - input: importlib_metadata.PackagePath | importlib.metadata.PackagePath, -) -> importlib_metadata.PackagePath: - if isinstance(input, importlib_metadata.PackagePath): - return input - replacement = importlib_metadata.PackagePath(input) - vars(replacement).update(vars(input)) - return replacement diff --git a/importlib_metadata/_compat/localize.py b/importlib_metadata/_compat/localize.py new file mode 100644 index 00000000..9062ff01 --- /dev/null +++ b/importlib_metadata/_compat/localize.py @@ -0,0 +1,45 @@ +from __future__ import annotations + +import email.message +import importlib.metadata +import warnings +from typing import cast + +import importlib_metadata._adapters + + +def dist( + dist: importlib_metadata.Distribution | importlib.metadata.Distribution, +) -> importlib_metadata.Distribution: + """ + Ensure dist is an :class:`importlib_metadata.Distribution`. + """ + if isinstance(dist, importlib_metadata.Distribution): + return dist + if isinstance(dist, importlib.metadata.PathDistribution): + return importlib_metadata.PathDistribution( + cast(importlib_metadata._meta.SimplePath, dist._path) + ) + # workaround for when pytest has replaced importlib_metadata + # https://github.com/python/importlib_metadata/pull/505#issuecomment-2344329001 + if dist.__class__.__module__ != 'importlib_metadata': + warnings.warn(f"Unrecognized distribution subclass {dist.__class__}") + return cast(importlib_metadata.Distribution, dist) + + +def metadata( + input: importlib_metadata._adapters.Message | email.message.Message, +) -> importlib_metadata._adapters.Message: + if isinstance(input, importlib_metadata._adapters.Message): + return input + return importlib_metadata._adapters.Message(input) + + +def package_path( + input: importlib_metadata.PackagePath | importlib.metadata.PackagePath, +) -> importlib_metadata.PackagePath: + if isinstance(input, importlib_metadata.PackagePath): + return input + replacement = importlib_metadata.PackagePath(input) + vars(replacement).update(vars(input)) + return replacement