@@ -6,7 +6,7 @@ from _typeshed import FileDescriptorOrPath, ReadableBuffer, WriteableBuffer
6
6
from collections .abc import Callable , Iterable , Iterator
7
7
from os import _Opener
8
8
from types import TracebackType
9
- from typing import IO , Any , BinaryIO , Literal , TextIO , TypeVar , overload
9
+ from typing import IO , Any , BinaryIO , Literal , Protocol , TextIO , TypeVar , overload , type_check_only
10
10
from typing_extensions import Self
11
11
12
12
__all__ = [
@@ -94,7 +94,10 @@ class BufferedIOBase(IOBase):
94
94
95
95
class FileIO (RawIOBase , BinaryIO ): # type: ignore[misc] # incompatible definitions of writelines in the base classes
96
96
mode : str
97
- name : FileDescriptorOrPath
97
+ # The type of "name" equals the argument passed in to the constructor,
98
+ # but that can make FileIO incompatible with other I/O types that assume
99
+ # "name" is a str. In the future, making FileIO generic might help.
100
+ name : Any
98
101
def __init__ (
99
102
self , file : FileDescriptorOrPath , mode : str = ..., closefd : bool = ..., opener : _Opener | None = ...
100
103
) -> None : ...
@@ -146,16 +149,43 @@ class TextIOBase(IOBase):
146
149
def readlines (self , __hint : int = - 1 ) -> list [str ]: ... # type: ignore[override]
147
150
def read (self , __size : int | None = ...) -> str : ...
148
151
152
+ @type_check_only
153
+ class _WrappedBuffer (Protocol ):
154
+ # "name" is wrapped by TextIOWrapper. Its type is inconsistent between
155
+ # the various I/O types, see the comments on TextIOWrapper.name and
156
+ # TextIO.name.
157
+ @property
158
+ def name (self ) -> Any : ...
159
+ @property
160
+ def closed (self ) -> bool : ...
161
+ def read (self , size : int = ..., / ) -> ReadableBuffer : ...
162
+ # Optional: def read1(self, size: int, /) -> ReadableBuffer: ...
163
+ def write (self , b : bytes , / ) -> object : ...
164
+ def flush (self ) -> object : ...
165
+ def close (self ) -> object : ...
166
+ def seekable (self ) -> bool : ...
167
+ def readable (self ) -> bool : ...
168
+ def writable (self ) -> bool : ...
169
+ def truncate (self , size : int , / ) -> int : ...
170
+ def fileno (self ) -> int : ...
171
+ def isatty (self ) -> int : ...
172
+ # Optional: Only needs to be present if seekable() returns True.
173
+ # def seek(self, offset: Literal[0], whence: Literal[2]) -> int: ...
174
+ # def tell(self) -> int: ...
175
+
176
+ # TODO: Should be generic over the buffer type, but needs to wait for
177
+ # TypeVar defaults.
149
178
class TextIOWrapper (TextIOBase , TextIO ): # type: ignore[misc] # incompatible definitions of write in the base classes
150
179
def __init__ (
151
180
self ,
152
- buffer : IO [ bytes ] ,
181
+ buffer : _WrappedBuffer ,
153
182
encoding : str | None = ...,
154
183
errors : str | None = ...,
155
184
newline : str | None = ...,
156
185
line_buffering : bool = ...,
157
186
write_through : bool = ...,
158
187
) -> None : ...
188
+ # Equals the "buffer" argument passed in to the constructor.
159
189
@property
160
190
def buffer (self ) -> BinaryIO : ...
161
191
@property
@@ -180,7 +210,11 @@ class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible d
180
210
def writelines (self , __lines : Iterable [str ]) -> None : ... # type: ignore[override]
181
211
def readline (self , __size : int = - 1 ) -> str : ... # type: ignore[override]
182
212
def readlines (self , __hint : int = - 1 ) -> list [str ]: ... # type: ignore[override]
183
- def seek (self , __cookie : int , __whence : int = 0 ) -> int : ... # stubtest needs this
213
+ # Equals the "buffer" argument passed in to the constructor.
214
+ def detach (self ) -> BinaryIO : ...
215
+ # TextIOWrapper's version of seek only supports a limited subset of
216
+ # operations.
217
+ def seek (self , __cookie : int , __whence : int = 0 ) -> int : ...
184
218
185
219
class StringIO (TextIOWrapper ):
186
220
def __init__ (self , initial_value : str | None = ..., newline : str | None = ...) -> None : ...
0 commit comments