Skip to content

Commit 1079875

Browse files
srittaumingyu.park
authored andcommitted
Copy typechecker-internal symbols to _typeshed._type_checker_internals (python#13816)
1 parent c1ef517 commit 1079875

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Internals used by some type checkers.
2+
#
3+
# Don't use this module directly. It is only for type checkers to use.
4+
5+
import sys
6+
import typing_extensions
7+
from _collections_abc import dict_items, dict_keys, dict_values
8+
from abc import ABCMeta
9+
from collections.abc import Awaitable, Generator, Iterable, Mapping
10+
from typing import Any, ClassVar, Generic, TypeVar, overload
11+
from typing_extensions import Never
12+
13+
_T = TypeVar("_T")
14+
15+
# Used for an undocumented mypy feature. Does not exist at runtime.
16+
promote = object()
17+
18+
# Fallback type providing methods and attributes that appear on all `TypedDict` types.
19+
# N.B. Keep this mostly in sync with typing_extensions._TypedDict/mypy_extensions._TypedDict
20+
class TypedDictFallback(Mapping[str, object], metaclass=ABCMeta):
21+
__total__: ClassVar[bool]
22+
__required_keys__: ClassVar[frozenset[str]]
23+
__optional_keys__: ClassVar[frozenset[str]]
24+
# __orig_bases__ sometimes exists on <3.12, but not consistently,
25+
# so we only add it to the stub on 3.12+
26+
if sys.version_info >= (3, 12):
27+
__orig_bases__: ClassVar[tuple[Any, ...]]
28+
if sys.version_info >= (3, 13):
29+
__readonly_keys__: ClassVar[frozenset[str]]
30+
__mutable_keys__: ClassVar[frozenset[str]]
31+
32+
def copy(self) -> typing_extensions.Self: ...
33+
# Using Never so that only calls using mypy plugin hook that specialize the signature
34+
# can go through.
35+
def setdefault(self, k: Never, default: object) -> object: ...
36+
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
37+
def pop(self, k: Never, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
38+
def update(self, m: typing_extensions.Self, /) -> None: ...
39+
def __delitem__(self, k: Never) -> None: ...
40+
def items(self) -> dict_items[str, object]: ...
41+
def keys(self) -> dict_keys[str, object]: ...
42+
def values(self) -> dict_values[str, object]: ...
43+
@overload
44+
def __or__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
45+
@overload
46+
def __or__(self, value: dict[str, Any], /) -> dict[str, object]: ...
47+
@overload
48+
def __ror__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
49+
@overload
50+
def __ror__(self, value: dict[str, Any], /) -> dict[str, object]: ...
51+
# supposedly incompatible definitions of __or__ and __ior__
52+
def __ior__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ... # type: ignore[misc]
53+
54+
# Fallback type providing methods and attributes that appear on all `NamedTuple` types.
55+
class NamedTupleFallback(tuple[Any, ...]):
56+
_field_defaults: ClassVar[dict[str, Any]]
57+
_fields: ClassVar[tuple[str, ...]]
58+
# __orig_bases__ sometimes exists on <3.12, but not consistently
59+
# So we only add it to the stub on 3.12+.
60+
if sys.version_info >= (3, 12):
61+
__orig_bases__: ClassVar[tuple[Any, ...]]
62+
63+
@overload
64+
def __init__(self, typename: str, fields: Iterable[tuple[str, Any]], /) -> None: ...
65+
@overload
66+
@typing_extensions.deprecated(
67+
"Creating a typing.NamedTuple using keyword arguments is deprecated and support will be removed in Python 3.15"
68+
)
69+
def __init__(self, typename: str, fields: None = None, /, **kwargs: Any) -> None: ...
70+
@classmethod
71+
def _make(cls, iterable: Iterable[Any]) -> typing_extensions.Self: ...
72+
def _asdict(self) -> dict[str, Any]: ...
73+
def _replace(self, **kwargs: Any) -> typing_extensions.Self: ...
74+
if sys.version_info >= (3, 13):
75+
def __replace__(self, **kwargs: Any) -> typing_extensions.Self: ...
76+
77+
# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
78+
_S = TypeVar("_S")
79+
_YieldT_co = TypeVar("_YieldT_co", covariant=True)
80+
_SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True)
81+
_ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True)
82+
83+
# The parameters correspond to Generator, but the 4th is the original type.
84+
class AwaitableGenerator(
85+
Awaitable[_ReturnT_nd_co],
86+
Generator[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co],
87+
Generic[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co, _S],
88+
metaclass=ABCMeta,
89+
): ...

‎stdlib/builtins.pyi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ class tuple(Sequence[_T_co]):
10111011
# Doesn't exist at runtime, but deleting this breaks mypy and pyright. See:
10121012
# https://github.com/python/typeshed/issues/7580
10131013
# https://github.com/python/mypy/issues/8240
1014+
# Obsolete, use types.FunctionType instead.
10141015
@final
10151016
@type_check_only
10161017
class function:

‎stdlib/typing.pyi‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class TypeVar:
220220
def evaluate_default(self) -> EvaluateFunc | None: ...
221221

222222
# Used for an undocumented mypy feature. Does not exist at runtime.
223+
# Obsolete, use _typeshed._type_checker_internals.promote instead.
223224
_promote = object()
224225

225226
# N.B. Keep this definition in sync with typing_extensions._SpecialForm
@@ -564,6 +565,7 @@ class Coroutine(Awaitable[_ReturnT_nd_co], Generic[_YieldT_co, _SendT_nd_contra,
564565

565566
# NOTE: This type does not exist in typing.py or PEP 484 but mypy needs it to exist.
566567
# The parameters correspond to Generator, but the 4th is the original type.
568+
# Obsolete, use _typeshed._type_checker_internals.AwaitableGenerator instead.
567569
@type_check_only
568570
class AwaitableGenerator(
569571
Awaitable[_ReturnT_nd_co],
@@ -946,6 +948,7 @@ if sys.version_info >= (3, 11):
946948

947949
# Type constructors
948950

951+
# Obsolete, will be changed to a function. Use _typeshed._type_checker_internals.NamedTupleFallback instead.
949952
class NamedTuple(tuple[Any, ...]):
950953
_field_defaults: ClassVar[dict[str, Any]]
951954
_fields: ClassVar[tuple[str, ...]]
@@ -970,6 +973,7 @@ class NamedTuple(tuple[Any, ...]):
970973

971974
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
972975
# N.B. Keep this mostly in sync with typing_extensions._TypedDict/mypy_extensions._TypedDict
976+
# Obsolete, use _typeshed._type_checker_internals.TypedDictFallback instead.
973977
@type_check_only
974978
class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
975979
__total__: ClassVar[bool]

0 commit comments

Comments
 (0)