Skip to content

Commit f6ed7c0

Browse files
authored
gh-108411: Make typing.IO/BinaryIO arguments positional-only (#142906)
`IO` is purported to be the type of the file objects returned by `open`. However, all methods on those objects take positional-only arguments, while `IO`'s methods are declared with regular arguments. As such, the file objects cannot actually be considered to implement `IO`. The same thing applies to `BinaryIO`. Fix this by adjusting the definition of these ABCs to match the file objects. This is technically a breaking change, but it is unlikely to actually break anything: * These methods should never be called at runtime, since they are abstract. Therefore, this should not cause any runtime errors. * In typeshed these arguments are already positional-only, so this should not cause any errors during typechecking either.
1 parent 9a1c70c commit f6ed7c0

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

Lib/typing.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,23 +3612,23 @@ def isatty(self) -> bool:
36123612
pass
36133613

36143614
@abstractmethod
3615-
def read(self, n: int = -1) -> AnyStr:
3615+
def read(self, n: int = -1, /) -> AnyStr:
36163616
pass
36173617

36183618
@abstractmethod
36193619
def readable(self) -> bool:
36203620
pass
36213621

36223622
@abstractmethod
3623-
def readline(self, limit: int = -1) -> AnyStr:
3623+
def readline(self, limit: int = -1, /) -> AnyStr:
36243624
pass
36253625

36263626
@abstractmethod
3627-
def readlines(self, hint: int = -1) -> list[AnyStr]:
3627+
def readlines(self, hint: int = -1, /) -> list[AnyStr]:
36283628
pass
36293629

36303630
@abstractmethod
3631-
def seek(self, offset: int, whence: int = 0) -> int:
3631+
def seek(self, offset: int, whence: int = 0, /) -> int:
36323632
pass
36333633

36343634
@abstractmethod
@@ -3640,27 +3640,27 @@ def tell(self) -> int:
36403640
pass
36413641

36423642
@abstractmethod
3643-
def truncate(self, size: int | None = None) -> int:
3643+
def truncate(self, size: int | None = None, /) -> int:
36443644
pass
36453645

36463646
@abstractmethod
36473647
def writable(self) -> bool:
36483648
pass
36493649

36503650
@abstractmethod
3651-
def write(self, s: AnyStr) -> int:
3651+
def write(self, s: AnyStr, /) -> int:
36523652
pass
36533653

36543654
@abstractmethod
3655-
def writelines(self, lines: list[AnyStr]) -> None:
3655+
def writelines(self, lines: list[AnyStr], /) -> None:
36563656
pass
36573657

36583658
@abstractmethod
36593659
def __enter__(self) -> IO[AnyStr]:
36603660
pass
36613661

36623662
@abstractmethod
3663-
def __exit__(self, type, value, traceback) -> None:
3663+
def __exit__(self, type, value, traceback, /) -> None:
36643664
pass
36653665

36663666

@@ -3670,7 +3670,7 @@ class BinaryIO(IO[bytes]):
36703670
__slots__ = ()
36713671

36723672
@abstractmethod
3673-
def write(self, s: bytes | bytearray) -> int:
3673+
def write(self, s: bytes | bytearray, /) -> int:
36743674
pass
36753675

36763676
@abstractmethod
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``typing.IO`` and ``typing.BinaryIO`` method arguments are now
2+
positional-only.

0 commit comments

Comments
 (0)