Skip to content

Commit

Permalink
Extract localize functions into their own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 11, 2024
1 parent 430626f commit 058ef1b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
12 changes: 5 additions & 7 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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]:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
45 changes: 0 additions & 45 deletions importlib_metadata/_compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +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']
Expand Down Expand Up @@ -63,40 +55,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
45 changes: 45 additions & 0 deletions importlib_metadata/_compat/localize.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 058ef1b

Please sign in to comment.