importlib.metadata: Use the SimplePath protocol (#11445)

Closes #9217. Followup from #11436.

This mostly makes a simpler set of changes than #9217; in particular
it does not make PathDistribution generic. I think this is in line with
how the protocol is supposed to be used, though not always in line with
the runtime type annotations (which don't all make sense).

Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
This commit is contained in:
Jelle Zijlstra
2024-02-18 21:05:33 -08:00
committed by GitHub
parent 6df52a7b39
commit 84572bbfe6

View File

@@ -10,7 +10,7 @@ from os import PathLike
from pathlib import Path
from re import Pattern
from typing import Any, ClassVar, Generic, NamedTuple, TypeVar, overload
from typing_extensions import Self
from typing_extensions import Self, TypeAlias
_T = TypeVar("_T")
_KT = TypeVar("_KT")
@@ -33,9 +33,17 @@ if sys.version_info >= (3, 10):
__all__ += ["PackageMetadata", "packages_distributions"]
if sys.version_info >= (3, 10):
from importlib.metadata._meta import PackageMetadata as PackageMetadata
from importlib.metadata._meta import PackageMetadata as PackageMetadata, SimplePath
def packages_distributions() -> Mapping[str, list[str]]: ...
if sys.version_info >= (3, 12):
# It's generic but shouldn't be
_SimplePath: TypeAlias = SimplePath[Any]
else:
_SimplePath: TypeAlias = SimplePath
else:
_SimplePath: TypeAlias = Path
class PackageNotFoundError(ModuleNotFoundError):
@property
def name(self) -> str: ... # type: ignore[override]
@@ -184,7 +192,7 @@ class Distribution(_distribution_parent):
@abc.abstractmethod
def read_text(self, filename: str) -> str | None: ...
@abc.abstractmethod
def locate_file(self, path: StrPath) -> PathLike[str]: ...
def locate_file(self, path: StrPath) -> _SimplePath: ...
@classmethod
def from_name(cls, name: str) -> Distribution: ...
@overload
@@ -233,14 +241,14 @@ class MetadataPathFinder(DistributionFinder):
@classmethod
def find_distributions(cls, context: DistributionFinder.Context = ...) -> Iterable[PathDistribution]: ...
if sys.version_info >= (3, 10):
# Yes, this is an instance method that has argumend named "cls"
# Yes, this is an instance method that has a parameter named "cls"
def invalidate_caches(cls) -> None: ...
class PathDistribution(Distribution):
_path: Path
def __init__(self, path: Path) -> None: ...
def read_text(self, filename: StrPath) -> str: ...
def locate_file(self, path: StrPath) -> PathLike[str]: ...
_path: _SimplePath
def __init__(self, path: _SimplePath) -> None: ...
def read_text(self, filename: StrPath) -> str | None: ...
def locate_file(self, path: StrPath) -> _SimplePath: ...
def distribution(distribution_name: str) -> Distribution: ...
@overload