| 1 |
# Test stub for typing module, with features for async/await related tests. |
| 2 |
# |
| 3 |
# Use [typing fixtures/typing-async.pyi] to use this instead of lib-stub/typing.pyi |
| 4 |
# in a particular test case. |
| 5 |
# |
| 6 |
# Many of the definitions have special handling in the type checker, so they |
| 7 |
# can just be initialized to anything. |
| 8 |
|
| 9 |
from abc import abstractmethod, ABCMeta |
| 10 |
|
| 11 |
cast = 0 |
| 12 |
overload = 0 |
| 13 |
Any = object() |
| 14 |
Union = 0 |
| 15 |
Optional = 0 |
| 16 |
TypeVar = 0 |
| 17 |
Generic = 0 |
| 18 |
Protocol = 0 |
| 19 |
Tuple = 0 |
| 20 |
Callable = 0 |
| 21 |
NamedTuple = 0 |
| 22 |
Type = 0 |
| 23 |
ClassVar = 0 |
| 24 |
Final = 0 |
| 25 |
Literal = 0 |
| 26 |
NoReturn = 0 |
| 27 |
Self = 0 |
| 28 |
|
| 29 |
T = TypeVar('T') |
| 30 |
T_co = TypeVar('T_co', covariant=True) |
| 31 |
R_co = TypeVar('R_co', covariant=True) |
| 32 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 33 |
S_contra = TypeVar('S_contra', contravariant=True) |
| 34 |
U = TypeVar('U') |
| 35 |
V = TypeVar('V') |
| 36 |
S = TypeVar('S') |
| 37 |
|
| 38 |
# Note: definitions below are different from typeshed, variances are declared |
| 39 |
# to silence the protocol variance checks. Maybe it is better to use type: ignore? |
| 40 |
|
| 41 |
class Container(Protocol[T_co]): |
| 42 |
@abstractmethod |
| 43 |
# Use int because bool isn't in the default test builtins |
| 44 |
def __contains__(self, arg: object) -> int: pass |
| 45 |
|
| 46 |
class Iterable(Protocol[T_co]): |
| 47 |
@abstractmethod |
| 48 |
def __iter__(self) -> 'Iterator[T_co]': pass |
| 49 |
|
| 50 |
class Iterator(Iterable[T_co], Protocol): |
| 51 |
@abstractmethod |
| 52 |
def __next__(self) -> T_co: pass |
| 53 |
|
| 54 |
class Generator(Iterator[T_co], Generic[T_co, S_contra, R_co]): |
| 55 |
@abstractmethod |
| 56 |
def send(self, value: S_contra) -> T_co: pass |
| 57 |
|
| 58 |
@abstractmethod |
| 59 |
def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass |
| 60 |
|
| 61 |
@abstractmethod |
| 62 |
def close(self) -> None: pass |
| 63 |
|
| 64 |
@abstractmethod |
| 65 |
def __iter__(self) -> 'Generator[T_co, S_contra, R_co]': pass |
| 66 |
|
| 67 |
class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, S_contra]): |
| 68 |
@abstractmethod |
| 69 |
def __anext__(self) -> Awaitable[T_co]: pass |
| 70 |
|
| 71 |
@abstractmethod |
| 72 |
def asend(self, value: S_contra) -> Awaitable[T_co]: pass |
| 73 |
|
| 74 |
@abstractmethod |
| 75 |
def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T_co]: pass |
| 76 |
|
| 77 |
@abstractmethod |
| 78 |
def aclose(self) -> Awaitable[T_co]: pass |
| 79 |
|
| 80 |
@abstractmethod |
| 81 |
def __aiter__(self) -> 'AsyncGenerator[T_co, S_contra]': pass |
| 82 |
|
| 83 |
class Awaitable(Protocol[T_co]): |
| 84 |
@abstractmethod |
| 85 |
def __await__(self) -> Generator[Any, Any, T_co]: pass |
| 86 |
|
| 87 |
class AwaitableGenerator( |
| 88 |
Awaitable[R_co], |
| 89 |
Generator[T_co, S_contra, R_co], |
| 90 |
Generic[T_co, S_contra, R_co, S], |
| 91 |
metaclass=ABCMeta |
| 92 |
): |
| 93 |
pass |
| 94 |
|
| 95 |
class Coroutine(Awaitable[R_co], Generic[T_co, S_contra, R_co]): |
| 96 |
@abstractmethod |
| 97 |
def send(self, value: S_contra) -> T_co: pass |
| 98 |
|
| 99 |
@abstractmethod |
| 100 |
def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass |
| 101 |
|
| 102 |
@abstractmethod |
| 103 |
def close(self) -> None: pass |
| 104 |
|
| 105 |
class AsyncIterable(Protocol[T_co]): |
| 106 |
@abstractmethod |
| 107 |
def __aiter__(self) -> 'AsyncIterator[T_co]': pass |
| 108 |
|
| 109 |
class AsyncIterator(AsyncIterable[T_co], Protocol): |
| 110 |
def __aiter__(self) -> 'AsyncIterator[T_co]': return self |
| 111 |
@abstractmethod |
| 112 |
def __anext__(self) -> Awaitable[T_co]: pass |
| 113 |
|
| 114 |
class Sequence(Iterable[T_co], Container[T_co]): |
| 115 |
@abstractmethod |
| 116 |
def __getitem__(self, n: Any) -> T_co: pass |
| 117 |
|
| 118 |
class Mapping(Iterable[T], Generic[T, T_co], metaclass=ABCMeta): |
| 119 |
def keys(self) -> Iterable[T]: pass # Approximate return type |
| 120 |
def __getitem__(self, key: T) -> T_co: pass |
| 121 |
@overload |
| 122 |
def get(self, key: T, /) -> Optional[T_co]: pass |
| 123 |
@overload |
| 124 |
def get(self, key: T, default: T_co, /) -> T_co: pass # type: ignore[misc] |
| 125 |
@overload |
| 126 |
def get(self, key: T, default: V, /) -> Union[T_co, V]: pass |
| 127 |
|
| 128 |
class ContextManager(Generic[T_co]): |
| 129 |
def __enter__(self) -> T_co: pass |
| 130 |
# Use Any because not all the precise types are in the fixtures. |
| 131 |
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass |
| 132 |
|
| 133 |
class AsyncContextManager(Generic[T_co]): |
| 134 |
def __aenter__(self) -> Awaitable[T_co]: pass |
| 135 |
# Use Any because not all the precise types are in the fixtures. |
| 136 |
def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Awaitable[Any]: pass |
| 137 |
|
| 138 |
class _SpecialForm: pass |