@@ -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__ = [
@@ -146,10 +146,29 @@ class TextIOBase(IOBase):
146
146
def readlines (self , __hint : int = - 1 ) -> list [str ]: ... # type: ignore[override]
147
147
def read (self , __size : int | None = ...) -> str : ...
148
148
149
+ @type_check_only
150
+ class _WrappedBuffer (Protocol ):
151
+ name : str
152
+ closed : bool
153
+ def read (self , size : int = ...) -> bytes : ...
154
+ # Optional: def read1(size: int, /) -> bytes: ...
155
+ def write (self , b : bytes , / ) -> object : ...
156
+ def flush (self ) -> object : ...
157
+ def close (self ) -> object : ...
158
+ def seekable () -> bool : ...
159
+ def readable () -> bool : ...
160
+ def writable () -> bool : ...
161
+ def truncate (size : int , / ) -> int : ...
162
+ def fileno () -> int : ...
163
+ def isatty () -> int : ...
164
+ # Optional: Only needs to be present if seekable() returns True.
165
+ # def seek(self, offset: Literal[0], whence: Literal[2]) -> int: ...
166
+ # def tell(self) -> int: ...
167
+
149
168
class TextIOWrapper (TextIOBase , TextIO ): # type: ignore[misc] # incompatible definitions of write in the base classes
150
169
def __init__ (
151
170
self ,
152
- buffer : IO [ bytes ] ,
171
+ buffer : _WrappedBuffer ,
153
172
encoding : str | None = ...,
154
173
errors : str | None = ...,
155
174
newline : str | None = ...,
@@ -180,7 +199,10 @@ class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible d
180
199
def writelines (self , __lines : Iterable [str ]) -> None : ... # type: ignore[override]
181
200
def readline (self , __size : int = - 1 ) -> str : ... # type: ignore[override]
182
201
def readlines (self , __hint : int = - 1 ) -> list [str ]: ... # type: ignore[override]
183
- def seek (self , __cookie : int , __whence : int = 0 ) -> int : ... # stubtest needs this
202
+ @overload
203
+ def seek (self , __cookie : int , __whence : Literal [0 ] = 0 ) -> int : ...
204
+ @overload
205
+ def seek (self , __cookie : Literal [0 ], __whence : Literal [1 , 2 ]) -> int : ...
184
206
185
207
class StringIO (TextIOWrapper ):
186
208
def __init__ (self , initial_value : str | None = ..., newline : str | None = ...) -> None : ...
0 commit comments