Skip to content

Commit f97f62e

Browse files
committed
Copy typechecker-internal symbols to _typeshed._tc
Cf. python#7580
1 parent bb1cbfa commit f97f62e

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

stdlib/_typeshed/_tc.pyi

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Internals used by some type checkers.
3+
4+
Don't use this module directly. It is only for type checkers to use.
5+
"""
6+
7+
import sys
8+
import typing_extensions
9+
from _collections_abc import dict_items, dict_keys, dict_values
10+
from abc import ABCMeta
11+
from collections.abc import Awaitable, Generator, Mapping
12+
from types import CellType, CodeType
13+
from typing import Any, ClassVar, Generic, TypeVar, overload
14+
from typing_extensions import Never, ParamSpec, TypeVarTuple, final
15+
16+
_T = TypeVar("_T")
17+
18+
# Used for an undocumented mypy feature. Does not exist at runtime.
19+
_promote = object()
20+
21+
# Internal mypy fallback type for all typed dicts.
22+
# N.B. Keep this mostly in sync with typing_extensions._TypedDict/mypy_extensions._TypedDict
23+
class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
24+
__total__: ClassVar[bool]
25+
__required_keys__: ClassVar[frozenset[str]]
26+
__optional_keys__: ClassVar[frozenset[str]]
27+
# __orig_bases__ sometimes exists on <3.12, but not consistently,
28+
# so we only add it to the stub on 3.12+
29+
if sys.version_info >= (3, 12):
30+
__orig_bases__: ClassVar[tuple[Any, ...]]
31+
if sys.version_info >= (3, 13):
32+
__readonly_keys__: ClassVar[frozenset[str]]
33+
__mutable_keys__: ClassVar[frozenset[str]]
34+
35+
def copy(self) -> typing_extensions.Self: ...
36+
# Using Never so that only calls using mypy plugin hook that specialize the signature
37+
# can go through.
38+
def setdefault(self, k: Never, default: object) -> object: ...
39+
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
40+
def pop(self, k: Never, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
41+
def update(self, m: typing_extensions.Self, /) -> None: ...
42+
def __delitem__(self, k: Never) -> None: ...
43+
def items(self) -> dict_items[str, object]: ...
44+
def keys(self) -> dict_keys[str, object]: ...
45+
def values(self) -> dict_values[str, object]: ...
46+
@overload
47+
def __or__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
48+
@overload
49+
def __or__(self, value: dict[str, Any], /) -> dict[str, object]: ...
50+
@overload
51+
def __ror__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
52+
@overload
53+
def __ror__(self, value: dict[str, Any], /) -> dict[str, object]: ...
54+
# supposedly incompatible definitions of __or__ and __ior__
55+
def __ior__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ... # type: ignore[misc]
56+
57+
# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
58+
_S = TypeVar("_S")
59+
_YieldT_co = TypeVar("_YieldT_co", covariant=True)
60+
_SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True)
61+
_ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True)
62+
63+
# The parameters correspond to Generator, but the 4th is the original type.
64+
class AwaitableGenerator(
65+
Awaitable[_ReturnT_nd_co],
66+
Generator[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co],
67+
Generic[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co, _S],
68+
metaclass=ABCMeta,
69+
): ...
70+
71+
@final
72+
class function:
73+
# Make sure this class definition stays roughly in line with `types.FunctionType`
74+
@property
75+
def __closure__(self) -> tuple[CellType, ...] | None: ...
76+
__code__: CodeType
77+
__defaults__: tuple[Any, ...] | None
78+
__dict__: dict[str, Any]
79+
@property
80+
def __globals__(self) -> dict[str, Any]: ...
81+
__name__: str
82+
__qualname__: str
83+
__annotations__: dict[str, Any]
84+
__kwdefaults__: dict[str, Any]
85+
if sys.version_info >= (3, 10):
86+
@property
87+
def __builtins__(self) -> dict[str, Any]: ...
88+
if sys.version_info >= (3, 12):
89+
__type_params__: tuple[TypeVar | ParamSpec | TypeVarTuple, ...]
90+
91+
__module__: str
92+
# mypy uses `builtins.function.__get__` to represent methods, properties, and getset_descriptors so we type the return as Any.
93+
def __get__(self, instance: object, owner: type | None = None, /) -> Any: ...

stdlib/builtins.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ class tuple(Sequence[_T_co]):
10041004
# Doesn't exist at runtime, but deleting this breaks mypy and pyright. See:
10051005
# https://github.com/python/typeshed/issues/7580
10061006
# https://github.com/python/mypy/issues/8240
1007+
# Obsolete, use _typecheck._tc.function instead.
10071008
@final
10081009
@type_check_only
10091010
class function:

stdlib/typing.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class TypeVar:
193193
def has_default(self) -> bool: ...
194194

195195
# Used for an undocumented mypy feature. Does not exist at runtime.
196+
# Obsolete, use _typeshed._tc._promote instead.
196197
_promote = object()
197198

198199
# N.B. Keep this definition in sync with typing_extensions._SpecialForm
@@ -531,6 +532,7 @@ class Coroutine(Awaitable[_ReturnT_nd_co], Generic[_YieldT_co, _SendT_nd_contra,
531532

532533
# NOTE: This type does not exist in typing.py or PEP 484 but mypy needs it to exist.
533534
# The parameters correspond to Generator, but the 4th is the original type.
535+
# Obsolete, use _typeshed._tc.AwaitableGenerator instead.
534536
@type_check_only
535537
class AwaitableGenerator(
536538
Awaitable[_ReturnT_nd_co],
@@ -925,6 +927,7 @@ class NamedTuple(tuple[Any, ...]):
925927

926928
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
927929
# N.B. Keep this mostly in sync with typing_extensions._TypedDict/mypy_extensions._TypedDict
930+
# Obsolete, use _typeshed._tc._TypedDict instead.
928931
@type_check_only
929932
class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
930933
__total__: ClassVar[bool]

0 commit comments

Comments
 (0)