From 57e0a218f00b03b79ebe0791c42ade1c4ea26728 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Tue, 6 Jun 2017 19:21:08 -0700 Subject: [PATCH 01/10] make io.pyi import _io.py(i), like io.py does --- stdlib/2/_io.pyi | 36 ++++++++++++++--- stdlib/2/io.pyi | 100 +++++++---------------------------------------- 2 files changed, 46 insertions(+), 90 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 959d1d128434..9c7bea3ee81d 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -2,13 +2,11 @@ from typing import Any, BinaryIO, IO, Iterable, Iterator, List, Optional, Type, DEFAULT_BUFFER_SIZE = ... # type: int - class BlockingIOError(IOError): characters_written = ... # type: int class UnsupportedOperation(ValueError, IOError): ... - class _IOBase(BinaryIO): def _checkClosed(self) -> None: ... def _checkReadable(self) -> None: ... @@ -16,6 +14,7 @@ class _IOBase(BinaryIO): def _checkWritable(self) -> None: ... # All these methods are concrete here (you can instantiate this) def close(self) -> None: ... + def closed(self) -> bool: ... def fileno(self) -> int: ... def flush(self) -> None: ... def isatty(self) -> bool: ... @@ -65,9 +64,17 @@ class BufferedWriter(_BufferedIOBase): mode = ... # type: str class BytesIO(_BufferedIOBase): + def __init__(self, initial_bytes: bytes = ...) -> None: ... def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... - def getvalue(self) -> str: ... + def getvalue(self) -> bytes: ... + def writelines(self, lines: Iterable[bytes]) -> None: ... + def getvalue(self) -> bytes: ... + def read1(self) -> bytes: ... + def __iter__(self) -> Iterator[bytes]: ... + def next(self) -> bytes: ... + def __enter__(self) -> 'BytesIO': ... + def __exit__(self, type, value, traceback) -> bool: ... class _RawIOBase(_IOBase): def readall(self) -> str: ... @@ -95,10 +102,17 @@ class _TextIOBase(_IOBase): raise UnsupportedOperation class StringIO(_TextIOBase): + name = ... # type: str line_buffering = ... # type: bool - def getvalue(self) -> str: ... + def __init__(self, initial_value: unicode = ..., + newline: unicode = ...) -> None: ... def __setstate__(self, state: tuple) -> None: ... def __getstate__(self) -> tuple: ... + def getvalue(self) -> str: ... + def __iter__(self) -> Iterator[str]: ... + def next(self) -> str: ... + def __enter__(self) -> 'StringIO': ... + def __exit__(self, type, value, traceback) -> bool: ... class TextIOWrapper(_TextIOBase): name = ... # type: str @@ -106,4 +120,16 @@ class TextIOWrapper(_TextIOBase): buffer = ... # type: str _CHUNK_SIZE = ... # type: int -def open(file: Union[int, str], mode: str = ...) -> _IOBase: ... + def __init__(self, buffer: IO[str], encoding: unicode = ..., + errors: unicode = ..., newline: unicode = ..., + line_buffering: bool = ..., + write_through: bool = ...) -> None: ... + def __iter__(self) -> Iterator[unicode]: ... + def next(self) -> unicode: ... + def __enter__(self) -> StringIO: ... + def __exit__(self, type, value, traceback) -> bool: ... + +def open(file: Union[str, unicode, int], + mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., + errors: unicode = ..., newline: unicode = ..., + closefd: bool = ...) -> IO[Any]: ... diff --git a/stdlib/2/io.pyi b/stdlib/2/io.pyi index 03cd38e0bbcd..45818b7d2196 100644 --- a/stdlib/2/io.pyi +++ b/stdlib/2/io.pyi @@ -7,98 +7,28 @@ from typing import List, BinaryIO, TextIO, IO, overload, Iterator, Iterable, Any, Union, Optional import _io -DEFAULT_BUFFER_SIZE = 0 +from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, + open, FileIO, BytesIO, StringIO, BufferedReader, + BufferedWriter, BufferedRWPair, BufferedRandom, + IncrementalNewlineDecoder, TextIOWrapper) -def open(file: Union[str, unicode, int], +def _OpenWrapper(file: Union[str, unicode, int], mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., errors: unicode = ..., newline: unicode = ..., closefd: bool = ...) -> IO[Any]: ... -class IOBase(_io._IOBase): ... - -class BytesIO(BinaryIO): - def __init__(self, initial_bytes: str = ...) -> None: ... - # TODO getbuffer - # TODO see comments in BinaryIO for missing functionality - def close(self) -> None: ... - def closed(self) -> bool: ... - def fileno(self) -> int: ... - def flush(self) -> None: ... - def isatty(self) -> bool: ... - def read(self, n: int = ...) -> str: ... - def readable(self) -> bool: ... - def readline(self, limit: int = ...) -> str: ... - def readlines(self, hint: int = ...) -> List[str]: ... - def seek(self, offset: int, whence: int = ...) -> int: ... - def seekable(self) -> bool: ... - def tell(self) -> int: ... - def truncate(self, size: Optional[int] = ...) -> int: ... - def writable(self) -> bool: ... - def write(self, s: str) -> int: ... - def writelines(self, lines: Iterable[str]) -> None: ... - def getvalue(self) -> str: ... - def read1(self) -> str: ... +SEEK_SET = ... # type: int +SEEK_CUR = ... # type: int +SEEK_END = ... # type: int - def __iter__(self) -> Iterator[str]: ... - def next(self) -> str: ... - def __enter__(self) -> 'BytesIO': ... - def __exit__(self, type, value, traceback) -> bool: ... -class StringIO(TextIO): - def __init__(self, initial_value: unicode = ..., - newline: unicode = ...) -> None: ... - # TODO see comments in BinaryIO for missing functionality - name = ... # type: str - def close(self) -> None: ... - def closed(self) -> bool: ... - def fileno(self) -> int: ... - def flush(self) -> None: ... - def isatty(self) -> bool: ... - def read(self, n: int = ...) -> unicode: ... - def readable(self) -> bool: ... - def readline(self, limit: int = ...) -> unicode: ... - def readlines(self, hint: int = ...) -> List[unicode]: ... - def seek(self, offset: int, whence: int = ...) -> int: ... - def seekable(self) -> bool: ... - def tell(self) -> int: ... - def truncate(self, size: Optional[int] = ...) -> int: ... - def writable(self) -> bool: ... - def write(self, s: unicode) -> int: ... - def writelines(self, lines: Iterable[unicode]) -> None: ... - def getvalue(self) -> unicode: ... - - def __iter__(self) -> Iterator[unicode]: ... - def next(self) -> unicode: ... - def __enter__(self) -> 'StringIO': ... - def __exit__(self, type, value, traceback) -> bool: ... +class IOBase(_io._IOBase): ... -class TextIOWrapper(TextIO): - # write_through is undocumented but used by subprocess - def __init__(self, buffer: IO[str], encoding: unicode = ..., - errors: unicode = ..., newline: unicode = ..., - line_buffering: bool = ..., - write_through: bool = ...) -> None: ... - # TODO see comments in BinaryIO for missing functionality - def close(self) -> None: ... - def closed(self) -> bool: ... - def fileno(self) -> int: ... - def flush(self) -> None: ... - def isatty(self) -> bool: ... - def read(self, n: int = ...) -> unicode: ... - def readable(self) -> bool: ... - def readline(self, limit: int = ...) -> unicode: ... - def readlines(self, hint: int = ...) -> List[unicode]: ... - def seek(self, offset: int, whence: int = ...) -> int: ... - def seekable(self) -> bool: ... - def tell(self) -> int: ... - def truncate(self, size: Optional[int] = ...) -> int: ... - def writable(self) -> bool: ... - def write(self, s: unicode) -> int: ... - def writelines(self, lines: Iterable[unicode]) -> None: ... +class RawIOBase(_io._RawIOBase, IOBase): + pass - def __iter__(self) -> Iterator[unicode]: ... - def next(self) -> unicode: ... - def __enter__(self) -> StringIO: ... - def __exit__(self, type, value, traceback) -> bool: ... +class BufferedIOBase(_io._BufferedIOBase, IOBase): + pass -class BufferedIOBase(_io._BufferedIOBase, IOBase): ... +class TextIOBase(_io._TextIOBase, IOBase): + pass From 34ce366a02d3dc386b138d0c5651bddbcb519e15 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Tue, 6 Jun 2017 19:27:35 -0700 Subject: [PATCH 02/10] make write/writelines take 'Any', on _IOBase --- stdlib/2/_io.pyi | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 9c7bea3ee81d..cdaf88f8b6df 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -27,8 +27,8 @@ class _IOBase(BinaryIO): def tell(self) -> int: ... def truncate(self, size: Optional[int] = ...) -> int: ... def writable(self) -> bool: ... - def write(self, s: bytes) -> int: ... - def writelines(self, lines: Iterable[bytes]) -> None: ... + def write(self, s: Any) -> int: ... + def writelines(self, lines: Iterable[Any]) -> None: ... def next(self) -> bytes: ... def __iter__(self) -> Iterator[bytes]: ... def __enter__(self) -> '_IOBase': ... @@ -40,7 +40,8 @@ class _BufferedIOBase(_IOBase): def read1(self, n: int) -> str: ... def read(self, n: int = ...) -> str: ... def readinto(self, buffer: bytearray) -> int: ... - def write(self, s: str) -> int: ... + def write(self, s: Any) -> int: ... + def writelines(self, lines: Iterable[Any]) -> None: ... def detach(self) -> "_BufferedIOBase": ... class BufferedRWPair(_BufferedIOBase): @@ -68,6 +69,7 @@ class BytesIO(_BufferedIOBase): def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... def getvalue(self) -> bytes: ... + def write(self, s: bytes) -> int: ... def writelines(self, lines: Iterable[bytes]) -> None: ... def getvalue(self) -> bytes: ... def read1(self) -> bytes: ... @@ -113,6 +115,8 @@ class StringIO(_TextIOBase): def next(self) -> str: ... def __enter__(self) -> 'StringIO': ... def __exit__(self, type, value, traceback) -> bool: ... + def write(self, s: str) -> int: ... + def writelines(self, lines: Iterable[str]) -> None: ... class TextIOWrapper(_TextIOBase): name = ... # type: str @@ -128,6 +132,8 @@ class TextIOWrapper(_TextIOBase): def next(self) -> unicode: ... def __enter__(self) -> StringIO: ... def __exit__(self, type, value, traceback) -> bool: ... + def write(self, s: unicode) -> int: ... + def writelines(self, lines: Iterable[unicode]) -> None: ... def open(file: Union[str, unicode, int], mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., From 44371bb7c2053e8f73d12a92711b2a9df9c527e6 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Fri, 9 Jun 2017 10:25:46 -0700 Subject: [PATCH 03/10] fix mypy warnings --- stdlib/2/_io.pyi | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index cdaf88f8b6df..a0617b388eaa 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -27,8 +27,8 @@ class _IOBase(BinaryIO): def tell(self) -> int: ... def truncate(self, size: Optional[int] = ...) -> int: ... def writable(self) -> bool: ... - def write(self, s: Any) -> int: ... - def writelines(self, lines: Iterable[Any]) -> None: ... + def write(self, s: bytes) -> int: ... + def writelines(self, lines: Iterable[bytes]) -> None: ... def next(self) -> bytes: ... def __iter__(self) -> Iterator[bytes]: ... def __enter__(self) -> '_IOBase': ... @@ -37,8 +37,8 @@ class _IOBase(BinaryIO): traceback: Optional[Any]) -> bool: ... class _BufferedIOBase(_IOBase): - def read1(self, n: int) -> str: ... - def read(self, n: int = ...) -> str: ... + def read1(self) -> Any: ... + def read(self, n: int = ...) -> Any: ... def readinto(self, buffer: bytearray) -> int: ... def write(self, s: Any) -> int: ... def writelines(self, lines: Iterable[Any]) -> None: ... @@ -71,7 +71,6 @@ class BytesIO(_BufferedIOBase): def getvalue(self) -> bytes: ... def write(self, s: bytes) -> int: ... def writelines(self, lines: Iterable[bytes]) -> None: ... - def getvalue(self) -> bytes: ... def read1(self) -> bytes: ... def __iter__(self) -> Iterator[bytes]: ... def next(self) -> bytes: ... @@ -99,7 +98,7 @@ class _TextIOBase(_IOBase): errors = ... # type: Optional[str] newlines = ... # type: Union[str, unicode] encoding = ... # type: Optional[str] - def read(self, n: int = ...) -> str: ... + def read(self, n: int = ...) -> unicode: ... # type: ignore def detach(self) -> None: raise UnsupportedOperation @@ -110,13 +109,13 @@ class StringIO(_TextIOBase): newline: unicode = ...) -> None: ... def __setstate__(self, state: tuple) -> None: ... def __getstate__(self) -> tuple: ... - def getvalue(self) -> str: ... - def __iter__(self) -> Iterator[str]: ... - def next(self) -> str: ... + def getvalue(self) -> unicode: ... + def __iter__(self) -> Iterator[unicode]: ... # type: ignore + def next(self) -> unicode: ... # type: ignore def __enter__(self) -> 'StringIO': ... def __exit__(self, type, value, traceback) -> bool: ... - def write(self, s: str) -> int: ... - def writelines(self, lines: Iterable[str]) -> None: ... + def write(self, s: unicode) -> int: ... + def writelines(self, lines: Iterable[unicode]) -> None: ... class TextIOWrapper(_TextIOBase): name = ... # type: str @@ -128,8 +127,8 @@ class TextIOWrapper(_TextIOBase): errors: unicode = ..., newline: unicode = ..., line_buffering: bool = ..., write_through: bool = ...) -> None: ... - def __iter__(self) -> Iterator[unicode]: ... - def next(self) -> unicode: ... + def __iter__(self) -> Iterator[unicode]: ... # type: ignore + def next(self) -> unicode: ... # type: ignore def __enter__(self) -> StringIO: ... def __exit__(self, type, value, traceback) -> bool: ... def write(self, s: unicode) -> int: ... From 0acb51a3ed61ed16e1f23b681613927b091c4a2d Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Fri, 9 Jun 2017 14:37:00 -0700 Subject: [PATCH 04/10] Add missing constructors, fix inconsistencies. Also, as far as possible, try to simplify, by moving methods into base classes. --- stdlib/2/_io.pyi | 91 ++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index a0617b388eaa..95c0151fcbaf 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -1,4 +1,4 @@ -from typing import Any, BinaryIO, IO, Iterable, Iterator, List, Optional, Type, Tuple, Union +from typing import Any, AnyStr, BinaryIO, IO, TextIO, Iterable, Iterator, List, Optional, Type, Tuple, Union DEFAULT_BUFFER_SIZE = ... # type: int @@ -7,71 +7,77 @@ class BlockingIOError(IOError): class UnsupportedOperation(ValueError, IOError): ... -class _IOBase(BinaryIO): +class _IOBase(object): + @property + def closed(self) -> bool: ... def _checkClosed(self) -> None: ... def _checkReadable(self) -> None: ... def _checkSeekable(self) -> None: ... def _checkWritable(self) -> None: ... # All these methods are concrete here (you can instantiate this) def close(self) -> None: ... - def closed(self) -> bool: ... def fileno(self) -> int: ... def flush(self) -> None: ... def isatty(self) -> bool: ... - def read(self, n: int = ...) -> bytes: ... def readable(self) -> bool: ... - def readline(self, limit: int = ...) -> bytes: ... - def readlines(self, hint: int = ...) -> list[bytes]: ... def seek(self, offset: int, whence: int = ...) -> int: ... def seekable(self) -> bool: ... def tell(self) -> int: ... def truncate(self, size: Optional[int] = ...) -> int: ... def writable(self) -> bool: ... - def write(self, s: bytes) -> int: ... - def writelines(self, lines: Iterable[bytes]) -> None: ... - def next(self) -> bytes: ... - def __iter__(self) -> Iterator[bytes]: ... - def __enter__(self) -> '_IOBase': ... - def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], - # TODO: traceback should be TracebackType but that's defined in types - traceback: Optional[Any]) -> bool: ... + def __exit__(self, type, value, traceback) -> bool: ... + # This just returns self: + def __iter__(self) -> Iterator[Any]: ... + # The parameter type of writelines[s]() is determined by that of write(): + def writelines(self, lines: Iterable[AnyStr]) -> None: ... + # The return type of readline[s]() and next() is determined by that of read(): + def readline(self, limit: int = ...) -> Any: ... + def readlines(self, hint: int = ...) -> list[AnyStr]: ... + def next(self) -> Any: ... class _BufferedIOBase(_IOBase): - def read1(self) -> Any: ... - def read(self, n: int = ...) -> Any: ... + def read1(self, size: int) -> Any: ... + def read(self, size: int = ...) -> Any: ... def readinto(self, buffer: bytearray) -> int: ... def write(self, s: Any) -> int: ... - def writelines(self, lines: Iterable[Any]) -> None: ... def detach(self) -> "_BufferedIOBase": ... class BufferedRWPair(_BufferedIOBase): - def peek(self, n: int = ...) -> str: ... + def __init__(self, reader: _RawIOBase, writer: _RawIOBase) -> None: ... + def peek(self, n: int = ...) -> Any: ... class BufferedRandom(_BufferedIOBase): + mode = ... # type: str name = ... # type: str raw = ... # type: _IOBase - mode = ... # type: str - def peek(self, n: int = ...) -> str: ... + def __init__(self, raw: _IOBase, + buffer_size: int = ..., + max_buffer_size: int = ...) -> None: ... + def peek(self, n: int = ...) -> bytes: ... class BufferedReader(_BufferedIOBase): + mode = ... # type: str name = ... # type: str raw = ... # type: _IOBase - mode = ... # type: str - def peek(self, n: int = ...) -> str: ... + def __init__(self, raw: _IOBase, buffer_size: int = ...) -> None: ... + def peek(self, n: int = ...) -> bytes: ... class BufferedWriter(_BufferedIOBase): name = ... # type: str raw = ... # type: _IOBase mode = ... # type: str + def __init__(self, raw: _IOBase, + buffer_size: int = ..., + max_buffer_size: int = ...) -> None: ... -class BytesIO(_BufferedIOBase): +class BytesIO(_BufferedIOBase, BinaryIO): def __init__(self, initial_bytes: bytes = ...) -> None: ... def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... def getvalue(self) -> bytes: ... def write(self, s: bytes) -> int: ... - def writelines(self, lines: Iterable[bytes]) -> None: ... - def read1(self) -> bytes: ... + def writelines(self, lines: Iterable[bytes]) -> None: ... # type: ignore + def read1(self, size: int) -> bytes: ... def __iter__(self) -> Iterator[bytes]: ... def next(self) -> bytes: ... def __enter__(self) -> 'BytesIO': ... @@ -79,60 +85,53 @@ class BytesIO(_BufferedIOBase): class _RawIOBase(_IOBase): def readall(self) -> str: ... - def read(self, n: int = ...) -> str: ... + def read(self, size: int = ...) -> str: ... # type: ignore -class FileIO(_RawIOBase): +class FileIO(_RawIOBase, BytesIO): # type: ignore mode = ... # type: str closefd = ... # type: bool + def __init__(self, file: str, mode: str = ...) -> None: ... def readinto(self, buffer: bytearray)-> int: ... def write(self, pbuf: str) -> int: ... class IncrementalNewlineDecoder(object): newlines = ... # type: Union[str, unicode] + def __init__(self, decoder, translate, z = ...) -> None: ... def decode(self, input, final) -> Any: ... def getstate(self) -> Tuple[Any, int]: ... def setstate(self, state: Tuple[Any, int]) -> None: ... def reset(self) -> None: ... -class _TextIOBase(_IOBase): +class _TextIOBase(_IOBase, TextIO): errors = ... # type: Optional[str] newlines = ... # type: Union[str, unicode] encoding = ... # type: Optional[str] - def read(self, n: int = ...) -> unicode: ... # type: ignore - def detach(self) -> None: - raise UnsupportedOperation + def __iter__(self) -> Iterator[unicode]: ... # type: ignore + def next(self) -> unicode: ... # type: ignore + def read(self, size: int = ...) -> unicode: ... # type: ignore + def write(self, pbuf: unicode) -> int: ... + def writelines(self, lines: Iterable[unicode]) -> None: ... # type: ignore + def detach(self) -> None: ... class StringIO(_TextIOBase): - name = ... # type: str line_buffering = ... # type: bool def __init__(self, initial_value: unicode = ..., newline: unicode = ...) -> None: ... def __setstate__(self, state: tuple) -> None: ... def __getstate__(self) -> tuple: ... def getvalue(self) -> unicode: ... - def __iter__(self) -> Iterator[unicode]: ... # type: ignore - def next(self) -> unicode: ... # type: ignore def __enter__(self) -> 'StringIO': ... - def __exit__(self, type, value, traceback) -> bool: ... - def write(self, s: unicode) -> int: ... - def writelines(self, lines: Iterable[unicode]) -> None: ... class TextIOWrapper(_TextIOBase): name = ... # type: str line_buffering = ... # type: bool - buffer = ... # type: str + buffer = ... # type: BinaryIO _CHUNK_SIZE = ... # type: int - - def __init__(self, buffer: IO[str], encoding: unicode = ..., + def __init__(self, buffer: IO[unicode], encoding: unicode = ..., errors: unicode = ..., newline: unicode = ..., line_buffering: bool = ..., write_through: bool = ...) -> None: ... - def __iter__(self) -> Iterator[unicode]: ... # type: ignore - def next(self) -> unicode: ... # type: ignore - def __enter__(self) -> StringIO: ... - def __exit__(self, type, value, traceback) -> bool: ... - def write(self, s: unicode) -> int: ... - def writelines(self, lines: Iterable[unicode]) -> None: ... + def __enter__(self) -> 'TextIOWrapper': ... def open(file: Union[str, unicode, int], mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., From 58cc0ae8a71cbfaf520621d2c2324f4fbc4fb8cd Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Fri, 9 Jun 2017 15:12:59 -0700 Subject: [PATCH 05/10] fix lint+mypy warnings --- stdlib/2/_io.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 95c0151fcbaf..97da2b8934d4 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -96,7 +96,7 @@ class FileIO(_RawIOBase, BytesIO): # type: ignore class IncrementalNewlineDecoder(object): newlines = ... # type: Union[str, unicode] - def __init__(self, decoder, translate, z = ...) -> None: ... + def __init__(self, decoder, translate, z=...) -> None: ... def decode(self, input, final) -> Any: ... def getstate(self) -> Tuple[Any, int]: ... def setstate(self, state: Tuple[Any, int]) -> None: ... @@ -105,7 +105,7 @@ class IncrementalNewlineDecoder(object): class _TextIOBase(_IOBase, TextIO): errors = ... # type: Optional[str] newlines = ... # type: Union[str, unicode] - encoding = ... # type: Optional[str] + encoding = ... # type: str def __iter__(self) -> Iterator[unicode]: ... # type: ignore def next(self) -> unicode: ... # type: ignore def read(self, size: int = ...) -> unicode: ... # type: ignore @@ -127,7 +127,7 @@ class TextIOWrapper(_TextIOBase): line_buffering = ... # type: bool buffer = ... # type: BinaryIO _CHUNK_SIZE = ... # type: int - def __init__(self, buffer: IO[unicode], encoding: unicode = ..., + def __init__(self, buffer: IO, encoding: unicode = ..., errors: unicode = ..., newline: unicode = ..., line_buffering: bool = ..., write_through: bool = ...) -> None: ... From 7dc8830de3c05b830dfb37f6700c35c494f5add9 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Mon, 12 Jun 2017 08:43:06 -0700 Subject: [PATCH 06/10] add missing __enter__ methods --- stdlib/2/_io.pyi | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 97da2b8934d4..df4d78628701 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -41,10 +41,12 @@ class _BufferedIOBase(_IOBase): def readinto(self, buffer: bytearray) -> int: ... def write(self, s: Any) -> int: ... def detach(self) -> "_BufferedIOBase": ... + def __enter__(self) -> '_BufferedIOBase': ... class BufferedRWPair(_BufferedIOBase): def __init__(self, reader: _RawIOBase, writer: _RawIOBase) -> None: ... def peek(self, n: int = ...) -> Any: ... + def __enter__(self) -> 'BufferedRWPair': ... class BufferedRandom(_BufferedIOBase): mode = ... # type: str @@ -54,6 +56,7 @@ class BufferedRandom(_BufferedIOBase): buffer_size: int = ..., max_buffer_size: int = ...) -> None: ... def peek(self, n: int = ...) -> bytes: ... + def __enter__(self) -> 'BufferedRandom': ... class BufferedReader(_BufferedIOBase): mode = ... # type: str @@ -61,6 +64,7 @@ class BufferedReader(_BufferedIOBase): raw = ... # type: _IOBase def __init__(self, raw: _IOBase, buffer_size: int = ...) -> None: ... def peek(self, n: int = ...) -> bytes: ... + def __enter__(self) -> 'BufferedReader': ... class BufferedWriter(_BufferedIOBase): name = ... # type: str @@ -69,8 +73,9 @@ class BufferedWriter(_BufferedIOBase): def __init__(self, raw: _IOBase, buffer_size: int = ..., max_buffer_size: int = ...) -> None: ... + def __enter__(self) -> 'BufferedWriter': ... -class BytesIO(_BufferedIOBase, BinaryIO): +class BytesIO(_BufferedIOBase, BinaryIO): # type: ignore def __init__(self, initial_bytes: bytes = ...) -> None: ... def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... @@ -81,11 +86,11 @@ class BytesIO(_BufferedIOBase, BinaryIO): def __iter__(self) -> Iterator[bytes]: ... def next(self) -> bytes: ... def __enter__(self) -> 'BytesIO': ... - def __exit__(self, type, value, traceback) -> bool: ... class _RawIOBase(_IOBase): def readall(self) -> str: ... def read(self, size: int = ...) -> str: ... # type: ignore + def __enter__(self) -> '_RawIOBase': ... class FileIO(_RawIOBase, BytesIO): # type: ignore mode = ... # type: str @@ -93,6 +98,7 @@ class FileIO(_RawIOBase, BytesIO): # type: ignore def __init__(self, file: str, mode: str = ...) -> None: ... def readinto(self, buffer: bytearray)-> int: ... def write(self, pbuf: str) -> int: ... + def __enter__(self) -> 'FileIO': ... class IncrementalNewlineDecoder(object): newlines = ... # type: Union[str, unicode] @@ -112,6 +118,7 @@ class _TextIOBase(_IOBase, TextIO): def write(self, pbuf: unicode) -> int: ... def writelines(self, lines: Iterable[unicode]) -> None: ... # type: ignore def detach(self) -> None: ... + def __enter__(self) -> '_TextIOBase': ... class StringIO(_TextIOBase): line_buffering = ... # type: bool From 43885273373b1d9d362966649c8480a8880cf854 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Mon, 12 Jun 2017 08:57:26 -0700 Subject: [PATCH 07/10] make _IOBase inherit from BinaryIO --- stdlib/2/_io.pyi | 6 +++--- stdlib/2/io.pyi | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index df4d78628701..58ff1caf372a 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -7,7 +7,7 @@ class BlockingIOError(IOError): class UnsupportedOperation(ValueError, IOError): ... -class _IOBase(object): +class _IOBase(BinaryIO): @property def closed(self) -> bool: ... def _checkClosed(self) -> None: ... @@ -75,7 +75,7 @@ class BufferedWriter(_BufferedIOBase): max_buffer_size: int = ...) -> None: ... def __enter__(self) -> 'BufferedWriter': ... -class BytesIO(_BufferedIOBase, BinaryIO): # type: ignore +class BytesIO(_BufferedIOBase): # type: ignore def __init__(self, initial_bytes: bytes = ...) -> None: ... def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... @@ -108,7 +108,7 @@ class IncrementalNewlineDecoder(object): def setstate(self, state: Tuple[Any, int]) -> None: ... def reset(self) -> None: ... -class _TextIOBase(_IOBase, TextIO): +class _TextIOBase(_IOBase, TextIO): # type: ignore errors = ... # type: Optional[str] newlines = ... # type: Union[str, unicode] encoding = ... # type: str diff --git a/stdlib/2/io.pyi b/stdlib/2/io.pyi index 45818b7d2196..d782fd4f37dd 100644 --- a/stdlib/2/io.pyi +++ b/stdlib/2/io.pyi @@ -30,5 +30,5 @@ class RawIOBase(_io._RawIOBase, IOBase): class BufferedIOBase(_io._BufferedIOBase, IOBase): pass -class TextIOBase(_io._TextIOBase, IOBase): +class TextIOBase(_io._TextIOBase, IOBase): # type: ignore pass From ffff66a9d4076e7c64361292a3409c023c2dcb2b Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Mon, 12 Jun 2017 14:16:19 -0700 Subject: [PATCH 08/10] make _TextIOBase not subclass _IOBase --- stdlib/2/_io.pyi | 59 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index 58ff1caf372a..be3b1eba0ac9 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -27,25 +27,25 @@ class _IOBase(BinaryIO): def writable(self) -> bool: ... def __exit__(self, type, value, traceback) -> bool: ... # This just returns self: - def __iter__(self) -> Iterator[Any]: ... + def __iter__(self) -> Iterator[bytes]: ... # The parameter type of writelines[s]() is determined by that of write(): - def writelines(self, lines: Iterable[AnyStr]) -> None: ... + def writelines(self, lines: Iterable[bytes]) -> None: ... # The return type of readline[s]() and next() is determined by that of read(): - def readline(self, limit: int = ...) -> Any: ... - def readlines(self, hint: int = ...) -> list[AnyStr]: ... - def next(self) -> Any: ... + def readline(self, limit: int = ...) -> bytes: ... + def readlines(self, hint: int = ...) -> list[bytes]: ... + def next(self) -> bytes: ... class _BufferedIOBase(_IOBase): - def read1(self, size: int) -> Any: ... - def read(self, size: int = ...) -> Any: ... + def read1(self, size: int) -> bytes: ... + def read(self, size: int = ...) -> bytes: ... def readinto(self, buffer: bytearray) -> int: ... - def write(self, s: Any) -> int: ... + def write(self, s: bytes) -> int: ... def detach(self) -> "_BufferedIOBase": ... def __enter__(self) -> '_BufferedIOBase': ... class BufferedRWPair(_BufferedIOBase): def __init__(self, reader: _RawIOBase, writer: _RawIOBase) -> None: ... - def peek(self, n: int = ...) -> Any: ... + def peek(self, n: int = ...) -> bytes: ... def __enter__(self) -> 'BufferedRWPair': ... class BufferedRandom(_BufferedIOBase): @@ -75,13 +75,13 @@ class BufferedWriter(_BufferedIOBase): max_buffer_size: int = ...) -> None: ... def __enter__(self) -> 'BufferedWriter': ... -class BytesIO(_BufferedIOBase): # type: ignore +class BytesIO(_BufferedIOBase): def __init__(self, initial_bytes: bytes = ...) -> None: ... def __setstate__(self, tuple) -> None: ... def __getstate__(self) -> tuple: ... def getvalue(self) -> bytes: ... def write(self, s: bytes) -> int: ... - def writelines(self, lines: Iterable[bytes]) -> None: ... # type: ignore + def writelines(self, lines: Iterable[bytes]) -> None: ... def read1(self, size: int) -> bytes: ... def __iter__(self) -> Iterator[bytes]: ... def next(self) -> bytes: ... @@ -89,10 +89,10 @@ class BytesIO(_BufferedIOBase): # type: ignore class _RawIOBase(_IOBase): def readall(self) -> str: ... - def read(self, size: int = ...) -> str: ... # type: ignore + def read(self, size: int = ...) -> str: ... def __enter__(self) -> '_RawIOBase': ... -class FileIO(_RawIOBase, BytesIO): # type: ignore +class FileIO(_RawIOBase, BytesIO): # type: ignore # for __enter__ mode = ... # type: str closefd = ... # type: bool def __init__(self, file: str, mode: str = ...) -> None: ... @@ -108,17 +108,38 @@ class IncrementalNewlineDecoder(object): def setstate(self, state: Tuple[Any, int]) -> None: ... def reset(self) -> None: ... -class _TextIOBase(_IOBase, TextIO): # type: ignore + +# Note: In the actual _io.py, _TextIOBase inherits from _IOBase. +class _TextIOBase(TextIO): errors = ... # type: Optional[str] newlines = ... # type: Union[str, unicode] encoding = ... # type: str - def __iter__(self) -> Iterator[unicode]: ... # type: ignore - def next(self) -> unicode: ... # type: ignore - def read(self, size: int = ...) -> unicode: ... # type: ignore - def write(self, pbuf: unicode) -> int: ... - def writelines(self, lines: Iterable[unicode]) -> None: ... # type: ignore + @property + def closed(self) -> bool: ... + def _checkClosed(self) -> None: ... + def _checkReadable(self) -> None: ... + def _checkSeekable(self) -> None: ... + def _checkWritable(self) -> None: ... + def close(self) -> None: ... def detach(self) -> None: ... + def fileno(self) -> int: ... + def flush(self) -> None: ... + def isatty(self) -> bool: ... + def next(self) -> unicode: ... + def read(self, size: int = ...) -> unicode: ... + def readable(self) -> bool: ... + def readline(self, limit: int = ...) -> unicode: ... + def readlines(self, hint: int = ...) -> list[unicode]: ... + def seek(self, offset: int, whence: int = ...) -> int: ... + def seekable(self) -> bool: ... + def tell(self) -> int: ... + def truncate(self, size: Optional[int] = ...) -> int: ... + def writable(self) -> bool: ... + def write(self, pbuf: unicode) -> int: ... + def writelines(self, lines: Iterable[unicode]) -> None: ... def __enter__(self) -> '_TextIOBase': ... + def __exit__(self, type, value, traceback) -> bool: ... + def __iter__(self) -> Iterator[unicode]: ... class StringIO(_TextIOBase): line_buffering = ... # type: bool From b75b47a1d4bdbc6dbe518fc3e57d865aaa9d8119 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Tue, 13 Jun 2017 08:33:49 -0700 Subject: [PATCH 09/10] address review comments --- stdlib/2/_io.pyi | 61 ++++++++++++++++++++++++------------------------ stdlib/2/io.pyi | 17 ++++++++++---- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index be3b1eba0ac9..b22342ab4e1f 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -1,4 +1,5 @@ -from typing import Any, AnyStr, BinaryIO, IO, TextIO, Iterable, Iterator, List, Optional, Type, Tuple, Union +from typing import Any, AnyStr, BinaryIO, IO, Text, TextIO, Iterable, Iterator, List, Optional, Type, Tuple, TypeVar, Union +from types import TracebackType DEFAULT_BUFFER_SIZE = ... # type: int @@ -7,6 +8,8 @@ class BlockingIOError(IOError): class UnsupportedOperation(ValueError, IOError): ... +_T = TypeVar("_T") + class _IOBase(BinaryIO): @property def closed(self) -> bool: ... @@ -25,9 +28,9 @@ class _IOBase(BinaryIO): def tell(self) -> int: ... def truncate(self, size: Optional[int] = ...) -> int: ... def writable(self) -> bool: ... - def __exit__(self, type, value, traceback) -> bool: ... - # This just returns self: - def __iter__(self) -> Iterator[bytes]: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[Any]) -> bool: ... + def __iter__(self: _T) -> _T: ... # The parameter type of writelines[s]() is determined by that of write(): def writelines(self, lines: Iterable[bytes]) -> None: ... # The return type of readline[s]() and next() is determined by that of read(): @@ -36,17 +39,17 @@ class _IOBase(BinaryIO): def next(self) -> bytes: ... class _BufferedIOBase(_IOBase): - def read1(self, size: int) -> bytes: ... + def read1(self, n: int) -> bytes: ... def read(self, size: int = ...) -> bytes: ... def readinto(self, buffer: bytearray) -> int: ... def write(self, s: bytes) -> int: ... - def detach(self) -> "_BufferedIOBase": ... - def __enter__(self) -> '_BufferedIOBase': ... + def detach(self) -> _IOBase: ... class BufferedRWPair(_BufferedIOBase): - def __init__(self, reader: _RawIOBase, writer: _RawIOBase) -> None: ... + def __init__(self, reader: _RawIOBase, writer: _RawIOBase, + buffer_size: int = ..., max_buffer_size: int = ...) -> None: ... def peek(self, n: int = ...) -> bytes: ... - def __enter__(self) -> 'BufferedRWPair': ... + def __enter__(self) -> BufferedRWPair: ... class BufferedRandom(_BufferedIOBase): mode = ... # type: str @@ -56,7 +59,6 @@ class BufferedRandom(_BufferedIOBase): buffer_size: int = ..., max_buffer_size: int = ...) -> None: ... def peek(self, n: int = ...) -> bytes: ... - def __enter__(self) -> 'BufferedRandom': ... class BufferedReader(_BufferedIOBase): mode = ... # type: str @@ -64,7 +66,6 @@ class BufferedReader(_BufferedIOBase): raw = ... # type: _IOBase def __init__(self, raw: _IOBase, buffer_size: int = ...) -> None: ... def peek(self, n: int = ...) -> bytes: ... - def __enter__(self) -> 'BufferedReader': ... class BufferedWriter(_BufferedIOBase): name = ... # type: str @@ -73,7 +74,6 @@ class BufferedWriter(_BufferedIOBase): def __init__(self, raw: _IOBase, buffer_size: int = ..., max_buffer_size: int = ...) -> None: ... - def __enter__(self) -> 'BufferedWriter': ... class BytesIO(_BufferedIOBase): def __init__(self, initial_bytes: bytes = ...) -> None: ... @@ -83,14 +83,11 @@ class BytesIO(_BufferedIOBase): def write(self, s: bytes) -> int: ... def writelines(self, lines: Iterable[bytes]) -> None: ... def read1(self, size: int) -> bytes: ... - def __iter__(self) -> Iterator[bytes]: ... def next(self) -> bytes: ... - def __enter__(self) -> 'BytesIO': ... class _RawIOBase(_IOBase): def readall(self) -> str: ... - def read(self, size: int = ...) -> str: ... - def __enter__(self) -> '_RawIOBase': ... + def read(self, n: int = ...) -> str: ... class FileIO(_RawIOBase, BytesIO): # type: ignore # for __enter__ mode = ... # type: str @@ -98,7 +95,6 @@ class FileIO(_RawIOBase, BytesIO): # type: ignore # for __enter__ def __init__(self, file: str, mode: str = ...) -> None: ... def readinto(self, buffer: bytearray)-> int: ... def write(self, pbuf: str) -> int: ... - def __enter__(self) -> 'FileIO': ... class IncrementalNewlineDecoder(object): newlines = ... # type: Union[str, unicode] @@ -112,7 +108,8 @@ class IncrementalNewlineDecoder(object): # Note: In the actual _io.py, _TextIOBase inherits from _IOBase. class _TextIOBase(TextIO): errors = ... # type: Optional[str] - newlines = ... # type: Union[str, unicode] + # TODO: On _TextIOBase, this is always None. But it's unicode/bytes in subclasses. + newlines = ... # type: Union[None, unicode, bytes] encoding = ... # type: str @property def closed(self) -> bool: ... @@ -121,7 +118,7 @@ class _TextIOBase(TextIO): def _checkSeekable(self) -> None: ... def _checkWritable(self) -> None: ... def close(self) -> None: ... - def detach(self) -> None: ... + def detach(self) -> IO: ... def fileno(self) -> int: ... def flush(self) -> None: ... def isatty(self) -> bool: ... @@ -137,31 +134,35 @@ class _TextIOBase(TextIO): def writable(self) -> bool: ... def write(self, pbuf: unicode) -> int: ... def writelines(self, lines: Iterable[unicode]) -> None: ... - def __enter__(self) -> '_TextIOBase': ... - def __exit__(self, type, value, traceback) -> bool: ... - def __iter__(self) -> Iterator[unicode]: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[Any]) -> bool: ... + def __iter__(self: _T) -> _T: ... class StringIO(_TextIOBase): line_buffering = ... # type: bool - def __init__(self, initial_value: unicode = ..., - newline: unicode = ...) -> None: ... + def __init__(self, + initial_value: Optional[unicode] = ..., + newline: Optional[unicode] = ...) -> None: ... def __setstate__(self, state: tuple) -> None: ... def __getstate__(self) -> tuple: ... def getvalue(self) -> unicode: ... - def __enter__(self) -> 'StringIO': ... class TextIOWrapper(_TextIOBase): name = ... # type: str line_buffering = ... # type: bool buffer = ... # type: BinaryIO _CHUNK_SIZE = ... # type: int - def __init__(self, buffer: IO, encoding: unicode = ..., - errors: unicode = ..., newline: unicode = ..., + def __init__(self, buffer: IO, + encoding: Optional[Text] = ..., + errors: Optional[Text] = ..., + newline: Optional[Text] = ..., line_buffering: bool = ..., write_through: bool = ...) -> None: ... - def __enter__(self) -> 'TextIOWrapper': ... def open(file: Union[str, unicode, int], - mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., - errors: unicode = ..., newline: unicode = ..., + mode: unicode = ..., + buffering: int = ..., + encoding: Optional[Text] = ..., + errors: Optional[Text] = ..., + newline: Optional[Text] = ..., closefd: bool = ...) -> IO[Any]: ... diff --git a/stdlib/2/io.pyi b/stdlib/2/io.pyi index d782fd4f37dd..0c01b2b15342 100644 --- a/stdlib/2/io.pyi +++ b/stdlib/2/io.pyi @@ -7,10 +7,19 @@ from typing import List, BinaryIO, TextIO, IO, overload, Iterator, Iterable, Any, Union, Optional import _io -from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, - open, FileIO, BytesIO, StringIO, BufferedReader, - BufferedWriter, BufferedRWPair, BufferedRandom, - IncrementalNewlineDecoder, TextIOWrapper) +from _io import BlockingIOError as BlockingIOError +from _io import BufferedRWPair as BufferedRWPair +from _io import BufferedRandom as BufferedRandom +from _io import BufferedReader as BufferedReader +from _io import BufferedWriter as BufferedWriter +from _io import BytesIO as BytesIO +from _io import DEFAULT_BUFFER_SIZE as DEFAULT_BUFFER_SIZE +from _io import FileIO as FileIO +from _io import IncrementalNewlineDecoder as IncrementalNewlineDecoder +from _io import StringIO as StringIO +from _io import TextIOWrapper as TextIOWrapper +from _io import UnsupportedOperation as UnsupportedOperation +from _io import open as open def _OpenWrapper(file: Union[str, unicode, int], mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., From 986a33fffee108146d64112b97fede139018cd26 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Tue, 13 Jun 2017 08:47:30 -0700 Subject: [PATCH 10/10] remove trailing whitespace --- stdlib/2/_io.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2/_io.pyi b/stdlib/2/_io.pyi index b22342ab4e1f..0a0b6d0fc877 100644 --- a/stdlib/2/_io.pyi +++ b/stdlib/2/_io.pyi @@ -152,7 +152,7 @@ class TextIOWrapper(_TextIOBase): line_buffering = ... # type: bool buffer = ... # type: BinaryIO _CHUNK_SIZE = ... # type: int - def __init__(self, buffer: IO, + def __init__(self, buffer: IO, encoding: Optional[Text] = ..., errors: Optional[Text] = ..., newline: Optional[Text] = ...,