Skip to content

Commit 41974b8

Browse files
committed
gh-108411: Make typing.IO/BinaryIO arguments positional-only
`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 4345253 commit 41974b8

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
@@ -3524,23 +3524,23 @@ def isatty(self) -> bool:
35243524
pass
35253525

35263526
@abstractmethod
3527-
def read(self, n: int = -1) -> AnyStr:
3527+
def read(self, n: int = -1, /) -> AnyStr:
35283528
pass
35293529

35303530
@abstractmethod
35313531
def readable(self) -> bool:
35323532
pass
35333533

35343534
@abstractmethod
3535-
def readline(self, limit: int = -1) -> AnyStr:
3535+
def readline(self, limit: int = -1, /) -> AnyStr:
35363536
pass
35373537

35383538
@abstractmethod
3539-
def readlines(self, hint: int = -1) -> list[AnyStr]:
3539+
def readlines(self, hint: int = -1, /) -> list[AnyStr]:
35403540
pass
35413541

35423542
@abstractmethod
3543-
def seek(self, offset: int, whence: int = 0) -> int:
3543+
def seek(self, offset: int, whence: int = 0, /) -> int:
35443544
pass
35453545

35463546
@abstractmethod
@@ -3552,27 +3552,27 @@ def tell(self) -> int:
35523552
pass
35533553

35543554
@abstractmethod
3555-
def truncate(self, size: int | None = None) -> int:
3555+
def truncate(self, size: int | None = None, /) -> int:
35563556
pass
35573557

35583558
@abstractmethod
35593559
def writable(self) -> bool:
35603560
pass
35613561

35623562
@abstractmethod
3563-
def write(self, s: AnyStr) -> int:
3563+
def write(self, s: AnyStr, /) -> int:
35643564
pass
35653565

35663566
@abstractmethod
3567-
def writelines(self, lines: list[AnyStr]) -> None:
3567+
def writelines(self, lines: list[AnyStr], /) -> None:
35683568
pass
35693569

35703570
@abstractmethod
35713571
def __enter__(self) -> IO[AnyStr]:
35723572
pass
35733573

35743574
@abstractmethod
3575-
def __exit__(self, type, value, traceback) -> None:
3575+
def __exit__(self, type, value, traceback, /) -> None:
35763576
pass
35773577

35783578

@@ -3582,7 +3582,7 @@ class BinaryIO(IO[bytes]):
35823582
__slots__ = ()
35833583

35843584
@abstractmethod
3585-
def write(self, s: bytes | bytearray) -> int:
3585+
def write(self, s: bytes | bytearray, /) -> int:
35863586
pass
35873587

35883588
@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)