Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5d25a7

Browse files
importlib.metadata: Improve and test SimplePath protocol (#11436)
Co-authored-by: layday <[email protected]>
1 parent e961db9 commit e5d25a7

6 files changed

Lines changed: 60 additions & 8 deletions

File tree

stdlib/importlib/metadata/_meta.pyi

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from collections.abc import Iterator
33
from typing import Any, Protocol, TypeVar, overload
44

55
_T = TypeVar("_T")
6+
_T_co = TypeVar("_T_co", covariant=True)
67

78
class PackageMetadata(Protocol):
89
def __len__(self) -> int: ...
@@ -22,19 +23,27 @@ class PackageMetadata(Protocol):
2223
def get(self, name: str, failobj: _T) -> _T | str: ...
2324

2425
if sys.version_info >= (3, 12):
25-
class SimplePath(Protocol[_T]):
26-
def joinpath(self) -> _T: ...
26+
class SimplePath(Protocol[_T_co]):
27+
# At runtime this is defined as taking `str | _T`, but that causes trouble.
28+
# See #11436.
29+
def joinpath(self, other: str, /) -> _T_co: ...
2730
@property
28-
def parent(self) -> _T: ...
31+
def parent(self) -> _T_co: ...
2932
def read_text(self) -> str: ...
30-
def __truediv__(self, other: _T | str) -> _T: ...
33+
# As with joinpath(), this is annotated as taking `str | _T` at runtime.
34+
def __truediv__(self, other: str, /) -> _T_co: ...
3135

3236
else:
3337
class SimplePath(Protocol):
34-
def joinpath(self) -> SimplePath: ...
35-
def parent(self) -> SimplePath: ...
38+
# Actually takes only self at runtime, but that's clearly wrong
39+
def joinpath(self, other: Any, /) -> SimplePath: ...
40+
# Not defined as a property at runtime, but it should be
41+
@property
42+
def parent(self) -> Any: ...
3643
def read_text(self) -> str: ...
3744
# There was a bug in `SimplePath` definition in cpython, see #8451
3845
# Strictly speaking `__div__` was defined in 3.10, not __truediv__,
3946
# but it should have always been `__truediv__`.
40-
def __truediv__(self) -> SimplePath: ...
47+
# Also, the runtime defines this method as taking no arguments,
48+
# which is obviously wrong.
49+
def __truediv__(self, other: Any, /) -> SimplePath: ...
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from _typeshed import StrPath
5+
from os import PathLike
6+
from pathlib import Path
7+
from typing import Any
8+
from zipfile import Path as ZipPath
9+
10+
if sys.version_info >= (3, 10):
11+
from importlib.metadata._meta import SimplePath
12+
13+
# Simplified version of zipfile.Path
14+
class MyPath:
15+
@property
16+
def parent(self) -> PathLike[str]: ... # undocumented
17+
18+
def read_text(self, encoding: str | None = ..., errors: str | None = ...) -> str: ...
19+
def joinpath(self, *other: StrPath) -> MyPath: ...
20+
def __truediv__(self, add: StrPath) -> MyPath: ...
21+
22+
if sys.version_info >= (3, 12):
23+
24+
def takes_simple_path(p: SimplePath[Any]) -> None: ...
25+
26+
else:
27+
28+
def takes_simple_path(p: SimplePath) -> None: ...
29+
30+
takes_simple_path(Path())
31+
takes_simple_path(ZipPath(""))
32+
takes_simple_path(MyPath())
33+
takes_simple_path("some string") # type: ignore

tests/regr_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def run_testcases(
180180
# Avoid race conditions when reading the cache
181181
# (https://github.com/python/typeshed/issues/11220)
182182
"--no-incremental",
183+
# Not useful for the test cases
184+
"--disable-error-code=empty-body",
183185
]
184186

185187
if package.is_stdlib:

tests/stubtest_allowlists/py310.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ gettext.install
1313
gettext.translation
1414
importlib._abc.Loader.exec_module # See Lib/importlib/_abc.py. Might be defined for backwards compatibility
1515
importlib.abc.Finder.find_module
16-
importlib.metadata._meta.SimplePath.__truediv__ # See comments in the stub
1716
# platform.uname_result's processor field is now dynamically made to exist
1817
platform.uname_result.__match_args__
1918
platform.uname_result.__new__
@@ -26,6 +25,10 @@ typing._SpecialForm.__mro_entries__
2625
weakref.ProxyType.__reversed__ # Doesn't really exist
2726
builtins.ellipsis # type is not exposed anywhere
2827

28+
# Runtime definition of protocol is incorrect
29+
importlib.metadata._meta.SimplePath.__truediv__
30+
importlib.metadata._meta.SimplePath.joinpath
31+
2932
# Modules that exist at runtime, but shouldn't be added to typeshed
3033
ctypes.test
3134
ctypes\.test\..+

tests/stubtest_allowlists/py311.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ tkinter._VersionInfoType.__doc__
1818
typing.NewType.__mro_entries__
1919
builtins.ellipsis # type is not exposed anywhere
2020

21+
# Runtime definition of protocol is incorrect
22+
importlib.metadata._meta.SimplePath.__truediv__
23+
importlib.metadata._meta.SimplePath.joinpath
24+
2125
# Modules that exist at runtime, but shouldn't be added to typeshed
2226
ctypes.test
2327
ctypes\.test\..+

tests/stubtest_allowlists/py312.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ types.GenericAlias.__getattr__
7575
types.GenericAlias.__mro_entries__
7676
sys._monitoring # Doesn't really exist. See comments in the stub.
7777
weakref.ProxyType.__reversed__ # Doesn't really exist
78+
importlib.metadata._meta.SimplePath.joinpath # Incorrect runtime definition
7879

7980
# sys attributes that are not always defined
8081
sys.last_exc

0 commit comments

Comments
 (0)