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