|
| 1 | +# Utility types for typeshed |
| 2 | +# |
| 3 | +# See the README.md file in this directory for more information. |
| 4 | + |
| 5 | +import array |
| 6 | +import sys |
| 7 | +from os import PathLike |
| 8 | +from typing import AbstractSet, Any, Container, Iterable, Protocol, Tuple, TypeVar, Union |
| 9 | +from typing_extensions import Literal, final |
| 10 | + |
| 11 | +_KT = TypeVar("_KT") |
| 12 | +_KT_co = TypeVar("_KT_co", covariant=True) |
| 13 | +_KT_contra = TypeVar("_KT_contra", contravariant=True) |
| 14 | +_VT = TypeVar("_VT") |
| 15 | +_VT_co = TypeVar("_VT_co", covariant=True) |
| 16 | +_T = TypeVar("_T") |
| 17 | +_T_co = TypeVar("_T_co", covariant=True) |
| 18 | +_T_contra = TypeVar("_T_contra", contravariant=True) |
| 19 | + |
| 20 | +# Use for "self" annotations: |
| 21 | +# def __enter__(self: Self) -> Self: ... |
| 22 | +Self = TypeVar("Self") # noqa Y001 |
| 23 | + |
| 24 | +# stable |
| 25 | +class IdentityFunction(Protocol): |
| 26 | + def __call__(self, __x: _T) -> _T: ... |
| 27 | + |
| 28 | +class SupportsLessThan(Protocol): |
| 29 | + def __lt__(self, __other: Any) -> bool: ... |
| 30 | + |
| 31 | +SupportsLessThanT = TypeVar("SupportsLessThanT", bound=SupportsLessThan) # noqa: Y001 |
| 32 | + |
| 33 | +class SupportsDivMod(Protocol[_T_contra, _T_co]): |
| 34 | + def __divmod__(self, __other: _T_contra) -> _T_co: ... |
| 35 | + |
| 36 | +class SupportsRDivMod(Protocol[_T_contra, _T_co]): |
| 37 | + def __rdivmod__(self, __other: _T_contra) -> _T_co: ... |
| 38 | + |
| 39 | +class SupportsLenAndGetItem(Protocol[_T_co]): |
| 40 | + def __len__(self) -> int: ... |
| 41 | + def __getitem__(self, __k: int) -> _T_co: ... |
| 42 | + |
| 43 | +# Mapping-like protocols |
| 44 | + |
| 45 | +# stable |
| 46 | +class SupportsItems(Protocol[_KT_co, _VT_co]): |
| 47 | + def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: ... |
| 48 | + |
| 49 | +# stable |
| 50 | +class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]): |
| 51 | + def keys(self) -> Iterable[_KT]: ... |
| 52 | + def __getitem__(self, __k: _KT) -> _VT_co: ... |
| 53 | + |
| 54 | +# stable |
| 55 | +class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): |
| 56 | + def __getitem__(self, __k: _KT_contra) -> _VT_co: ... |
| 57 | + |
| 58 | +# stable |
| 59 | +class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, _VT]): |
| 60 | + def __setitem__(self, __k: _KT_contra, __v: _VT) -> None: ... |
| 61 | + def __delitem__(self, __v: _KT_contra) -> None: ... |
| 62 | + |
| 63 | +# These aliases are simple strings in Python 2. |
| 64 | +StrPath = Union[str, PathLike[str]] # stable |
| 65 | +BytesPath = Union[bytes, PathLike[bytes]] # stable |
| 66 | +StrOrBytesPath = Union[str, bytes, PathLike[str], PathLike[bytes]] # stable |
| 67 | + |
| 68 | +OpenTextModeUpdating = Literal[ |
| 69 | + "r+", |
| 70 | + "+r", |
| 71 | + "rt+", |
| 72 | + "r+t", |
| 73 | + "+rt", |
| 74 | + "tr+", |
| 75 | + "t+r", |
| 76 | + "+tr", |
| 77 | + "w+", |
| 78 | + "+w", |
| 79 | + "wt+", |
| 80 | + "w+t", |
| 81 | + "+wt", |
| 82 | + "tw+", |
| 83 | + "t+w", |
| 84 | + "+tw", |
| 85 | + "a+", |
| 86 | + "+a", |
| 87 | + "at+", |
| 88 | + "a+t", |
| 89 | + "+at", |
| 90 | + "ta+", |
| 91 | + "t+a", |
| 92 | + "+ta", |
| 93 | + "x+", |
| 94 | + "+x", |
| 95 | + "xt+", |
| 96 | + "x+t", |
| 97 | + "+xt", |
| 98 | + "tx+", |
| 99 | + "t+x", |
| 100 | + "+tx", |
| 101 | +] |
| 102 | +OpenTextModeWriting = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"] |
| 103 | +OpenTextModeReading = Literal["r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"] |
| 104 | +OpenTextMode = Union[OpenTextModeUpdating, OpenTextModeWriting, OpenTextModeReading] |
| 105 | +OpenBinaryModeUpdating = Literal[ |
| 106 | + "rb+", |
| 107 | + "r+b", |
| 108 | + "+rb", |
| 109 | + "br+", |
| 110 | + "b+r", |
| 111 | + "+br", |
| 112 | + "wb+", |
| 113 | + "w+b", |
| 114 | + "+wb", |
| 115 | + "bw+", |
| 116 | + "b+w", |
| 117 | + "+bw", |
| 118 | + "ab+", |
| 119 | + "a+b", |
| 120 | + "+ab", |
| 121 | + "ba+", |
| 122 | + "b+a", |
| 123 | + "+ba", |
| 124 | + "xb+", |
| 125 | + "x+b", |
| 126 | + "+xb", |
| 127 | + "bx+", |
| 128 | + "b+x", |
| 129 | + "+bx", |
| 130 | +] |
| 131 | +OpenBinaryModeWriting = Literal["wb", "bw", "ab", "ba", "xb", "bx"] |
| 132 | +OpenBinaryModeReading = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"] |
| 133 | +OpenBinaryMode = Union[OpenBinaryModeUpdating, OpenBinaryModeReading, OpenBinaryModeWriting] |
| 134 | + |
| 135 | +# stable |
| 136 | +class HasFileno(Protocol): |
| 137 | + def fileno(self) -> int: ... |
| 138 | + |
| 139 | +FileDescriptor = int # stable |
| 140 | +FileDescriptorLike = Union[int, HasFileno] # stable |
| 141 | + |
| 142 | +# stable |
| 143 | +class SupportsRead(Protocol[_T_co]): |
| 144 | + def read(self, __length: int = ...) -> _T_co: ... |
| 145 | + |
| 146 | +# stable |
| 147 | +class SupportsReadline(Protocol[_T_co]): |
| 148 | + def readline(self, __length: int = ...) -> _T_co: ... |
| 149 | + |
| 150 | +# stable |
| 151 | +class SupportsNoArgReadline(Protocol[_T_co]): |
| 152 | + def readline(self) -> _T_co: ... |
| 153 | + |
| 154 | +# stable |
| 155 | +class SupportsWrite(Protocol[_T_contra]): |
| 156 | + def write(self, __s: _T_contra) -> Any: ... |
| 157 | + |
| 158 | +ReadableBuffer = Union[bytes, bytearray, memoryview, array.array[Any]] # stable |
| 159 | +WriteableBuffer = Union[bytearray, memoryview, array.array[Any]] # stable |
| 160 | + |
| 161 | +# stable |
| 162 | +if sys.version_info >= (3, 10): |
| 163 | + from types import NoneType as NoneType |
| 164 | +else: |
| 165 | + # Used by type checkers for checks involving None (does not exist at runtime) |
| 166 | + @final |
| 167 | + class NoneType: |
| 168 | + def __bool__(self) -> Literal[False]: ... |
0 commit comments