diff --git a/stdlib/@tests/test_cases/check_io.py b/stdlib/@tests/test_cases/check_io.py index c3ee5aa4c6c0..7e2c8001f3a5 100644 --- a/stdlib/@tests/test_cases/check_io.py +++ b/stdlib/@tests/test_cases/check_io.py @@ -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 @@ -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) diff --git a/stdlib/_io.pyi b/stdlib/_io.pyi index 5e216123be98..9012e0fd5348 100644 --- a/stdlib/_io.pyi +++ b/stdlib/_io.pyi @@ -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) @@ -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 @@ -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 @@ -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 @@ -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: ...