Skip to content

Use a Protocol as the argument for io.TextIOWrapper #10229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
27 changes: 23 additions & 4 deletions stdlib/io.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ from _typeshed import FileDescriptorOrPath, ReadableBuffer, WriteableBuffer
from collections.abc import Callable, Iterable, Iterator
from os import _Opener
from types import TracebackType
from typing import IO, Any, BinaryIO, Literal, TextIO, TypeVar, overload
from typing_extensions import Self
from typing import IO, Any, BinaryIO, Literal, Protocol, TextIO, TypeVar, overload
from typing_extensions import Literal, Self

__all__ = [
"BlockingIOError",
Expand Down Expand Up @@ -146,18 +146,36 @@ class TextIOBase(IOBase):
def readlines(self, __hint: int = -1) -> list[str]: ... # type: ignore[override]
def read(self, __size: int | None = ...) -> str: ...

class _TextIOWrapperBuffer(Protocol):
def readable(self) -> bool: ...
def seekable(self) -> bool: ...
def writable(self) -> bool: ...
def isatty(self) -> bool: ...
def write(self, __s: bytes) -> object: ...
def flush(self) -> object: ...
def read(self, __n: int) -> ReadableBuffer: ...
def seek(self, __offset: int, __whence: int = ...) -> int: ...
def tell(self) -> int: ...
def truncate(self, __size: int | None = ...) -> int: ...
def fileno(self) -> int: ...
def close(self) -> None: ...
@property
def name(self) -> str: ...
@property
def closed(self) -> bool: ...

class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible definitions of write in the base classes
def __init__(
self,
buffer: IO[bytes],
buffer: _TextIOWrapperBuffer,
encoding: str | None = ...,
errors: str | None = ...,
newline: str | None = ...,
line_buffering: bool = ...,
write_through: bool = ...,
) -> None: ...
@property
def buffer(self) -> BinaryIO: ...
def buffer(self) -> _TextIOWrapperBuffer: ... # type: ignore[override]
@property
def closed(self) -> bool: ...
@property
Expand All @@ -180,6 +198,7 @@ class TextIOWrapper(TextIOBase, TextIO): # type: ignore[misc] # incompatible d
def writelines(self, __lines: Iterable[str]) -> None: ... # type: ignore[override]
def readline(self, __size: int = -1) -> str: ... # type: ignore[override]
def readlines(self, __hint: int = -1) -> list[str]: ... # type: ignore[override]
def detach(self) -> _TextIOWrapperBuffer: ... # type: ignore[override]
def seek(self, __cookie: int, __whence: int = 0) -> int: ... # stubtest needs this

class StringIO(TextIOWrapper):
Expand Down