Skip to content

Commit 4321cc2

Browse files
committed
Turn TextIOWrapper.__init__(buffer) into a protocol
1 parent 0393485 commit 4321cc2

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

stdlib/io.pyi

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from _typeshed import FileDescriptorOrPath, ReadableBuffer, WriteableBuffer
66
from collections.abc import Callable, Iterable, Iterator
77
from os import _Opener
88
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
1010
from typing_extensions import Self
1111

1212
__all__ = [
@@ -146,10 +146,29 @@ class TextIOBase(IOBase):
146146
def readlines(self, __hint: int = -1) -> list[str]: ... # type: ignore[override]
147147
def read(self, __size: int | None = ...) -> str: ...
148148

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+
149168
class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible definitions of write in the base classes
150169
def __init__(
151170
self,
152-
buffer: IO[bytes],
171+
buffer: _WrappedBuffer,
153172
encoding: str | None = ...,
154173
errors: str | None = ...,
155174
newline: str | None = ...,
@@ -180,7 +199,10 @@ class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible d
180199
def writelines(self, __lines: Iterable[str]) -> None: ... # type: ignore[override]
181200
def readline(self, __size: int = -1) -> str: ... # type: ignore[override]
182201
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: ...
184206

185207
class StringIO(TextIOWrapper):
186208
def __init__(self, initial_value: str | None = ..., newline: str | None = ...) -> None: ...

0 commit comments

Comments
 (0)