Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion stdlib/@tests/test_cases/check_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from _io import BufferedReader, BufferedRWPair, BufferedWriter
from gzip import GzipFile
from io import FileIO, RawIOBase, TextIOWrapper
from io import BufferedRandom, BytesIO, FileIO, RawIOBase, TextIOWrapper
from socket import SocketIO
from typing import Any
from typing_extensions import assert_type
Expand All @@ -16,3 +16,23 @@
assert_type(TextIOWrapper(FileIO("")).buffer, FileIO)
assert_type(TextIOWrapper(FileIO(13)).detach(), FileIO)
assert_type(TextIOWrapper(GzipFile("")).buffer, GzipFile)


class Index:
def __index__(self) -> int:
return 1


assert_type(FileIO("").seek(Index()), int)
assert_type(BufferedReader(BytesIO()).seek(Index()), int)
assert_type(BufferedWriter(BytesIO()).seek(Index()), int)
assert_type(BufferedRandom(FileIO("")).seek(Index()), int)
FileIO("").seek("1") # type: ignore[arg-type]


class NarrowBytesIO(BytesIO):
def seek(self, pos: int, whence: int = 0, /) -> int:
return super().seek(pos, whence)


assert_type(NarrowBytesIO().seek(1), int)
10 changes: 5 additions & 5 deletions stdlib/_io.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from collections.abc import Callable, Iterable, Iterator
from io import BufferedIOBase, RawIOBase, TextIOBase, UnsupportedOperation as UnsupportedOperation
from os import _Opener
from types import TracebackType
from typing import IO, Any, BinaryIO, Final, Generic, Literal, Protocol, TextIO, TypeVar, overload, type_check_only
from typing import IO, Any, BinaryIO, Final, Generic, Literal, Protocol, SupportsIndex, TextIO, TypeVar, overload, type_check_only
from typing_extensions import Self, disjoint_base

_S = TypeVar("_S", bound=str)
Expand Down Expand Up @@ -107,7 +107,7 @@ class FileIO(RawIOBase, _RawIOBase, BinaryIO): # type: ignore[misc] # incompat
) -> None: ...
@property
def closefd(self) -> bool: ...
def seek(self, pos: int, whence: int = 0, /) -> int: ...
def seek(self, pos: SupportsIndex, whence: int = 0, /) -> int: ...
def read(self, size: int | None = -1, /) -> bytes | MaybeNone: ...

@disjoint_base
Expand Down Expand Up @@ -160,7 +160,7 @@ class BufferedReader(BufferedIOBase, _BufferedIOBase, BinaryIO, Generic[_Buffere
def __init__(self, raw: _BufferedReaderStreamT, buffer_size: int = 8192) -> None: ...

def peek(self, size: int = 0, /) -> bytes: ...
def seek(self, target: int, whence: int = 0, /) -> int: ...
def seek(self, target: SupportsIndex, whence: int = 0, /) -> int: ...
def truncate(self, pos: int | None = None, /) -> int: ...

@type_check_only
Expand Down Expand Up @@ -198,7 +198,7 @@ class BufferedWriter(BufferedIOBase, _BufferedIOBase, BinaryIO, Generic[_Buffere
def __init__(self, raw: _BufferedWriterStreamT, buffer_size: int = 8192) -> None: ...

def write(self, buffer: ReadableBuffer, /) -> int: ...
def seek(self, target: int, whence: int = 0, /) -> int: ...
def seek(self, target: SupportsIndex, whence: int = 0, /) -> int: ...
def truncate(self, pos: int | None = None, /) -> int: ...

@disjoint_base
Expand All @@ -211,7 +211,7 @@ class BufferedRandom(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore
else:
def __init__(self, raw: RawIOBase, buffer_size: int = 8192) -> None: ...

def seek(self, target: int, whence: int = 0, /) -> int: ... # stubtest needs this
def seek(self, target: SupportsIndex, whence: int = 0, /) -> int: ... # stubtest needs this
def peek(self, size: int = 0, /) -> bytes: ...
def truncate(self, pos: int | None = None, /) -> int: ...

Expand Down
Loading