1 # Stubs for typing (Python 2.7)
\r
3 from abc import abstractmethod, ABCMeta
\r
4 from types import CodeType, FrameType, TracebackType
\r
5 import collections # Needed by aliases like DefaultDict, see mypy issue 2986
\r
7 # Definitions of special type checking related constructs. Their definitions
\r
8 # are not used, so their value does not matter.
\r
14 no_type_check = object()
\r
16 class _SpecialForm(object):
\r
17 def __getitem__(self, typeargs: Any) -> object: ...
\r
19 Tuple: _SpecialForm = ...
\r
20 Generic: _SpecialForm = ...
\r
21 Protocol: _SpecialForm = ...
\r
22 Callable: _SpecialForm = ...
\r
23 Type: _SpecialForm = ...
\r
24 ClassVar: _SpecialForm = ...
\r
26 class GenericMeta(type): ...
\r
28 # Return type that indicates a function does not return.
\r
29 # This type is equivalent to the None type, but the no-op Union is necessary to
\r
30 # distinguish the None type from the None value.
\r
31 NoReturn = Union[None]
\r
33 # Type aliases and type constructors
\r
36 # Class for defining generic aliases for library types.
\r
37 def __init__(self, target_type: type) -> None: ...
\r
38 def __getitem__(self, typeargs: Any) -> Any: ...
\r
40 Union = TypeAlias(object)
\r
41 Optional = TypeAlias(object)
\r
42 List = TypeAlias(object)
\r
43 Dict = TypeAlias(object)
\r
44 DefaultDict = TypeAlias(object)
\r
45 Set = TypeAlias(object)
\r
46 FrozenSet = TypeAlias(object)
\r
47 Counter = TypeAlias(object)
\r
48 Deque = TypeAlias(object)
\r
50 # Predefined type variables.
\r
51 AnyStr = TypeVar('AnyStr', str, unicode)
\r
53 # Abstract base classes.
\r
55 # These type variables are used by the container types.
\r
58 _KT = TypeVar('_KT') # Key type.
\r
59 _VT = TypeVar('_VT') # Value type.
\r
60 _T_co = TypeVar('_T_co', covariant=True) # Any type covariant containers.
\r
61 _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers.
\r
62 _KT_co = TypeVar('_KT_co', covariant=True) # Key type covariant containers.
\r
63 _VT_co = TypeVar('_VT_co', covariant=True) # Value type covariant containers.
\r
64 _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
\r
65 _TC = TypeVar('_TC', bound=Type[object])
\r
67 def runtime(cls: _TC) -> _TC: ...
\r
70 class SupportsInt(Protocol, metaclass=ABCMeta):
\r
72 def __int__(self) -> int: ...
\r
75 class SupportsFloat(Protocol, metaclass=ABCMeta):
\r
77 def __float__(self) -> float: ...
\r
80 class SupportsComplex(Protocol, metaclass=ABCMeta):
\r
82 def __complex__(self) -> complex: ...
\r
85 class SupportsAbs(Protocol[_T_co]):
\r
87 def __abs__(self) -> _T_co: ...
\r
90 class SupportsRound(Protocol[_T_co]):
\r
92 def __round__(self, ndigits: int = ...) -> _T_co: ...
\r
95 class Reversible(Protocol[_T_co]):
\r
97 def __reversed__(self) -> Iterator[_T_co]: ...
\r
100 class Sized(Protocol, metaclass=ABCMeta):
\r
102 def __len__(self) -> int: ...
\r
105 class Hashable(Protocol, metaclass=ABCMeta):
\r
106 # TODO: This is special, in that a subclass of a hashable class may not be hashable
\r
107 # (for example, list vs. object). It's not obvious how to represent this. This class
\r
108 # is currently mostly useless for static checking.
\r
110 def __hash__(self) -> int: ...
\r
113 class Iterable(Protocol[_T_co]):
\r
115 def __iter__(self) -> Iterator[_T_co]: ...
\r
118 class Iterator(Iterable[_T_co], Protocol[_T_co]):
\r
120 def next(self) -> _T_co: ...
\r
121 def __iter__(self) -> Iterator[_T_co]: ...
\r
123 class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
\r
125 def next(self) -> _T_co: ...
\r
128 def send(self, value: _T_contra) -> _T_co: ...
\r
131 def throw(self, typ: Type[BaseException], val: Optional[BaseException] = ...,
\r
132 tb: TracebackType = ...) -> _T_co: ...
\r
134 def close(self) -> None: ...
\r
136 def gi_code(self) -> CodeType: ...
\r
138 def gi_frame(self) -> FrameType: ...
\r
140 def gi_running(self) -> bool: ...
\r
143 class Container(Protocol[_T_co]):
\r
145 def __contains__(self, x: object) -> bool: ...
\r
147 class Sequence(Iterable[_T_co], Container[_T_co], Sized, Reversible[_T_co], Generic[_T_co]):
\r
150 def __getitem__(self, i: int) -> _T_co: ...
\r
153 def __getitem__(self, s: slice) -> Sequence[_T_co]: ...
\r
155 def index(self, x: Any) -> int: ...
\r
156 def count(self, x: Any) -> int: ...
\r
157 def __contains__(self, x: object) -> bool: ...
\r
158 def __iter__(self) -> Iterator[_T_co]: ...
\r
159 def __reversed__(self) -> Iterator[_T_co]: ...
\r
161 class MutableSequence(Sequence[_T], Generic[_T]):
\r
163 def insert(self, index: int, object: _T) -> None: ...
\r
166 def __setitem__(self, i: int, o: _T) -> None: ...
\r
169 def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
\r
172 def __delitem__(self, i: int) -> None: ...
\r
175 def __delitem__(self, i: slice) -> None: ...
\r
177 def append(self, object: _T) -> None: ...
\r
178 def extend(self, iterable: Iterable[_T]) -> None: ...
\r
179 def reverse(self) -> None: ...
\r
180 def pop(self, index: int = ...) -> _T: ...
\r
181 def remove(self, object: _T) -> None: ...
\r
182 def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: ...
\r
184 class AbstractSet(Sized, Iterable[_T_co], Container[_T_co], Generic[_T_co]):
\r
186 def __contains__(self, x: object) -> bool: ...
\r
188 def __le__(self, s: AbstractSet[Any]) -> bool: ...
\r
189 def __lt__(self, s: AbstractSet[Any]) -> bool: ...
\r
190 def __gt__(self, s: AbstractSet[Any]) -> bool: ...
\r
191 def __ge__(self, s: AbstractSet[Any]) -> bool: ...
\r
192 def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
\r
193 def __or__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
\r
194 def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ...
\r
195 def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
\r
196 # TODO: argument can be any container?
\r
197 def isdisjoint(self, s: AbstractSet[Any]) -> bool: ...
\r
199 class MutableSet(AbstractSet[_T], Generic[_T]):
\r
201 def add(self, x: _T) -> None: ...
\r
203 def discard(self, x: _T) -> None: ...
\r
205 def clear(self) -> None: ...
\r
206 def pop(self) -> _T: ...
\r
207 def remove(self, element: _T) -> None: ...
\r
208 def __ior__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
\r
209 def __iand__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
\r
210 def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
\r
211 def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...
\r
213 class MappingView(Sized):
\r
214 def __len__(self) -> int: ...
\r
216 class ItemsView(AbstractSet[Tuple[_KT_co, _VT_co]], MappingView, Generic[_KT_co, _VT_co]):
\r
217 def __contains__(self, o: object) -> bool: ...
\r
218 def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
\r
220 class KeysView(AbstractSet[_KT_co], MappingView, Generic[_KT_co]):
\r
221 def __contains__(self, o: object) -> bool: ...
\r
222 def __iter__(self) -> Iterator[_KT_co]: ...
\r
224 class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
\r
225 def __contains__(self, o: object) -> bool: ...
\r
226 def __iter__(self) -> Iterator[_VT_co]: ...
\r
229 class ContextManager(Protocol[_T_co]):
\r
230 def __enter__(self) -> _T_co: ...
\r
231 def __exit__(self, exc_type: Optional[Type[BaseException]],
\r
232 exc_value: Optional[BaseException],
\r
233 traceback: Optional[TracebackType]) -> Optional[bool]: ...
\r
235 class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
\r
236 # TODO: We wish the key type could also be covariant, but that doesn't work,
\r
237 # see discussion in https: //github.com/python/typing/pull/273.
\r
239 def __getitem__(self, k: _KT) -> _VT_co:
\r
243 def get(self, k: _KT) -> Optional[_VT_co]: ...
\r
245 def get(self, k: _KT, default: Union[_VT_co, _T]) -> Union[_VT_co, _T]: ...
\r
246 def keys(self) -> list[_KT]: ...
\r
247 def values(self) -> list[_VT_co]: ...
\r
248 def items(self) -> list[Tuple[_KT, _VT_co]]: ...
\r
249 def iterkeys(self) -> Iterator[_KT]: ...
\r
250 def itervalues(self) -> Iterator[_VT_co]: ...
\r
251 def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ...
\r
252 def __contains__(self, o: object) -> bool: ...
\r
254 class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
\r
256 def __setitem__(self, k: _KT, v: _VT) -> None: ...
\r
258 def __delitem__(self, v: _KT) -> None: ...
\r
260 def clear(self) -> None: ...
\r
262 def pop(self, k: _KT) -> _VT: ...
\r
264 def pop(self, k: _KT, default: Union[_VT, _T] = ...) -> Union[_VT, _T]: ...
\r
265 def popitem(self) -> Tuple[_KT, _VT]: ...
\r
266 def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
\r
268 def update(self, __m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
\r
270 def update(self, __m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
\r
272 def update(self, **kwargs: _VT) -> None: ...
\r
276 TYPE_CHECKING = True
\r
278 class IO(Iterator[AnyStr], Generic[AnyStr]):
\r
280 # TODO use abstract properties
\r
282 def mode(self) -> str: ...
\r
284 def name(self) -> str: ...
\r
286 def close(self) -> None: ...
\r
288 def closed(self) -> bool: ...
\r
290 def fileno(self) -> int: ...
\r
292 def flush(self) -> None: ...
\r
294 def isatty(self) -> bool: ...
\r
295 # TODO what if n is None?
\r
297 def read(self, n: int = ...) -> AnyStr: ...
\r
299 def readable(self) -> bool: ...
\r
301 def readline(self, limit: int = ...) -> AnyStr: ...
\r
303 def readlines(self, hint: int = ...) -> list[AnyStr]: ...
\r
305 def seek(self, offset: int, whence: int = ...) -> int: ...
\r
307 def seekable(self) -> bool: ...
\r
309 def tell(self) -> int: ...
\r
311 def truncate(self, size: Optional[int] = ...) -> int: ...
\r
313 def writable(self) -> bool: ...
\r
314 # TODO buffer objects
\r
316 def write(self, s: AnyStr) -> int: ...
\r
318 def writelines(self, lines: Iterable[AnyStr]) -> None: ...
\r
321 def next(self) -> AnyStr: ...
\r
323 def __iter__(self) -> Iterator[AnyStr]: ...
\r
325 def __enter__(self) -> IO[AnyStr]: ...
\r
327 def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException],
\r
328 traceback: Optional[TracebackType]) -> bool: ...
\r
330 class BinaryIO(IO[str]):
\r
335 def __enter__(self) -> BinaryIO: ...
\r
337 class TextIO(IO[unicode]):
\r
338 # TODO use abstractproperty
\r
340 def buffer(self) -> BinaryIO: ...
\r
342 def encoding(self) -> str: ...
\r
344 def errors(self) -> Optional[str]: ...
\r
346 def line_buffering(self) -> bool: ...
\r
348 def newlines(self) -> Any: ... # None, str or tuple
\r
350 def __enter__(self) -> TextIO: ...
\r
352 class ByteString(Sequence[int], metaclass=ABCMeta): ...
\r
354 class Match(Generic[AnyStr]):
\r
358 lastgroup = ... # type: AnyStr
\r
359 string = ... # type: AnyStr
\r
361 # The regular expression object whose match() or search() method produced
\r
362 # this match instance.
\r
363 re = ... # type: 'Pattern[AnyStr]'
\r
365 def expand(self, template: AnyStr) -> AnyStr: ...
\r
368 def group(self, group1: int = ...) -> AnyStr: ...
\r
370 def group(self, group1: str) -> AnyStr: ...
\r
372 def group(self, group1: int, group2: int,
\r
373 *groups: int) -> Sequence[AnyStr]: ...
\r
375 def group(self, group1: str, group2: str,
\r
376 *groups: str) -> Sequence[AnyStr]: ...
\r
378 def groups(self, default: AnyStr = ...) -> Sequence[AnyStr]: ...
\r
379 def groupdict(self, default: AnyStr = ...) -> dict[str, AnyStr]: ...
\r
380 def start(self, group: Union[int, str] = ...) -> int: ...
\r
381 def end(self, group: Union[int, str] = ...) -> int: ...
\r
382 def span(self, group: Union[int, str] = ...) -> Tuple[int, int]: ...
\r
384 class Pattern(Generic[AnyStr]):
\r
388 pattern = ... # type: AnyStr
\r
390 def search(self, string: AnyStr, pos: int = ...,
\r
391 endpos: int = ...) -> Optional[Match[AnyStr]]: ...
\r
392 def match(self, string: AnyStr, pos: int = ...,
\r
393 endpos: int = ...) -> Optional[Match[AnyStr]]: ...
\r
394 def split(self, string: AnyStr, maxsplit: int = ...) -> list[AnyStr]: ...
\r
395 def findall(self, string: AnyStr, pos: int = ...,
\r
396 endpos: int = ...) -> list[Any]: ...
\r
397 def finditer(self, string: AnyStr, pos: int = ...,
\r
398 endpos: int = ...) -> Iterator[Match[AnyStr]]: ...
\r
401 def sub(self, repl: AnyStr, string: AnyStr,
\r
402 count: int = ...) -> AnyStr: ...
\r
404 def sub(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr,
\r
405 count: int = ...) -> AnyStr: ...
\r
408 def subn(self, repl: AnyStr, string: AnyStr,
\r
409 count: int = ...) -> Tuple[AnyStr, int]: ...
\r
411 def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr,
\r
412 count: int = ...) -> Tuple[AnyStr, int]: ...
\r
416 def get_type_hints(obj: Callable, globalns: Optional[dict[Text, Any]] = ...,
\r
417 localns: Optional[dict[Text, Any]] = ...) -> None: ...
\r
419 def cast(tp: Type[_T], obj: Any) -> _T: ...
\r
421 # Type constructors
\r
423 # NamedTuple is special-cased in the type checker
\r
424 class NamedTuple(tuple):
\r
425 _fields = ... # type: Tuple[str, ...]
\r
427 def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., *,
\r
428 verbose: bool = ..., rename: bool = ..., **kwargs: Any) -> None: ...
\r
431 def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T: ...
\r
433 def _asdict(self) -> dict: ...
\r
434 def _replace(self: _T, **kwargs: Any) -> _T: ...
\r
436 def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
\r