4 from abc import abstractmethod, ABCMeta
\r
5 from types import CodeType, FrameType, TracebackType
\r
6 import collections # Needed by aliases like DefaultDict, see mypy issue 2986
\r
8 # Definitions of special type checking related constructs. Their definition
\r
9 # are not used, so their value does not matter.
\r
15 no_type_check = object()
\r
18 def __getitem__(self, typeargs: Any) -> Any: ...
\r
20 Tuple: _SpecialForm = ...
\r
21 Generic: _SpecialForm = ...
\r
22 Protocol: _SpecialForm = ...
\r
23 Callable: _SpecialForm = ...
\r
24 Type: _SpecialForm = ...
\r
25 ClassVar: _SpecialForm = ...
\r
27 class GenericMeta(type): ...
\r
29 # Return type that indicates a function does not return.
\r
30 # This type is equivalent to the None type, but the no-op Union is necessary to
\r
31 # distinguish the None type from the None value.
\r
32 NoReturn = Union[None]
\r
34 # Type aliases and type constructors
\r
37 # Class for defining generic aliases for library types.
\r
38 def __init__(self, target_type: type) -> None: ...
\r
39 def __getitem__(self, typeargs: Any) -> Any: ...
\r
41 Union = TypeAlias(object)
\r
42 Optional = TypeAlias(object)
\r
43 List = TypeAlias(object)
\r
44 Dict = TypeAlias(object)
\r
45 DefaultDict = TypeAlias(object)
\r
46 Set = TypeAlias(object)
\r
47 FrozenSet = TypeAlias(object)
\r
48 Counter = TypeAlias(object)
\r
49 Deque = TypeAlias(object)
\r
50 ChainMap = TypeAlias(object)
\r
52 # Predefined type variables.
\r
53 AnyStr = TypeVar('AnyStr', str, bytes)
\r
55 # Abstract base classes.
\r
57 # These type variables are used by the container types.
\r
60 _KT = TypeVar('_KT') # Key type.
\r
61 _VT = TypeVar('_VT') # Value type.
\r
62 _T_co = TypeVar('_T_co', covariant=True) # Any type covariant containers.
\r
63 _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers.
\r
64 _KT_co = TypeVar('_KT_co', covariant=True) # Key type covariant containers.
\r
65 _VT_co = TypeVar('_VT_co', covariant=True) # Value type covariant containers.
\r
66 _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
\r
67 _TC = TypeVar('_TC', bound=Type[object])
\r
69 def runtime(cls: _TC) -> _TC: ...
\r
72 class SupportsInt(Protocol, metaclass=ABCMeta):
\r
74 def __int__(self) -> int: ...
\r
77 class SupportsFloat(Protocol, metaclass=ABCMeta):
\r
79 def __float__(self) -> float: ...
\r
82 class SupportsComplex(Protocol, metaclass=ABCMeta):
\r
84 def __complex__(self) -> complex: ...
\r
87 class SupportsBytes(Protocol, metaclass=ABCMeta):
\r
89 def __bytes__(self) -> bytes: ...
\r
92 class SupportsAbs(Protocol[_T_co]):
\r
94 def __abs__(self) -> _T_co: ...
\r
97 class SupportsRound(Protocol[_T_co]):
\r
99 def __round__(self, ndigits: int = ...) -> _T_co: ...
\r
102 class Reversible(Protocol[_T_co]):
\r
104 def __reversed__(self) -> Iterator[_T_co]: ...
\r
107 class Sized(Protocol, metaclass=ABCMeta):
\r
109 def __len__(self) -> int: ...
\r
112 class Hashable(Protocol, metaclass=ABCMeta):
\r
113 # TODO: This is special, in that a subclass of a hashable class may not be hashable
\r
114 # (for example, list vs. object). It's not obvious how to represent this. This class
\r
115 # is currently mostly useless for static checking.
\r
117 def __hash__(self) -> int: ...
\r
120 class Iterable(Protocol[_T_co]):
\r
122 def __iter__(self) -> Iterator[_T_co]: ...
\r
125 class Iterator(Iterable[_T_co], Protocol[_T_co]):
\r
127 def __next__(self) -> _T_co: ...
\r
128 def __iter__(self) -> Iterator[_T_co]: ...
\r
130 class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
\r
132 def __next__(self) -> _T_co: ...
\r
135 def send(self, value: _T_contra) -> _T_co: ...
\r
138 def throw(self, typ: Type[BaseException], val: Optional[BaseException] = ...,
\r
139 tb: Optional[TracebackType] = ...) -> _T_co: ...
\r
142 def close(self) -> None: ...
\r
145 def __iter__(self) -> Generator[_T_co, _T_contra, _V_co]: ...
\r
148 def gi_code(self) -> CodeType: ...
\r
150 def gi_frame(self) -> FrameType: ...
\r
152 def gi_running(self) -> bool: ...
\r
154 def gi_yieldfrom(self) -> Optional[Generator]: ...
\r
156 # TODO: Several types should only be defined if sys.python_version >= (3, 5):
\r
157 # Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
\r
158 # See https: //github.com/python/typeshed/issues/655 for why this is not easy.
\r
161 class Awaitable(Protocol[_T_co]):
\r
163 def __await__(self) -> Generator[Any, None, _T_co]: ...
\r
165 class Coroutine(Awaitable[_V_co], Generic[_T_co, _T_contra, _V_co]):
\r
167 def send(self, value: _T_contra) -> _T_co: ...
\r
170 def throw(self, typ: Type[BaseException], val: Optional[BaseException] = ...,
\r
171 tb: Optional[TracebackType] = ...) -> _T_co: ...
\r
174 def close(self) -> None: ...
\r
177 # NOTE: This type does not exist in typing.py or PEP 484.
\r
178 # The parameters corrrespond to Generator, but the 4th is the original type.
\r
179 class AwaitableGenerator(Awaitable[_V_co], Generator[_T_co, _T_contra, _V_co],
\r
180 Generic[_T_co, _T_contra, _V_co, _S], metaclass=ABCMeta): ...
\r
183 class AsyncIterable(Protocol[_T_co]):
\r
185 def __aiter__(self) -> AsyncIterator[_T_co]: ...
\r
188 class AsyncIterator(AsyncIterable[_T_co],
\r
191 def __anext__(self) -> Awaitable[_T_co]: ...
\r
192 def __aiter__(self) -> AsyncIterator[_T_co]: ...
\r
194 if sys.version_info >= (3, 6):
\r
195 class AsyncGenerator(AsyncIterator[_T_co], Generic[_T_co, _T_contra]):
\r
197 def __anext__(self) -> Awaitable[_T_co]: ...
\r
200 def asend(self, value: _T_contra) -> Awaitable[_T_co]: ...
\r
203 def athrow(self, typ: Type[BaseException], val: Optional[BaseException] = ...,
\r
204 tb: Any = ...) -> Awaitable[_T_co]: ...
\r
207 def aclose(self) -> Awaitable[_T_co]: ...
\r
210 def __aiter__(self) -> AsyncGenerator[_T_co, _T_contra]: ...
\r
213 def ag_await(self) -> Any: ...
\r
215 def ag_code(self) -> CodeType: ...
\r
217 def ag_frame(self) -> FrameType: ...
\r
219 def ag_running(self) -> bool: ...
\r
222 class Container(Protocol[_T_co]):
\r
224 def __contains__(self, x: object) -> bool: ...
\r
227 if sys.version_info >= (3, 6):
\r
229 class Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
\r
230 _Collection = Collection
\r
233 class _Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
\r
235 class Sequence(_Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
\r
238 def __getitem__(self, i: int) -> _T_co: ...
\r
241 def __getitem__(self, s: slice) -> Sequence[_T_co]: ...
\r
243 if sys.version_info >= (3, 5):
\r
244 def index(self, x: Any, start: int = ..., end: int = ...) -> int: ...
\r
246 def index(self, x: Any) -> int: ...
\r
247 def count(self, x: Any) -> int: ...
\r
248 def __contains__(self, x: object) -> bool: ...
\r
249 def __iter__(self) -> Iterator[_T_co]: ...
\r
250 def __reversed__(self) -> Iterator[_T_co]: ...
\r
252 class MutableSequence(Sequence[_T], Generic[_T]):
\r
254 def insert(self, index: int, object: _T) -> None: ...
\r
257 def __setitem__(self, i: int, o: _T) -> None: ...
\r
260 def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
\r
263 def __delitem__(self, i: int) -> None: ...
\r
266 def __delitem__(self, i: slice) -> None: ...
\r
268 def append(self, object: _T) -> None: ...
\r
269 def clear(self) -> None: ...
\r
270 def extend(self, iterable: Iterable[_T]) -> None: ...
\r
271 def reverse(self) -> None: ...
\r
272 def pop(self, index: int = ...) -> _T: ...
\r
273 def remove(self, object: _T) -> None: ...
\r
274 def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: ...
\r
276 class AbstractSet(_Collection[_T_co], Generic[_T_co]):
\r
278 def __contains__(self, x: object) -> bool: ...
\r
280 def __le__(self, s: AbstractSet[Any]) -> bool: ...
\r
281 def __lt__(self, s: AbstractSet[Any]) -> bool: ...
\r
282 def __gt__(self, s: AbstractSet[Any]) -> bool: ...
\r
283 def __ge__(self, s: AbstractSet[Any]) -> bool: ...
\r
284 def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
\r
285 def __or__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
\r
286 def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
\r
287 def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
\r
288 # TODO: Argument can be a more general ABC?
\r
289 def isdisjoint(self, s: AbstractSet[Any]) -> bool: ...
\r
291 class MutableSet(AbstractSet[_T], Generic[_T]):
\r
293 def add(self, x: _T) -> None: ...
\r
295 def discard(self, x: _T) -> None: ...
\r
297 def clear(self) -> None: ...
\r
298 def pop(self) -> _T: ...
\r
299 def remove(self, element: _T) -> None: ...
\r
300 def __ior__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
\r
301 def __iand__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
\r
302 def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
\r
303 def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
\r
305 class MappingView(Sized):
\r
306 def __len__(self) -> int: ...
\r
308 class ItemsView(AbstractSet[Tuple[_KT_co, _VT_co]], MappingView, Generic[_KT_co, _VT_co]):
\r
309 def __contains__(self, o: object) -> bool: ...
\r
310 def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
\r
312 class KeysView(AbstractSet[_KT_co], MappingView, Generic[_KT_co]):
\r
313 def __contains__(self, o: object) -> bool: ...
\r
314 def __iter__(self) -> Iterator[_KT_co]: ...
\r
316 class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
\r
317 def __contains__(self, o: object) -> bool: ...
\r
318 def __iter__(self) -> Iterator[_VT_co]: ...
\r
321 class ContextManager(Protocol[_T_co]):
\r
322 def __enter__(self) -> _T_co: ...
\r
323 def __exit__(self, exc_type: Optional[Type[BaseException]],
\r
324 exc_value: Optional[BaseException],
\r
325 traceback: Optional[TracebackType]) -> Optional[bool]: ...
\r
327 if sys.version_info >= (3, 5):
\r
329 class AsyncContextManager(Protocol[_T_co]):
\r
330 def __aenter__(self) -> Awaitable[_T_co]: ...
\r
331 def __aexit__(self, exc_type: Optional[Type[BaseException]],
\r
332 exc_value: Optional[BaseException],
\r
333 traceback: Optional[TracebackType]) -> Awaitable[Optional[bool]]: ...
\r
335 class Mapping(_Collection[_KT], Generic[_KT, _VT_co]):
\r
336 # TODO: We wish the key type could also be covariant, but that doesn't work,
\r
337 # see discussion in https: //github.com/python/typing/pull/273.
\r
339 def __getitem__(self, k: _KT) -> _VT_co:
\r
343 def get(self, k: _KT) -> Optional[_VT_co]: ...
\r
345 def get(self, k: _KT, default: Union[_VT_co, _T]) -> Union[_VT_co, _T]: ...
\r
346 def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ...
\r
347 def keys(self) -> AbstractSet[_KT]: ...
\r
348 def values(self) -> ValuesView[_VT_co]: ...
\r
349 def __contains__(self, o: object) -> bool: ...
\r
351 class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
\r
353 def __setitem__(self, k: _KT, v: _VT) -> None: ...
\r
355 def __delitem__(self, v: _KT) -> None: ...
\r
357 def clear(self) -> None: ...
\r
359 def pop(self, k: _KT) -> _VT: ...
\r
361 def pop(self, k: _KT, default: Union[_VT, _T] = ...) -> Union[_VT, _T]: ...
\r
362 def popitem(self) -> Tuple[_KT, _VT]: ...
\r
363 def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
\r
364 # 'update' used to take a Union, but using overloading is better.
\r
365 # The second overloaded type here is a bit too general, because
\r
366 # Mapping[Tuple[_KT, _VT], W] is a subclass of Iterable[Tuple[_KT, _VT]],
\r
367 # but will always have the behavior of the first overloaded type
\r
368 # at runtime, leading to keys of a mix of types _KT and Tuple[_KT, _VT].
\r
369 # We don't currently have any way of forcing all Mappings to use
\r
370 # the first overload, but by using overloading rather than a Union,
\r
371 # mypy will commit to using the first overload when the argument is
\r
372 # known to be a Mapping with unknown type parameters, which is closer
\r
373 # to the behavior we want. See mypy issue #1430.
\r
375 def update(self, __m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
\r
377 def update(self, __m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
\r
379 def update(self, **kwargs: _VT) -> None: ...
\r
383 TYPE_CHECKING = True
\r
385 class IO(Iterator[AnyStr], Generic[AnyStr]):
\r
387 # TODO use abstract properties
\r
389 def mode(self) -> str: ...
\r
391 def name(self) -> str: ...
\r
393 def close(self) -> None: ...
\r
395 def closed(self) -> bool: ...
\r
397 def fileno(self) -> int: ...
\r
399 def flush(self) -> None: ...
\r
401 def isatty(self) -> bool: ...
\r
402 # TODO what if n is None?
\r
404 def read(self, n: int = ...) -> AnyStr: ...
\r
406 def readable(self) -> bool: ...
\r
408 def readline(self, limit: int = ...) -> AnyStr: ...
\r
410 def readlines(self, hint: int = ...) -> list[AnyStr]: ...
\r
412 def seek(self, offset: int, whence: int = ...) -> int: ...
\r
414 def seekable(self) -> bool: ...
\r
416 def tell(self) -> int: ...
\r
418 def truncate(self, size: Optional[int] = ...) -> int: ...
\r
420 def writable(self) -> bool: ...
\r
421 # TODO buffer objects
\r
423 def write(self, s: AnyStr) -> int: ...
\r
425 def writelines(self, lines: Iterable[AnyStr]) -> None: ...
\r
428 def __next__(self) -> AnyStr: ...
\r
430 def __iter__(self) -> Iterator[AnyStr]: ...
\r
432 def __enter__(self) -> IO[AnyStr]: ...
\r
434 def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException],
\r
435 traceback: Optional[TracebackType]) -> bool: ...
\r
437 class BinaryIO(IO[bytes]):
\r
443 def write(self, s: bytearray) -> int: ...
\r
446 def write(self, s: bytes) -> int: ...
\r
449 def __enter__(self) -> BinaryIO: ...
\r
451 class TextIO(IO[str]):
\r
452 # TODO use abstractproperty
\r
454 def buffer(self) -> BinaryIO: ...
\r
456 def encoding(self) -> str: ...
\r
458 def errors(self) -> Optional[str]: ...
\r
460 def line_buffering(self) -> int: ... # int on PyPy, bool on CPython
\r
462 def newlines(self) -> Any: ... # None, str or tuple
\r
464 def __enter__(self) -> TextIO: ...
\r
466 class ByteString(Sequence[int], metaclass=ABCMeta): ...
\r
468 class Match(Generic[AnyStr]):
\r
472 lastgroup = ... # type: AnyStr
\r
473 string = ... # type: AnyStr
\r
475 # The regular expression object whose match() or search() method produced
\r
476 # this match instance.
\r
477 re = ... # type: 'Pattern[AnyStr]'
\r
479 def expand(self, template: AnyStr) -> AnyStr: ...
\r
482 def group(self, group1: int = ...) -> AnyStr: ...
\r
484 def group(self, group1: str) -> AnyStr: ...
\r
486 def group(self, group1: int, group2: int,
\r
487 *groups: int) -> Sequence[AnyStr]: ...
\r
489 def group(self, group1: str, group2: str,
\r
490 *groups: str) -> Sequence[AnyStr]: ...
\r
492 def groups(self, default: AnyStr = ...) -> Sequence[AnyStr]: ...
\r
493 def groupdict(self, default: AnyStr = ...) -> dict[str, AnyStr]: ...
\r
494 def start(self, group: Union[int, str] = ...) -> int: ...
\r
495 def end(self, group: Union[int, str] = ...) -> int: ...
\r
496 def span(self, group: Union[int, str] = ...) -> Tuple[int, int]: ...
\r
497 if sys.version_info >= (3, 6):
\r
498 def __getitem__(self, g: Union[int, str]) -> AnyStr: ...
\r
500 class Pattern(Generic[AnyStr]):
\r
502 groupindex = ... # type: Mapping[str, int]
\r
504 pattern = ... # type: AnyStr
\r
506 def search(self, string: AnyStr, pos: int = ...,
\r
507 endpos: int = ...) -> Optional[Match[AnyStr]]: ...
\r
508 def match(self, string: AnyStr, pos: int = ...,
\r
509 endpos: int = ...) -> Optional[Match[AnyStr]]: ...
\r
510 # New in Python 3.4
\r
511 def fullmatch(self, string: AnyStr, pos: int = ...,
\r
512 endpos: int = ...) -> Optional[Match[AnyStr]]: ...
\r
513 def split(self, string: AnyStr, maxsplit: int = ...) -> list[AnyStr]: ...
\r
514 def findall(self, string: AnyStr, pos: int = ...,
\r
515 endpos: int = ...) -> list[Any]: ...
\r
516 def finditer(self, string: AnyStr, pos: int = ...,
\r
517 endpos: int = ...) -> Iterator[Match[AnyStr]]: ...
\r
520 def sub(self, repl: AnyStr, string: AnyStr,
\r
521 count: int = ...) -> AnyStr: ...
\r
523 def sub(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr,
\r
524 count: int = ...) -> AnyStr: ...
\r
527 def subn(self, repl: AnyStr, string: AnyStr,
\r
528 count: int = ...) -> Tuple[AnyStr, int]: ...
\r
530 def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr,
\r
531 count: int = ...) -> Tuple[AnyStr, int]: ...
\r
535 def get_type_hints(obj: Callable, globalns: Optional[dict[str, Any]] = ...,
\r
536 localns: Optional[dict[str, Any]] = ...) -> dict[str, Any]: ...
\r
538 def cast(tp: Type[_T], obj: Any) -> _T: ...
\r
540 # Type constructors
\r
542 # NamedTuple is special-cased in the type checker
\r
543 class NamedTuple(tuple):
\r
544 _field_types = ... # type: collections.OrderedDict[str, Type[Any]]
\r
545 _fields = ... # type: Tuple[str, ...]
\r
546 _source = ... # type: str
\r
548 def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., *,
\r
549 verbose: bool = ..., rename: bool = ..., **kwargs: Any) -> None: ...
\r
552 def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T: ...
\r
554 def _asdict(self) -> collections.OrderedDict[str, Any]: ...
\r
555 def _replace(self: _T, **kwargs: Any) -> _T: ...
\r
557 def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
\r