|
| 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: ... |
0 commit comments