Skip to content

Commit e37ac25

Browse files
authored
clean up io methods (#12947)
fixes for stubtest and default values
1 parent 348c44f commit e37ac25

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,6 @@ importlib.abc.Loader.exec_module # See Lib/importlib/_abc.py. Might be defined
5959
importlib.abc.MetaPathFinder.find_spec # Not defined on the actual class, but expected to exist.
6060
importlib.abc.PathEntryFinder.find_spec # Not defined on the actual class, but expected to exist.
6161
importlib.machinery.ExtensionFileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature
62-
io.BufferedRandom.truncate
63-
io.BufferedReader.seek
64-
io.BufferedReader.truncate
65-
io.BufferedWriter.seek
66-
io.BufferedWriter.truncate
67-
io.BytesIO.readlines
68-
io.BytesIO.seek # Parameter name for a positional-only param differs from its name in the inherited method
69-
io.FileIO.seek
70-
io.StringIO.seek
71-
io.StringIO.truncate
72-
io.TextIOWrapper.truncate
73-
_io.BufferedRandom.truncate
74-
_io.BufferedReader.seek
75-
_io.BufferedReader.truncate
76-
_io.BufferedWriter.seek
77-
_io.BufferedWriter.truncate
78-
_io.BytesIO.readlines
79-
_io.BytesIO.seek # Parameter name for a positional-only param differs from its name in the inherited method
80-
_io.FileIO.seek
81-
_io.StringIO.seek
82-
_io.StringIO.truncate
83-
_io.TextIOWrapper.truncate
8462
ipaddress.IPv4Interface.hostmask
8563
ipaddress.IPv6Interface.hostmask
8664
ipaddress._BaseAddress.is_global

stdlib/_io.pyi

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class _IOBase:
3333
def readable(self) -> bool: ...
3434
read: Callable[..., Any]
3535
def readlines(self, hint: int = -1, /) -> list[bytes]: ...
36-
def seek(self, offset: int, whence: int = ..., /) -> int: ...
36+
def seek(self, offset: int, whence: int = 0, /) -> int: ...
3737
def seekable(self) -> bool: ...
3838
def tell(self) -> int: ...
39-
def truncate(self, size: int | None = ..., /) -> int: ...
39+
def truncate(self, size: int | None = None, /) -> int: ...
4040
def writable(self) -> bool: ...
4141
write: Callable[..., Any]
4242
def writelines(self, lines: Iterable[ReadableBuffer], /) -> None: ...
@@ -59,8 +59,8 @@ class _BufferedIOBase(_IOBase):
5959
def readinto(self, buffer: WriteableBuffer, /) -> int: ...
6060
def write(self, buffer: ReadableBuffer, /) -> int: ...
6161
def readinto1(self, buffer: WriteableBuffer, /) -> int: ...
62-
def read(self, size: int | None = ..., /) -> bytes: ...
63-
def read1(self, size: int = ..., /) -> bytes: ...
62+
def read(self, size: int | None = -1, /) -> bytes: ...
63+
def read1(self, size: int = -1, /) -> bytes: ...
6464

6565
class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes
6666
mode: str
@@ -69,30 +69,38 @@ class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompat
6969
# "name" is a str. In the future, making FileIO generic might help.
7070
name: Any
7171
def __init__(
72-
self, file: FileDescriptorOrPath, mode: str = ..., closefd: bool = ..., opener: _Opener | None = ...
72+
self, file: FileDescriptorOrPath, mode: str = "r", closefd: bool = True, opener: _Opener | None = None
7373
) -> None: ...
7474
@property
7575
def closefd(self) -> bool: ...
76+
def seek(self, pos: int, whence: int = 0, /) -> int: ...
77+
def read(self, size: int | None = -1, /) -> bytes | MaybeNone: ...
7678

7779
class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
78-
def __init__(self, initial_bytes: ReadableBuffer = ...) -> None: ...
80+
def __init__(self, initial_bytes: ReadableBuffer = b"") -> None: ...
7981
# BytesIO does not contain a "name" field. This workaround is necessary
8082
# to allow BytesIO sub-classes to add this field, as it is defined
8183
# as a read-only property on IO[].
8284
name: Any
8385
def getvalue(self) -> bytes: ...
8486
def getbuffer(self) -> memoryview: ...
8587
def read1(self, size: int | None = -1, /) -> bytes: ...
88+
def readlines(self, size: int | None = None, /) -> list[bytes]: ...
89+
def seek(self, pos: int, whence: int = 0, /) -> int: ...
8690

8791
class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
8892
raw: RawIOBase
8993
def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ...
9094
def peek(self, size: int = 0, /) -> bytes: ...
95+
def seek(self, target: int, whence: int = 0, /) -> int: ...
96+
def truncate(self, pos: int | None = None, /) -> int: ...
9197

9298
class BufferedWriter(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of writelines in the base classes
9399
raw: RawIOBase
94100
def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ...
95101
def write(self, buffer: ReadableBuffer, /) -> int: ...
102+
def seek(self, target: int, whence: int = 0, /) -> int: ...
103+
def truncate(self, pos: int | None = None, /) -> int: ...
96104

97105
class BufferedRandom(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible definitions of methods in the base classes
98106
mode: str
@@ -101,10 +109,11 @@ class BufferedRandom(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore
101109
def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ...
102110
def seek(self, target: int, whence: int = 0, /) -> int: ... # stubtest needs this
103111
def peek(self, size: int = 0, /) -> bytes: ...
112+
def truncate(self, pos: int | None = None, /) -> int: ...
104113

105114
class BufferedRWPair(BufferedIOBase, _BufferedIOBase):
106115
def __init__(self, reader: RawIOBase, writer: RawIOBase, buffer_size: int = 8192) -> None: ...
107-
def peek(self, size: int = ..., /) -> bytes: ...
116+
def peek(self, size: int = 0, /) -> bytes: ...
108117

109118
class _TextIOBase(_IOBase):
110119
encoding: str
@@ -115,9 +124,9 @@ class _TextIOBase(_IOBase):
115124
def detach(self) -> BinaryIO: ...
116125
def write(self, s: str, /) -> int: ...
117126
def writelines(self, lines: Iterable[str], /) -> None: ... # type: ignore[override]
118-
def readline(self, size: int = ..., /) -> str: ... # type: ignore[override]
127+
def readline(self, size: int = -1, /) -> str: ... # type: ignore[override]
119128
def readlines(self, hint: int = -1, /) -> list[str]: ... # type: ignore[override]
120-
def read(self, size: int | None = ..., /) -> str: ...
129+
def read(self, size: int | None = -1, /) -> str: ...
121130

122131
@type_check_only
123132
class _WrappedBuffer(Protocol):
@@ -177,19 +186,22 @@ class TextIOWrapper(TextIOBase, _TextIOBase, TextIO, Generic[_BufferT_co]): # t
177186
# TextIOWrapper's version of seek only supports a limited subset of
178187
# operations.
179188
def seek(self, cookie: int, whence: int = 0, /) -> int: ...
189+
def truncate(self, pos: int | None = None, /) -> int: ...
180190

181191
class StringIO(TextIOBase, _TextIOBase, TextIO): # type: ignore[misc] # incompatible definitions of write in the base classes
182-
def __init__(self, initial_value: str | None = ..., newline: str | None = ...) -> None: ...
192+
def __init__(self, initial_value: str | None = "", newline: str | None = "\n") -> None: ...
183193
# StringIO does not contain a "name" field. This workaround is necessary
184194
# to allow StringIO sub-classes to add this field, as it is defined
185195
# as a read-only property on IO[].
186196
name: Any
187197
def getvalue(self) -> str: ...
188198
@property
189199
def line_buffering(self) -> bool: ...
200+
def seek(self, pos: int, whence: int = 0, /) -> int: ...
201+
def truncate(self, pos: int | None = None, /) -> int: ...
190202

191203
class IncrementalNewlineDecoder:
192-
def __init__(self, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = ...) -> None: ...
204+
def __init__(self, decoder: codecs.IncrementalDecoder | None, translate: bool, errors: str = "strict") -> None: ...
193205
def decode(self, input: ReadableBuffer | str, final: bool = False) -> str: ...
194206
@property
195207
def newlines(self) -> str | tuple[str, ...] | None: ...

0 commit comments

Comments
 (0)