Skip to content
123 changes: 91 additions & 32 deletions stdlib/2/_io.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from typing import Any, BinaryIO, IO, 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


class BlockingIOError(IOError):
characters_written = ... # type: int

class UnsupportedOperation(ValueError, IOError): ...

_T = TypeVar("_T")

class _IOBase(BinaryIO):
@property
def closed(self) -> bool: ...
def _checkClosed(self) -> None: ...
def _checkReadable(self) -> None: ...
def _checkSeekable(self) -> None: ...
Expand All @@ -19,91 +22,147 @@ class _IOBase(BinaryIO):
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 __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():
def readline(self, limit: int = ...) -> bytes: ...
def readlines(self, hint: int = ...) -> list[bytes]: ...
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: ...

class _BufferedIOBase(_IOBase):
def read1(self, n: int) -> str: ...
def read(self, n: int = ...) -> str: ...
def read1(self, n: int) -> bytes: ...
def read(self, size: int = ...) -> bytes: ...
def readinto(self, buffer: bytearray) -> int: ...
def write(self, s: str) -> int: ...
def detach(self) -> "_BufferedIOBase": ...
def write(self, s: bytes) -> int: ...
def detach(self) -> _IOBase: ...

class BufferedRWPair(_BufferedIOBase):
def peek(self, n: int = ...) -> str: ...
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: ...

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):
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 write(self, s: bytes) -> int: ...
def writelines(self, lines: Iterable[bytes]) -> None: ...
def read1(self, size: int) -> bytes: ...
def next(self) -> bytes: ...

class _RawIOBase(_IOBase):
def readall(self) -> str: ...
def read(self, n: int = ...) -> str: ...

class FileIO(_RawIOBase):
class FileIO(_RawIOBase, BytesIO): # type: ignore # for __enter__
mode = ... # type: str
closefd = ... # type: bool
def __init__(self, file: str, mode: str = ...) -> None: ...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor argument is name according to the docs, not file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(That was the Python 3 version, actually. Python 2 is the same: https://github.com/python/cpython/blob/2.7/Modules/_io/fileio.c#L183)

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):

# Note: In the actual _io.py, _TextIOBase inherits from _IOBase.
class _TextIOBase(TextIO):
errors = ... # type: Optional[str]
newlines = ... # type: Union[str, unicode]
encoding = ... # type: Optional[str]
def read(self, n: int = ...) -> str: ...
def detach(self) -> None:
raise UnsupportedOperation
# 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: ...
def _checkClosed(self) -> None: ...
def _checkReadable(self) -> None: ...
def _checkSeekable(self) -> None: ...
def _checkWritable(self) -> None: ...
def close(self) -> None: ...
def detach(self) -> IO: ...
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: _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 getvalue(self) -> str: ...
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: ...

class TextIOWrapper(_TextIOBase):
name = ... # type: str
line_buffering = ... # type: bool
buffer = ... # type: str
buffer = ... # type: BinaryIO
_CHUNK_SIZE = ... # type: int

def open(file: Union[int, str], mode: str = ...) -> _IOBase: ...
def __init__(self, buffer: IO,
encoding: Optional[Text] = ...,
errors: Optional[Text] = ...,
newline: Optional[Text] = ...,
line_buffering: bool = ...,
write_through: bool = ...) -> None: ...

def open(file: Union[str, unicode, int],
mode: unicode = ...,
buffering: int = ...,
encoding: Optional[Text] = ...,
errors: Optional[Text] = ...,
newline: Optional[Text] = ...,
closefd: bool = ...) -> IO[Any]: ...
111 changes: 25 additions & 86 deletions stdlib/2/io.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,37 @@
from typing import List, BinaryIO, TextIO, IO, overload, Iterator, Iterable, Any, Union, Optional
import _io

DEFAULT_BUFFER_SIZE = 0

def open(file: Union[str, unicode, int],
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 = ...,
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): # type: ignore
pass