Format: application/octet-stream
1 import collections # Needed by aliases like DefaultDict, see mypy issue 2986
2 from abc import ABCMeta, abstractmethod
3 from types import CodeType, FrameType, TracebackType
4
5 # Definitions of special type checking related constructs. Their definitions
6 # are not used, so their value does not matter.
7
8 overload = object()
9 Any = object()
10
11 class TypeVar:
12 __name__: str
13 __bound__: Optional[Type[Any]]
14 __constraints__: Tuple[Type[Any], ...]
15 __covariant__: bool
16 __contravariant__: bool
17 def __init__(
18 self,
19 name: str,
20 *constraints: Type[Any],
21 bound: Optional[Type[Any]] = ...,
22 covariant: bool = ...,
23 contravariant: bool = ...,
24 ) -> None: ...
25
26 _promote = object()
27
28 class _SpecialForm(object):
29 def __getitem__(self, typeargs: Any) -> object: ...
30
31 Union: _SpecialForm = ...
32 Optional: _SpecialForm = ...
33 Tuple: _SpecialForm = ...
34 Generic: _SpecialForm = ...
35 Protocol: _SpecialForm = ...
36 Callable: _SpecialForm = ...
37 Type: _SpecialForm = ...
38 ClassVar: _SpecialForm = ...
39 Final: _SpecialForm = ...
40 _F = TypeVar("_F", bound=Callable[..., Any])
41
42 def final(f: _F) -> _F: ...
43
44 Literal: _SpecialForm = ...
45 # TypedDict is a (non-subscriptable) special form.
46 TypedDict: object = ...
47
48 class GenericMeta(type): ...
49
50 # Return type that indicates a function does not return.
51 # This type is equivalent to the None type, but the no-op Union is necessary to
52 # distinguish the None type from the None value.
53 NoReturn = Union[None]
54
55 # These type variables are used by the container types.
56 _T = TypeVar("_T")
57 _S = TypeVar("_S")
58 _KT = TypeVar("_KT") # Key type.
59 _VT = TypeVar("_VT") # Value type.
60 _T_co = TypeVar("_T_co", covariant=True) # Any type covariant containers.
61 _V_co = TypeVar("_V_co", covariant=True) # Any type covariant containers.
62 _KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
63 _VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
64 _T_contra = TypeVar("_T_contra", contravariant=True) # Ditto contravariant.
65 _TC = TypeVar("_TC", bound=Type[object])
66 _C = TypeVar("_C", bound=Callable[..., Any])
67
68 no_type_check = object()
69
70 def no_type_check_decorator(decorator: _C) -> _C: ...
71
72 # Type aliases and type constructors
73
74 class _Alias:
75 # Class for defining generic aliases for library types.
76 def __getitem__(self, typeargs: Any) -> Any: ...
77
78 List = _Alias()
79 Dict = _Alias()
80 DefaultDict = _Alias()
81 Set = _Alias()
82 FrozenSet = _Alias()
83 Counter = _Alias()
84 Deque = _Alias()
85
86 # Predefined type variables.
87 AnyStr = TypeVar("AnyStr", str, unicode)
88
89 # Abstract base classes.
90
91 def runtime_checkable(cls: _TC) -> _TC: ...
92 @runtime_checkable
93 class SupportsInt(Protocol, metaclass=ABCMeta):
94 @abstractmethod
95 def __int__(self) -> int: ...
96
97 @runtime_checkable
98 class SupportsFloat(Protocol, metaclass=ABCMeta):
99 @abstractmethod
100 def __float__(self) -> float: ...
101
102 @runtime_checkable
103 class SupportsComplex(Protocol, metaclass=ABCMeta):
104 @abstractmethod
105 def __complex__(self) -> complex: ...
106
107 @runtime_checkable
108 class SupportsAbs(Protocol[_T_co]):
109 @abstractmethod
110 def __abs__(self) -> _T_co: ...
111
112 @runtime_checkable
113 class Reversible(Protocol[_T_co]):
114 @abstractmethod
115 def __reversed__(self) -> Iterator[_T_co]: ...
116
117 @runtime_checkable
118 class Sized(Protocol, metaclass=ABCMeta):
119 @abstractmethod
120 def __len__(self) -> int: ...
121
122 @runtime_checkable
123 class Hashable(Protocol, metaclass=ABCMeta):
124 # TODO: This is special, in that a subclass of a hashable class may not be hashable
125 # (for example, list vs. object). It's not obvious how to represent this. This class
126 # is currently mostly useless for static checking.
127 @abstractmethod
128 def __hash__(self) -> int: ...
129
130 @runtime_checkable
131 class Iterable(Protocol[_T_co]):
132 @abstractmethod
133 def __iter__(self) -> Iterator[_T_co]: ...
134
135 @runtime_checkable
136 class Iterator(Iterable[_T_co], Protocol[_T_co]):
137 @abstractmethod
138 def next(self) -> _T_co: ...
139 def __iter__(self) -> Iterator[_T_co]: ...
140
141 class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
142 @abstractmethod
143 def next(self) -> _T_co: ...
144 @abstractmethod
145 def send(self, __value: _T_contra) -> _T_co: ...
146 @overload
147 @abstractmethod
148 def throw(
149 self, __typ: Type[BaseException], __val: Union[BaseException, object] = ..., __tb: Optional[TracebackType] = ...
150 ) -> _T_co: ...
151 @overload
152 @abstractmethod
153 def throw(self, __typ: BaseException, __val: None = ..., __tb: Optional[TracebackType] = ...) -> _T_co: ...
154 @abstractmethod
155 def close(self) -> None: ...
156 @property
157 def gi_code(self) -> CodeType: ...
158 @property
159 def gi_frame(self) -> FrameType: ...
160 @property
161 def gi_running(self) -> bool: ...
162
163 @runtime_checkable
164 class Container(Protocol[_T_co]):
165 @abstractmethod
166 def __contains__(self, x: object) -> bool: ...
167
168 class Sequence(Iterable[_T_co], Container[_T_co], Reversible[_T_co], Generic[_T_co]):
169 @overload
170 @abstractmethod
171 def __getitem__(self, i: int) -> _T_co: ...
172 @overload
173 @abstractmethod
174 def __getitem__(self, s: slice) -> Sequence[_T_co]: ...
175 # Mixin methods
176 def index(self, x: Any) -> int: ...
177 def count(self, x: Any) -> int: ...
178 def __contains__(self, x: object) -> bool: ...
179 def __iter__(self) -> Iterator[_T_co]: ...
180 def __reversed__(self) -> Iterator[_T_co]: ...
181 # Implement Sized (but don't have it as a base class).
182 @abstractmethod
183 def __len__(self) -> int: ...
184
185 class MutableSequence(Sequence[_T], Generic[_T]):
186 @abstractmethod
187 def insert(self, index: int, object: _T) -> None: ...
188 @overload
189 @abstractmethod
190 def __getitem__(self, i: int) -> _T: ...
191 @overload
192 @abstractmethod
193 def __getitem__(self, s: slice) -> MutableSequence[_T]: ...
194 @overload
195 @abstractmethod
196 def __setitem__(self, i: int, o: _T) -> None: ...
197 @overload
198 @abstractmethod
199 def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
200 @overload
201 @abstractmethod
202 def __delitem__(self, i: int) -> None: ...
203 @overload
204 @abstractmethod
205 def __delitem__(self, i: slice) -> None: ...
206 # Mixin methods
207 def append(self, object: _T) -> None: ...
208 def extend(self, iterable: Iterable[_T]) -> None: ...
209 def reverse(self) -> None: ...
210 def pop(self, index: int = ...) -> _T: ...
211 def remove(self, object: _T) -> None: ...
212 def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: ...
213
214 class AbstractSet(Iterable[_T_co], Container[_T_co], Generic[_T_co]):
215 @abstractmethod
216 def __contains__(self, x: object) -> bool: ...
217 # Mixin methods
218 def __le__(self, s: AbstractSet[Any]) -> bool: ...
219 def __lt__(self, s: AbstractSet[Any]) -> bool: ...
220 def __gt__(self, s: AbstractSet[Any]) -> bool: ...
221 def __ge__(self, s: AbstractSet[Any]) -> bool: ...
222 def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
223 def __or__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
224 def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
225 def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
226 # TODO: argument can be any container?
227 def isdisjoint(self, s: AbstractSet[Any]) -> bool: ...
228 # Implement Sized (but don't have it as a base class).
229 @abstractmethod
230 def __len__(self) -> int: ...
231
232 class MutableSet(AbstractSet[_T], Generic[_T]):
233 @abstractmethod
234 def add(self, x: _T) -> None: ...
235 @abstractmethod
236 def discard(self, x: _T) -> None: ...
237 # Mixin methods
238 def clear(self) -> None: ...
239 def pop(self) -> _T: ...
240 def remove(self, element: _T) -> None: ...
241 def __ior__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
242 def __iand__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
243 def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
244 def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
245
246 class MappingView(object):
247 def __len__(self) -> int: ...
248
249 class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
250 def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ...
251 def __contains__(self, o: object) -> bool: ...
252 def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
253
254 class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]):
255 def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ...
256 def __contains__(self, o: object) -> bool: ...
257 def __iter__(self) -> Iterator[_KT_co]: ...
258
259 class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
260 def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ...
261 def __contains__(self, o: object) -> bool: ...
262 def __iter__(self) -> Iterator[_VT_co]: ...
263
264 @runtime_checkable
265 class ContextManager(Protocol[_T_co]):
266 def __enter__(self) -> _T_co: ...
267 def __exit__(
268 self,
269 __exc_type: Optional[Type[BaseException]],
270 __exc_value: Optional[BaseException],
271 __traceback: Optional[TracebackType],
272 ) -> Optional[bool]: ...
273
274 class Mapping(Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
275 # TODO: We wish the key type could also be covariant, but that doesn't work,
276 # see discussion in https: //github.com/python/typing/pull/273.
277 @abstractmethod
278 def __getitem__(self, k: _KT) -> _VT_co: ...
279 # Mixin methods
280 @overload
281 def get(self, k: _KT) -> Optional[_VT_co]: ...
282 @overload
283 def get(self, k: _KT, default: Union[_VT_co, _T]) -> Union[_VT_co, _T]: ...
284 def keys(self) -> list[_KT]: ...
285 def values(self) -> list[_VT_co]: ...
286 def items(self) -> list[Tuple[_KT, _VT_co]]: ...
287 def iterkeys(self) -> Iterator[_KT]: ...
288 def itervalues(self) -> Iterator[_VT_co]: ...
289 def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ...
290 def __contains__(self, o: object) -> bool: ...
291 # Implement Sized (but don't have it as a base class).
292 @abstractmethod
293 def __len__(self) -> int: ...
294
295 class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
296 @abstractmethod
297 def __setitem__(self, k: _KT, v: _VT) -> None: ...
298 @abstractmethod
299 def __delitem__(self, v: _KT) -> None: ...
300 def clear(self) -> None: ...
301 @overload
302 def pop(self, k: _KT) -> _VT: ...
303 @overload
304 def pop(self, k: _KT, default: Union[_VT, _T] = ...) -> Union[_VT, _T]: ...
305 def popitem(self) -> Tuple[_KT, _VT]: ...
306 def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
307 @overload
308 def update(self, __m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
309 @overload
310 def update(self, __m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
311 @overload
312 def update(self, **kwargs: _VT) -> None: ...
313
314 Text = unicode
315
316 TYPE_CHECKING = True
317
318 class IO(Iterator[AnyStr], Generic[AnyStr]):
319 # TODO detach
320 # TODO use abstract properties
321 @property
322 def mode(self) -> str: ...
323 @property
324 def name(self) -> str: ...
325 @abstractmethod
326 def close(self) -> None: ...
327 @property
328 def closed(self) -> bool: ...
329 @abstractmethod
330 def fileno(self) -> int: ...
331 @abstractmethod
332 def flush(self) -> None: ...
333 @abstractmethod
334 def isatty(self) -> bool: ...
335 # TODO what if n is None?
336 @abstractmethod
337 def read(self, n: int = ...) -> AnyStr: ...
338 @abstractmethod
339 def readable(self) -> bool: ...
340 @abstractmethod
341 def readline(self, limit: int = ...) -> AnyStr: ...
342 @abstractmethod
343 def readlines(self, hint: int = ...) -> list[AnyStr]: ...
344 @abstractmethod
345 def seek(self, offset: int, whence: int = ...) -> int: ...
346 @abstractmethod
347 def seekable(self) -> bool: ...
348 @abstractmethod
349 def tell(self) -> int: ...
350 @abstractmethod
351 def truncate(self, size: Optional[int] = ...) -> int: ...
352 @abstractmethod
353 def writable(self) -> bool: ...
354 # TODO buffer objects
355 @abstractmethod
356 def write(self, s: AnyStr) -> int: ...
357 @abstractmethod
358 def writelines(self, lines: Iterable[AnyStr]) -> None: ...
359 @abstractmethod
360 def next(self) -> AnyStr: ...
361 @abstractmethod
362 def __iter__(self) -> Iterator[AnyStr]: ...
363 @abstractmethod
364 def __enter__(self) -> IO[AnyStr]: ...
365 @abstractmethod
366 def __exit__(
367 self, t: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]
368 ) -> Optional[bool]: ...
369
370 class BinaryIO(IO[str]):
371 # TODO readinto
372 # TODO read1?
373 # TODO peek?
374 @abstractmethod
375 def __enter__(self) -> BinaryIO: ...
376
377 class TextIO(IO[unicode]):
378 # TODO use abstractproperty
379 @property
380 def buffer(self) -> BinaryIO: ...
381 @property
382 def encoding(self) -> str: ...
383 @property
384 def errors(self) -> Optional[str]: ...
385 @property
386 def line_buffering(self) -> bool: ...
387 @property
388 def newlines(self) -> Any: ... # None, str or tuple
389 @abstractmethod
390 def __enter__(self) -> TextIO: ...
391
392 class ByteString(Sequence[int], metaclass=ABCMeta): ...
393
394 class Match(Generic[AnyStr]):
395 pos: int
396 endpos: int
397 lastindex: Optional[int]
398 string: AnyStr
399
400 # The regular expression object whose match() or search() method produced
401 # this match instance. This should not be Pattern[AnyStr] because the type
402 # of the pattern is independent of the type of the matched string in
403 # Python 2. Strictly speaking Match should be generic over AnyStr twice:
404 # once for the type of the pattern and once for the type of the matched
405 # string.
406 re: Pattern[Any]
407 # Can be None if there are no groups or if the last group was unnamed;
408 # otherwise matches the type of the pattern.
409 lastgroup: Optional[Any]
410 def expand(self, template: Union[str, Text]) -> Any: ...
411 @overload
412 def group(self, group1: int = ...) -> AnyStr: ...
413 @overload
414 def group(self, group1: str) -> AnyStr: ...
415 @overload
416 def group(self, group1: int, group2: int, *groups: int) -> Tuple[AnyStr, ...]: ...
417 @overload
418 def group(self, group1: str, group2: str, *groups: str) -> Tuple[AnyStr, ...]: ...
419 def groups(self, default: AnyStr = ...) -> Tuple[AnyStr, ...]: ...
420 def groupdict(self, default: AnyStr = ...) -> Dict[str, AnyStr]: ...
421 def start(self, __group: Union[int, str] = ...) -> int: ...
422 def end(self, __group: Union[int, str] = ...) -> int: ...
423 def span(self, __group: Union[int, str] = ...) -> Tuple[int, int]: ...
424 @property
425 def regs(self) -> Tuple[Tuple[int, int], ...]: ... # undocumented
426
427 # We need a second TypeVar with the same definition as AnyStr, because
428 # Pattern is generic over AnyStr (determining the type of its .pattern
429 # attribute), but at the same time its methods take either bytes or
430 # Text and return the same type, regardless of the type of the pattern.
431 _AnyStr2 = TypeVar("_AnyStr2", bytes, Text)
432
433 class Pattern(Generic[AnyStr]):
434 flags: int
435 groupindex: Dict[AnyStr, int]
436 groups: int
437 pattern: AnyStr
438 def search(self, string: _AnyStr2, pos: int = ..., endpos: int = ...) -> Optional[Match[_AnyStr2]]: ...
439 def match(self, string: _AnyStr2, pos: int = ..., endpos: int = ...) -> Optional[Match[_AnyStr2]]: ...
440 def split(self, string: _AnyStr2, maxsplit: int = ...) -> List[_AnyStr2]: ...
441 # Returns either a list of _AnyStr2 or a list of tuples, depending on
442 # whether there are groups in the pattern.
443 def findall(self, string: Union[bytes, Text], pos: int = ..., endpos: int = ...) -> List[Any]: ...
444 def finditer(self, string: _AnyStr2, pos: int = ..., endpos: int = ...) -> Iterator[Match[_AnyStr2]]: ...
445 @overload
446 def sub(self, repl: _AnyStr2, string: _AnyStr2, count: int = ...) -> _AnyStr2: ...
447 @overload
448 def sub(self, repl: Callable[[Match[_AnyStr2]], _AnyStr2], string: _AnyStr2, count: int = ...) -> _AnyStr2: ...
449 @overload
450 def subn(self, repl: _AnyStr2, string: _AnyStr2, count: int = ...) -> Tuple[_AnyStr2, int]: ...
451 @overload
452 def subn(self, repl: Callable[[Match[_AnyStr2]], _AnyStr2], string: _AnyStr2, count: int = ...) -> Tuple[_AnyStr2, int]: ...
453
454 # Functions
455
456 def get_type_hints(
457 obj: Callable[..., Any], globalns: Optional[Dict[Text, Any]] = ..., localns: Optional[Dict[Text, Any]] = ...
458 ) -> None: ...
459 @overload
460 def cast(tp: Type[_T], obj: Any) -> _T: ...
461 @overload
462 def cast(tp: str, obj: Any) -> Any: ...
463 @overload
464 def cast(tp: object, obj: Any) -> Any: ...
465
466 # Type constructors
467
468 # NamedTuple is special-cased in the type checker
469 class NamedTuple(Tuple[Any, ...]):
470 _fields: Tuple[str, ...]
471 def __init__(self, typename: Text, fields: Iterable[Tuple[Text, Any]] = ..., **kwargs: Any) -> None: ...
472 @classmethod
473 def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T: ...
474 def _asdict(self) -> Dict[str, Any]: ...
475 def _replace(self: _T, **kwargs: Any) -> _T: ...
476
477 # Internal mypy fallback type for all typed dicts (does not exist at runtime)
478 class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
479 def copy(self: _T) -> _T: ...
480 # Using NoReturn so that only calls using mypy plugin hook that specialize the signature
481 # can go through.
482 def setdefault(self, k: NoReturn, default: object) -> object: ...
483 # Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
484 def pop(self, k: NoReturn, default: _T = ...) -> object: ...
485 def update(self: _T, __m: _T) -> None: ...
486 def has_key(self, k: str) -> bool: ...
487 def viewitems(self) -> ItemsView[str, object]: ...
488 def viewkeys(self) -> KeysView[str]: ...
489 def viewvalues(self) -> ValuesView[object]: ...
490 def __delitem__(self, k: NoReturn) -> None: ...
491
492 def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
493
494 # This itself is only available during type checking
495 def type_check_only(func_or_cls: _C) -> _C: ...
496