typing-full.pyi 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # More complete stub for typing module.
  2. #
  3. # Use [typing fixtures/typing-full.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. from abc import abstractmethod, ABCMeta
  9. class GenericMeta(type): pass
  10. cast = 0
  11. overload = 0
  12. Any = 0
  13. Union = 0
  14. Optional = 0
  15. TypeVar = 0
  16. Generic = 0
  17. Protocol = 0
  18. Tuple = 0
  19. Callable = 0
  20. _promote = 0
  21. NamedTuple = 0
  22. Type = 0
  23. no_type_check = 0
  24. ClassVar = 0
  25. Final = 0
  26. Literal = 0
  27. TypedDict = 0
  28. NoReturn = 0
  29. NewType = 0
  30. T = TypeVar('T')
  31. T_co = TypeVar('T_co', covariant=True)
  32. T_contra = TypeVar('T_contra', contravariant=True)
  33. U = TypeVar('U')
  34. V = TypeVar('V')
  35. S = TypeVar('S')
  36. # Note: definitions below are different from typeshed, variances are declared
  37. # to silence the protocol variance checks. Maybe it is better to use type: ignore?
  38. @runtime_checkable
  39. class Container(Protocol[T_co]):
  40. @abstractmethod
  41. # Use int because bool isn't in the default test builtins
  42. def __contains__(self, arg: object) -> int: pass
  43. @runtime_checkable
  44. class Sized(Protocol):
  45. @abstractmethod
  46. def __len__(self) -> int: pass
  47. @runtime_checkable
  48. class Iterable(Protocol[T_co]):
  49. @abstractmethod
  50. def __iter__(self) -> 'Iterator[T_co]': pass
  51. @runtime_checkable
  52. class Iterator(Iterable[T_co], Protocol):
  53. @abstractmethod
  54. def __next__(self) -> T_co: pass
  55. class Generator(Iterator[T], Generic[T, U, V]):
  56. @abstractmethod
  57. def send(self, value: U) -> T: pass
  58. @abstractmethod
  59. def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass
  60. @abstractmethod
  61. def close(self) -> None: pass
  62. @abstractmethod
  63. def __iter__(self) -> 'Generator[T, U, V]': pass
  64. class AsyncGenerator(AsyncIterator[T], Generic[T, U]):
  65. @abstractmethod
  66. def __anext__(self) -> Awaitable[T]: pass
  67. @abstractmethod
  68. def asend(self, value: U) -> Awaitable[T]: pass
  69. @abstractmethod
  70. def athrow(self, typ: Any, val: Any=None, tb: Any=None) -> Awaitable[T]: pass
  71. @abstractmethod
  72. def aclose(self) -> Awaitable[T]: pass
  73. @abstractmethod
  74. def __aiter__(self) -> 'AsyncGenerator[T, U]': pass
  75. @runtime_checkable
  76. class Awaitable(Protocol[T]):
  77. @abstractmethod
  78. def __await__(self) -> Generator[Any, Any, T]: pass
  79. class AwaitableGenerator(Generator[T, U, V], Awaitable[V], Generic[T, U, V, S], metaclass=ABCMeta):
  80. pass
  81. class Coroutine(Awaitable[V], Generic[T, U, V]):
  82. @abstractmethod
  83. def send(self, value: U) -> T: pass
  84. @abstractmethod
  85. def throw(self, typ: Any, val: Any=None, tb: Any=None) -> None: pass
  86. @abstractmethod
  87. def close(self) -> None: pass
  88. @runtime_checkable
  89. class AsyncIterable(Protocol[T]):
  90. @abstractmethod
  91. def __aiter__(self) -> 'AsyncIterator[T]': pass
  92. @runtime_checkable
  93. class AsyncIterator(AsyncIterable[T], Protocol):
  94. def __aiter__(self) -> 'AsyncIterator[T]': return self
  95. @abstractmethod
  96. def __anext__(self) -> Awaitable[T]: pass
  97. class Sequence(Iterable[T_co], Container[T_co]):
  98. @abstractmethod
  99. def __getitem__(self, n: Any) -> T_co: pass
  100. class Mapping(Iterable[T], Generic[T, T_co], metaclass=ABCMeta):
  101. def keys(self) -> Iterable[T]: pass # Approximate return type
  102. def __getitem__(self, key: T) -> T_co: pass
  103. @overload
  104. def get(self, k: T) -> Optional[T_co]: pass
  105. @overload
  106. def get(self, k: T, default: Union[T_co, V]) -> Union[T_co, V]: pass
  107. def values(self) -> Iterable[T_co]: pass # Approximate return type
  108. def items(self) -> Iterable[Tuple[T, T_co]]: pass # Approximate return type
  109. def __len__(self) -> int: ...
  110. def __contains__(self, arg: object) -> int: pass
  111. class MutableMapping(Mapping[T, U], metaclass=ABCMeta):
  112. def __setitem__(self, k: T, v: U) -> None: pass
  113. class SupportsInt(Protocol):
  114. def __int__(self) -> int: pass
  115. class SupportsFloat(Protocol):
  116. def __float__(self) -> float: pass
  117. def runtime_checkable(cls: T) -> T:
  118. return cls
  119. class ContextManager(Generic[T]):
  120. def __enter__(self) -> T: pass
  121. # Use Any because not all the precise types are in the fixtures.
  122. def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass
  123. TYPE_CHECKING = 1
  124. # Fallback type for all typed dicts (does not exist at runtime).
  125. class _TypedDict(Mapping[str, object]):
  126. # Needed to make this class non-abstract. It is explicitly declared abstract in
  127. # typeshed, but we don't want to import abc here, as it would slow down the tests.
  128. def __iter__(self) -> Iterator[str]: ...
  129. def copy(self: T) -> T: ...
  130. # Using NoReturn so that only calls using the plugin hook can go through.
  131. def setdefault(self, k: NoReturn, default: object) -> object: ...
  132. # Mypy expects that 'default' has a type variable type.
  133. def pop(self, k: NoReturn, default: T = ...) -> object: ...
  134. def update(self: T, __m: T) -> None: ...
  135. def __delitem__(self, k: NoReturn) -> None: ...