importlib.metadata: Improve and test SimplePath protocol (#11436)

Co-authored-by: layday <layday@protonmail.com>
This commit is contained in:
Jelle Zijlstra
2024-02-18 00:36:01 -08:00
committed by GitHub
parent e961db9492
commit e5d25a7605
6 changed files with 60 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ from collections.abc import Iterator
from typing import Any, Protocol, TypeVar, overload
_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
class PackageMetadata(Protocol):
def __len__(self) -> int: ...
@@ -22,19 +23,27 @@ class PackageMetadata(Protocol):
def get(self, name: str, failobj: _T) -> _T | str: ...
if sys.version_info >= (3, 12):
class SimplePath(Protocol[_T]):
def joinpath(self) -> _T: ...
class SimplePath(Protocol[_T_co]):
# At runtime this is defined as taking `str | _T`, but that causes trouble.
# See #11436.
def joinpath(self, other: str, /) -> _T_co: ...
@property
def parent(self) -> _T: ...
def parent(self) -> _T_co: ...
def read_text(self) -> str: ...
def __truediv__(self, other: _T | str) -> _T: ...
# As with joinpath(), this is annotated as taking `str | _T` at runtime.
def __truediv__(self, other: str, /) -> _T_co: ...
else:
class SimplePath(Protocol):
def joinpath(self) -> SimplePath: ...
def parent(self) -> SimplePath: ...
# Actually takes only self at runtime, but that's clearly wrong
def joinpath(self, other: Any, /) -> SimplePath: ...
# Not defined as a property at runtime, but it should be
@property
def parent(self) -> Any: ...
def read_text(self) -> str: ...
# There was a bug in `SimplePath` definition in cpython, see #8451
# Strictly speaking `__div__` was defined in 3.10, not __truediv__,
# but it should have always been `__truediv__`.
def __truediv__(self) -> SimplePath: ...
# Also, the runtime defines this method as taking no arguments,
# which is obviously wrong.
def __truediv__(self, other: Any, /) -> SimplePath: ...