Skip to content

Commit 2b1c7d5

Browse files
authored
re-sort io classes into _io (#12755)
This version keeps it simple and clean: No changes to class bodies. The only changes here are moving between files and updating the naming and inheritance. Related to #3968 and split from #12740.
1 parent 3e29e05 commit 2b1c7d5

File tree

4 files changed

+247
-206
lines changed

4 files changed

+247
-206
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ io.FileIO.seek
6060
io.StringIO.seek
6161
io.StringIO.truncate
6262
io.TextIOWrapper.truncate
63+
_io.BufferedRandom.truncate
64+
_io.BufferedReader.seek
65+
_io.BufferedReader.truncate
66+
_io.BufferedWriter.seek
67+
_io.BufferedWriter.truncate
68+
_io.BytesIO.readlines
69+
_io.BytesIO.seek # Parameter name for a positional-only param differs from its name in the inherited method
70+
_io.FileIO.seek
71+
_io.StringIO.seek
72+
_io.StringIO.truncate
73+
_io.TextIOWrapper.truncate
6374
ipaddress.IPv4Interface.hostmask
6475
ipaddress.IPv6Interface.hostmask
6576
ipaddress._BaseNetwork.broadcast_address

stdlib/VERSIONS

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ _imp: 3.0-
3737
_interpchannels: 3.13-
3838
_interpqueues: 3.13-
3939
_interpreters: 3.13-
40+
_io: 3.0-
4041
_json: 3.0-
4142
_locale: 3.0-
4243
_lsprof: 3.0-

stdlib/_io.pyi

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import builtins
2+
import codecs
3+
import sys
4+
from _typeshed import FileDescriptorOrPath, MaybeNone, ReadableBuffer, WriteableBuffer
5+
from collections.abc import Callable, Iterable, Iterator
6+
from io import BufferedIOBase, RawIOBase, TextIOBase, UnsupportedOperation as UnsupportedOperation
7+
from os import _Opener
8+
from types import TracebackType
9+
from typing import IO, Any, BinaryIO, Final, Generic, Literal, Protocol, TextIO, TypeVar, overload, type_check_only
10+
from typing_extensions import Self
11+
12+
_T = TypeVar("_T")
13+
14+
DEFAULT_BUFFER_SIZE: Final = 8192
15+
16+
open = builtins.open
17+
18+
def open_code(path: str) -> IO[bytes]: ...
19+
20+
BlockingIOError = builtins.BlockingIOError
21+
22+
class _IOBase:
23+
def __iter__(self) -> Iterator[bytes]: ...
24+
def __next__(self) -> bytes: ...
25+
def __enter__(self) -> Self: ...
26+
def __exit__(
27+
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
28+
) -> None: ...
29+
def close(self) -> None: ...
30+
def fileno(self) -> int: ...
31+
def flush(self) -> None: ...
32+
def isatty(self) -> bool: ...
33+
def readable(self) -> bool: ...
34+
read: Callable[..., Any]
35+
def readlines(self, hint: int = -1, /) -> list[bytes]: ...
36+
def seek(self, offset: int, whence: int = ..., /) -> int: ...
37+
def seekable(self) -> bool: ...
38+
def tell(self) -> int: ...
39+
def truncate(self, size: int | None = ..., /) -> int: ...
40+
def writable(self) -> bool: ...
41+
write: Callable[..., Any]
42+
def writelines(self, lines: Iterable[ReadableBuffer], /) -> None: ...
43+
def readline(self, size: int | None = -1, /) -> bytes: ...
44+
def __del__(self) -> None: ...
45+
@property
46+
def closed(self) -> bool: ...
47+
def _checkClosed(self) -> None: ... # undocumented
48+
49+
class _RawIOBase(_IOBase):
50+
def readall(self) -> bytes: ...
51+
# The following methods can return None if the file is in non-blocking mode
52+
# and no data is available.
53+
def readinto(self, buffer: WriteableBuffer, /) -> int | MaybeNone: ...
54+
def write(self, b: ReadableBuffer, /) -> int | MaybeNone: ...
55+
def read(self, size: int = -1, /) -> bytes | MaybeNone: ...
56+
57+
class _BufferedIOBase(_IOBase):
58+
def detach(self) -> RawIOBase: ...
59+
def readinto(self, buffer: WriteableBuffer, /) -> int: ...
60+
def write(self, buffer: ReadableBuffer, /) -> int: ...
61+
def readinto1(self, buffer: WriteableBuffer, /) -> int: ...
62+
def read(self, size: int | None = ..., /) -> bytes: ...
63+
def read1(self, size: int = ..., /) -> bytes: ...
64+
65+
class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes
66+
mode: str
67+
# The type of "name" equals the argument passed in to the constructor,
68+
# but that can make FileIO incompatible with other I/O types that assume
69+
# "name" is a str. In the future, making FileIO generic might help.
70+
name: Any
71+
def __init__(
72+
self, file: FileDescriptorOrPath, mode: str = ..., closefd: bool = ..., opener: _Opener | None = ...
73+
) -> None: ...
74+
@property
75+
def closefd(self) -> bool: ...
76+
def __enter__(self) -> Self: ...
77+
78+
class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
79+
def __init__(self, initial_bytes: ReadableBuffer = ...) -> None: ...
80+
# BytesIO does not contain a "name" field. This workaround is necessary
81+
# to allow BytesIO sub-classes to add this field, as it is defined
82+
# as a read-only property on IO[].
83+
name: Any
84+
def __enter__(self) -> Self: ...
85+
def getvalue(self) -> bytes: ...
86+
def getbuffer(self) -> memoryview: ...
87+
def read1(self, size: int | None = -1, /) -> bytes: ...
88+
89+
class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
90+
raw: RawIOBase
91+
def __enter__(self) -> Self: ...
92+
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
93+
def peek(self, size: int = 0, /) -> bytes: ...
94+
95+
class BufferedWriter(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes
96+
raw: RawIOBase
97+
def __enter__(self) -> Self: ...
98+
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
99+
def write(self, buffer: ReadableBuffer, /) -> int: ...
100+
101+
class BufferedRandom(BufferedReader, BufferedWriter, BufferedIOBase, _BufferedIOBase): # type: ignore[misc] # incompatible definitions of methods in the base classes
102+
def __enter__(self) -> Self: ...
103+
def seek(self, target: int, whence: int = 0, /) -> int: ... # stubtest needs this
104+
105+
class BufferedRWPair(BufferedIOBase, _BufferedIOBase):
106+
def __init__(self, reader: RawIOBase, writer: RawIOBase, buffer_size: int = ...) -> None: ...
107+
def peek(self, size: int = ..., /) -> bytes: ...
108+
109+
class _TextIOBase(_IOBase):
110+
encoding: str
111+
errors: str | None
112+
newlines: str | tuple[str, ...] | None
113+
def __iter__(self) -> Iterator[str]: ... # type: ignore[override]
114+
def __next__(self) -> str: ... # type: ignore[override]
115+
def detach(self) -> BinaryIO: ...
116+
def write(self, s: str, /) -> int: ...
117+
def writelines(self, lines: Iterable[str], /) -> None: ... # type: ignore[override]
118+
def readline(self, size: int = ..., /) -> str: ... # type: ignore[override]
119+
def readlines(self, hint: int = -1, /) -> list[str]: ... # type: ignore[override]
120+
def read(self, size: int | None = ..., /) -> str: ...
121+
122+
@type_check_only
123+
class _WrappedBuffer(Protocol):
124+
# "name" is wrapped by TextIOWrapper. Its type is inconsistent between
125+
# the various I/O types, see the comments on TextIOWrapper.name and
126+
# TextIO.name.
127+
@property
128+
def name(self) -> Any: ...
129+
@property
130+
def closed(self) -> bool: ...
131+
def read(self, size: int = ..., /) -> ReadableBuffer: ...
132+
# Optional: def read1(self, size: int, /) -> ReadableBuffer: ...
133+
def write(self, b: bytes, /) -> object: ...
134+
def flush(self) -> object: ...
135+
def close(self) -> object: ...
136+
def seekable(self) -> bool: ...
137+
def readable(self) -> bool: ...
138+
def writable(self) -> bool: ...
139+
def truncate(self, size: int, /) -> int: ...
140+
def fileno(self) -> int: ...
141+
def isatty(self) -> bool: ...
142+
# Optional: Only needs to be present if seekable() returns True.
143+
# def seek(self, offset: Literal[0], whence: Literal[2]) -> int: ...
144+
# def tell(self) -> int: ...
145+
146+
_BufferT_co = TypeVar("_BufferT_co", bound=_WrappedBuffer, default=_WrappedBuffer, covariant=True)
147+
148+
class TextIOWrapper(TextIOBase, _TextIOBase, TextIO, Generic[_BufferT_co]): # type: ignore[misc] # incompatible definitions of write in the base classes
149+
def __init__(
150+
self,
151+
buffer: _BufferT_co,
152+
encoding: str | None = None,
153+
errors: str | None = None,
154+
newline: str | None = None,
155+
line_buffering: bool = False,
156+
write_through: bool = False,
157+
) -> None: ...
158+
# Equals the "buffer" argument passed in to the constructor.
159+
@property
160+
def buffer(self) -> _BufferT_co: ... # type: ignore[override]
161+
@property
162+
def closed(self) -> bool: ...
163+
@property
164+
def line_buffering(self) -> bool: ...
165+
@property
166+
def write_through(self) -> bool: ...
167+
def reconfigure(
168+
self,
169+
*,
170+
encoding: str | None = None,
171+
errors: str | None = None,
172+
newline: str | None = None,
173+
line_buffering: bool | None = None,
174+
write_through: bool | None = None,
175+
) -> None: ...
176+
# These are inherited from TextIOBase, but must exist in the stub to satisfy mypy.
177+
def __enter__(self) -> Self: ...
178+
def __iter__(self) -> Iterator[str]: ... # type: ignore[override]
179+
def __next__(self) -> str: ... # type: ignore[override]
180+
def writelines(self, lines: Iterable[str], /) -> None: ... # type: ignore[override]
181+
def readline(self, size: int = -1, /) -> str: ... # type: ignore[override]
182+
def readlines(self, hint: int = -1, /) -> list[str]: ... # type: ignore[override]
183+
# Equals the "buffer" argument passed in to the constructor.
184+
def detach(self) -> _BufferT_co: ... # type: ignore[override]
185+
# TextIOWrapper's version of seek only supports a limited subset of
186+
# operations.
187+
def seek(self, cookie: int, whence: int = 0, /) -> int: ...
188+
189+
class StringIO(TextIOWrapper, TextIOBase, _TextIOBase): # type: ignore[misc] # incompatible definitions of write in the base classes
190+
def __init__(self, initial_value: str | None = ..., newline: str | None = ...) -> None: ...
191+
# StringIO does not contain a "name" field. This workaround is necessary
192+
# to allow StringIO sub-classes to add this field, as it is defined
193+
# as a read-only property on IO[].
194+
name: Any
195+
def getvalue(self) -> str: ...
196+
197+
class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
198+
def __init__(self, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = ...) -> None: ...
199+
def decode(self, input: ReadableBuffer | str, final: bool = False) -> str: ...
200+
@property
201+
def newlines(self) -> str | tuple[str, ...] | None: ...
202+
def setstate(self, state: tuple[bytes, int], /) -> None: ...
203+
204+
if sys.version_info >= (3, 10):
205+
@overload
206+
def text_encoding(encoding: None, stacklevel: int = 2, /) -> Literal["locale", "utf-8"]: ...
207+
@overload
208+
def text_encoding(encoding: _T, stacklevel: int = 2, /) -> _T: ...

0 commit comments

Comments
 (0)