Unify stdlib/{2,3}/typing.pyi

Also fix signature of IO.seek, IO.truncate, IO.write, and MutableMapping.update
Fixes #1016

Note: I couldn't put typing.pyi in 2and3 because of an import cycle when adding `import sys` to 2/typing.pyi
This commit is contained in:
David Euresti
2017-03-17 16:02:06 -07:00
committed by Łukasz Langa
parent 37a854630c
commit d43f3be914
8 changed files with 55 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
# Stubs for StringIO (Python 2)
from typing import Any, IO, AnyStr, Iterator, Iterable, Generic, List
from typing import Any, IO, AnyStr, Iterator, Iterable, Generic, List, Optional
class StringIO(IO[AnyStr], Generic[AnyStr]):
closed = ... # type: bool
@@ -12,13 +12,13 @@ class StringIO(IO[AnyStr], Generic[AnyStr]):
def next(self) -> AnyStr: ...
def close(self) -> None: ...
def isatty(self) -> bool: ...
def seek(self, pos: int, mode: int = ...) -> None: ...
def seek(self, pos: int, mode: int = ...) -> int: ...
def tell(self) -> int: ...
def read(self, n: int = ...) -> AnyStr: ...
def readline(self, length: int = ...) -> AnyStr: ...
def readlines(self, sizehint: int = ...) -> List[AnyStr]: ...
def truncate(self, size: int = ...) -> int: ...
def write(self, s: AnyStr) -> None: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def write(self, s: AnyStr) -> int: ...
def writelines(self, iterable: Iterable[AnyStr]) -> None: ...
def flush(self) -> None: ...
def getvalue(self) -> AnyStr: ...

View File

@@ -950,13 +950,13 @@ class file(BinaryIO):
def readable(self) -> bool: ...
def writable(self) -> bool: ...
def seekable(self) -> bool: ...
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def tell(self) -> int: ...
def readline(self, limit: int = ...) -> str: ...
def readlines(self, hint: int = ...) -> List[str]: ...
def write(self, data: str) -> None: ...
def write(self, data: str) -> int: ...
def writelines(self, data: Iterable[str]) -> None: ...
def truncate(self, pos: int = ...) -> int: ...
def truncate(self, pos: Optional[int] = ...) -> int: ...
# Very old builtins
def apply(func: Callable[..., _T], args: Sequence[Any] = None, kwds: Mapping[str, Any] = None) -> _T: ...

View File

@@ -16,9 +16,9 @@ class InputType(IO[str], Iterator[str]):
def read(self, size: int = ...) -> str: ...
def readline(self, size: int = ...) -> str: ...
def readlines(self, hint: int = ...) -> List[str]: ...
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def tell(self) -> int: ...
def truncate(self, size: int = ...) -> Optional[int]: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def __iter__(self) -> 'InputType': ...
def next(self) -> str: ...
def reset(self) -> None: ...
@@ -35,13 +35,13 @@ class OutputType(IO[str], Iterator[str]):
def read(self, size: int = ...) -> str: ...
def readline(self, size: int = ...) -> str: ...
def readlines(self, hint: int = ...) -> List[str]: ...
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def tell(self) -> int: ...
def truncate(self, size: int = ...) -> Optional[int]: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def __iter__(self) -> 'OutputType': ...
def next(self) -> str: ...
def reset(self) -> None: ...
def write(self, b: Union[str, unicode]) -> None: ...
def write(self, b: Union[str, unicode]) -> int: ...
def writelines(self, lines: Iterable[Union[str, unicode]]) -> None: ...
@overload

View File

@@ -4,7 +4,7 @@
# Only a subset of functionality is included.
from typing import List, BinaryIO, TextIO, IO, overload, Iterator, Iterable, Any, Union
from typing import List, BinaryIO, TextIO, IO, overload, Iterator, Iterable, Any, Union, Optional
DEFAULT_BUFFER_SIZE = 0
@@ -30,12 +30,12 @@ class BytesIO(BinaryIO):
def readable(self) -> bool: ...
def readline(self, limit: int = ...) -> str: ...
def readlines(self, hint: int = ...) -> List[str]: ...
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
def truncate(self, size: int = ...) -> int: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def writable(self) -> bool: ...
def write(self, s: str) -> None: ...
def write(self, s: str) -> int: ...
def writelines(self, lines: Iterable[str]) -> None: ...
def getvalue(self) -> str: ...
def read1(self) -> str: ...
@@ -59,12 +59,12 @@ class StringIO(TextIO):
def readable(self) -> bool: ...
def readline(self, limit: int = ...) -> unicode: ...
def readlines(self, hint: int = ...) -> List[unicode]: ...
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
def truncate(self, size: int = ...) -> int: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def writable(self) -> bool: ...
def write(self, s: unicode) -> None: ...
def write(self, s: unicode) -> int: ...
def writelines(self, lines: Iterable[unicode]) -> None: ...
def getvalue(self) -> unicode: ...
@@ -89,12 +89,12 @@ class TextIOWrapper(TextIO):
def readable(self) -> bool: ...
def readline(self, limit: int = ...) -> unicode: ...
def readlines(self, hint: int = ...) -> List[unicode]: ...
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
def truncate(self, size: int = ...) -> int: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def writable(self) -> bool: ...
def write(self, s: unicode) -> None: ...
def write(self, s: unicode) -> int: ...
def writelines(self, lines: Iterable[unicode]) -> None: ...
def __iter__(self) -> Iterator[unicode]: ...

View File

@@ -13,11 +13,12 @@ Tuple = object()
Callable = object()
Type = object()
_promote = object()
no_type_check = object()
ClassVar = object()
class GenericMeta(type): ...
# Type aliases
# Type aliases and type constructors
class TypeAlias:
# Class for defining generic aliases for library types.
@@ -57,6 +58,10 @@ class SupportsFloat(metaclass=ABCMeta):
@abstractmethod
def __float__(self) -> float: ...
class SupportsComplex(metaclass=ABCMeta):
@abstractmethod
def __complex__(self) -> complex: ...
class SupportsAbs(Generic[_T]):
@abstractmethod
def __abs__(self) -> _T: ...
@@ -73,6 +78,13 @@ class Sized(metaclass=ABCMeta):
@abstractmethod
def __len__(self) -> int: ...
class Hashable(metaclass=ABCMeta):
# TODO: This is special, in that a subclass of a hashable class may not be hashable
# (for example, list vs. object). It's not obvious how to represent this. This class
# is currently mostly useless for static checking.
@abstractmethod
def __hash__(self) -> int: ...
class Iterable(Generic[_T_co]):
@abstractmethod
def __iter__(self) -> Iterator[_T_co]: ...
@@ -89,7 +101,9 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
def send(self, value: _T_contra) -> _T_co: ...
@abstractmethod
def throw(self, typ: BaseException, val: Any = None, tb: Any = None) -> None: ...
def throw(self, typ: Type[BaseException], val: Optional[BaseException] = None,
# TODO: tb should be TracebackType but that's defined in types
tb: Any = None) -> None: ...
@abstractmethod
def close(self) -> None: ...
@@ -121,8 +135,12 @@ class MutableSequence(Sequence[_T], Generic[_T]):
@overload
@abstractmethod
def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
@overload
@abstractmethod
def __delitem__(self, i: Union[int, slice]) -> None: ...
def __delitem__(self, i: int) -> None: ...
@overload
@abstractmethod
def __delitem__(self, i: slice) -> None: ...
# Mixin methods
def append(self, object: _T) -> None: ...
def extend(self, iterable: Iterable[_T]) -> None: ...
@@ -242,18 +260,18 @@ class IO(Iterator[AnyStr], Generic[AnyStr]):
@abstractmethod
def readlines(self, hint: int = ...) -> list[AnyStr]: ...
@abstractmethod
def seek(self, offset: int, whence: int = ...) -> None: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
@abstractmethod
def seekable(self) -> bool: ...
@abstractmethod
def tell(self) -> int: ...
@abstractmethod
def truncate(self, size: int = ...) -> Optional[int]: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
@abstractmethod
def writable(self) -> bool: ...
# TODO buffer objects
@abstractmethod
def write(self, s: AnyStr) -> None: ...
def write(self, s: AnyStr) -> int: ...
@abstractmethod
def writelines(self, lines: Iterable[AnyStr]) -> None: ...
@@ -290,6 +308,8 @@ class TextIO(IO[unicode]):
@abstractmethod
def __enter__(self) -> TextIO: ...
class ByteString(Sequence[int]): ...
class Match(Generic[AnyStr]):
pos = 0
endpos = 0

View File

@@ -614,9 +614,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = None) -> _VT: ...
@overload
def update(self, m: Mapping[_KT, _VT]) -> None: ...
def update(self, m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
@overload
def update(self, m: Iterable[Tuple[_KT, _VT]]) -> None: ...
def update(self, m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
def keys(self) -> KeysView[_KT]: ...
def values(self) -> ValuesView[_VT]: ...
def items(self) -> ItemsView[_KT, _VT]: ...

View File

@@ -125,9 +125,9 @@ class Counter(Dict[_T, int], Generic[_T]):
# Dict.update. Not sure if we should use '# type: ignore' instead
# and omit the type from the union.
@overload
def update(self, m: Mapping[_T, int]) -> None: ...
def update(self, m: Mapping[_T, int], **kwargs: int) -> None: ...
@overload
def update(self, m: Union[Iterable[_T], Iterable[Tuple[_T, int]]]) -> None: ...
def update(self, m: Union[Iterable[_T], Iterable[Tuple[_T, int]]], **kwargs: int) -> None: ...
def __add__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __sub__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...

View File

@@ -310,9 +310,9 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
# known to be a Mapping with unknown type parameters, which is closer
# to the behavior we want. See mypy issue #1430.
@overload
def update(self, m: Mapping[_KT, _VT]) -> None: ...
def update(self, m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
@overload
def update(self, m: Iterable[Tuple[_KT, _VT]]) -> None: ...
def update(self, m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
Text = str
@@ -350,9 +350,8 @@ class IO(Iterator[AnyStr], Generic[AnyStr]):
def seekable(self) -> bool: ...
@abstractmethod
def tell(self) -> int: ...
# TODO None should not be compatible with int
@abstractmethod
def truncate(self, size: int = ...) -> int: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
@abstractmethod
def writable(self) -> bool: ...
# TODO buffer objects