1 # Utility types for typeshed
3 # See the README.md file in this directory for more information.
9 from os import PathLike
10 from typing import AbstractSet, Any, Awaitable, Container, Iterable, Protocol, TypeVar, Union
11 from typing_extensions import Literal, final
14 _KT_co = TypeVar("_KT_co", covariant=True)
15 _KT_contra = TypeVar("_KT_contra", contravariant=True)
17 _VT_co = TypeVar("_VT_co", covariant=True)
19 _T_co = TypeVar("_T_co", covariant=True)
20 _T_contra = TypeVar("_T_contra", contravariant=True)
22 # Use for "self" annotations:
23 # def __enter__(self: Self) -> Self: ...
24 Self = TypeVar("Self") # noqa Y001
27 class IdentityFunction(Protocol):
28 def __call__(self, __x: _T) -> _T: ...
31 class SupportsNext(Protocol[_T_co]):
32 def __next__(self) -> _T_co: ...
35 class SupportsAnext(Protocol[_T_co]):
36 def __anext__(self) -> Awaitable[_T_co]: ...
38 class SupportsLessThan(Protocol):
39 def __lt__(self, __other: Any) -> bool: ...
41 SupportsLessThanT = TypeVar("SupportsLessThanT", bound=SupportsLessThan) # noqa: Y001
43 class SupportsGreaterThan(Protocol):
44 def __gt__(self, __other: Any) -> bool: ...
46 SupportsGreaterThanT = TypeVar("SupportsGreaterThanT", bound=SupportsGreaterThan) # noqa: Y001
48 class SupportsDivMod(Protocol[_T_contra, _T_co]):
49 def __divmod__(self, __other: _T_contra) -> _T_co: ...
51 class SupportsRDivMod(Protocol[_T_contra, _T_co]):
52 def __rdivmod__(self, __other: _T_contra) -> _T_co: ...
54 class SupportsLenAndGetItem(Protocol[_T_co]):
55 def __len__(self) -> int: ...
56 def __getitem__(self, __k: int) -> _T_co: ...
58 class SupportsTrunc(Protocol):
59 def __trunc__(self) -> int: ...
61 # Mapping-like protocols
64 class SupportsItems(Protocol[_KT_co, _VT_co]):
65 def items(self) -> AbstractSet[tuple[_KT_co, _VT_co]]: ...
68 class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
69 def keys(self) -> Iterable[_KT]: ...
70 def __getitem__(self, __k: _KT) -> _VT_co: ...
73 class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]):
74 def __getitem__(self, __k: _KT_contra) -> _VT_co: ...
77 class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, _VT]):
78 def __setitem__(self, __k: _KT_contra, __v: _VT) -> None: ...
79 def __delitem__(self, __v: _KT_contra) -> None: ...
81 # These aliases are simple strings in Python 2.
82 StrPath = Union[str, PathLike[str]] # stable
83 BytesPath = Union[bytes, PathLike[bytes]] # stable
84 StrOrBytesPath = Union[str, bytes, PathLike[str], PathLike[bytes]] # stable
86 OpenTextModeUpdating = Literal[
120 OpenTextModeWriting = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"]
121 OpenTextModeReading = Literal["r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"]
122 OpenTextMode = Union[OpenTextModeUpdating, OpenTextModeWriting, OpenTextModeReading]
123 OpenBinaryModeUpdating = Literal[
149 OpenBinaryModeWriting = Literal["wb", "bw", "ab", "ba", "xb", "bx"]
150 OpenBinaryModeReading = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"]
151 OpenBinaryMode = Union[OpenBinaryModeUpdating, OpenBinaryModeReading, OpenBinaryModeWriting]
154 class HasFileno(Protocol):
155 def fileno(self) -> int: ...
157 FileDescriptor = int # stable
158 FileDescriptorLike = Union[int, HasFileno] # stable
161 class SupportsRead(Protocol[_T_co]):
162 def read(self, __length: int = ...) -> _T_co: ...
165 class SupportsReadline(Protocol[_T_co]):
166 def readline(self, __length: int = ...) -> _T_co: ...
169 class SupportsNoArgReadline(Protocol[_T_co]):
170 def readline(self) -> _T_co: ...
173 class SupportsWrite(Protocol[_T_contra]):
174 def write(self, __s: _T_contra) -> Any: ...
176 ReadOnlyBuffer = bytes # stable
177 # Anything that implements the read-write buffer interface.
178 # The buffer interface is defined purely on the C level, so we cannot define a normal Protocol
179 # for it. Instead we have to list the most common stdlib buffer classes in a Union.
180 WriteableBuffer = Union[bytearray, memoryview, array.array[Any], mmap.mmap, ctypes._CData] # stable
181 # Same as _WriteableBuffer, but also includes read-only buffer types (like bytes).
182 ReadableBuffer = Union[ReadOnlyBuffer, WriteableBuffer] # stable
185 if sys.version_info >= (3, 10):
186 from types import NoneType as NoneType
188 # Used by type checkers for checks involving None (does not exist at runtime)
191 def __bool__(self) -> Literal[False]: ...