Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adjust overloads to work with pyright
  • Loading branch information
JelleZijlstra committed Mar 10, 2023
commit 590851b02b67c3ed7c74133cfdb719a824210fd3
8 changes: 4 additions & 4 deletions stdlib/tempfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ class _TemporaryFileWrapper(Generic[AnyStr], IO[AnyStr]):
def truncate(self, size: int | None = ...) -> int: ...
def writable(self) -> bool: ...
@overload
def write(self: _TemporaryFileWrapper[str], s: str) -> int: ...
def write(self, s: AnyStr) -> int: ...
@overload
def write(self: _TemporaryFileWrapper[bytes], s: ReadableBuffer) -> int: ...
@overload
def writelines(self: _TemporaryFileWrapper[str], lines: Iterable[str]) -> None: ...
def writelines(self, lines: Iterable[AnyStr]) -> None: ...
@overload
def writelines(self: _TemporaryFileWrapper[bytes], lines: Iterable[ReadableBuffer]) -> None: ...
Comment on lines +218 to +225

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These overloads might not work very well with pyright, which has different handling of self types to mypy (#9591). The same goes for the overloads on lines 401-408, and the overloads in typing.pyi. You might want to instead do something similar to what I did with re.Pattern and re.Match in #9592.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, and added some testcases for IO.write.


Expand Down Expand Up @@ -399,11 +399,11 @@ class SpooledTemporaryFile(IO[AnyStr], _SpooledTemporaryFileBase):
def tell(self) -> int: ...
def truncate(self, size: int | None = None) -> None: ... # type: ignore[override]
@overload
def write(self: SpooledTemporaryFile[str], s: str) -> int: ...
def write(self, s: AnyStr) -> int: ...
@overload
def write(self: SpooledTemporaryFile[bytes], s: ReadableBuffer) -> int: ...
@overload
def writelines(self: SpooledTemporaryFile[str], iterable: Iterable[str]) -> None: ...
def writelines(self, iterable: Iterable[AnyStr]) -> None: ...
@overload
def writelines(self: SpooledTemporaryFile[bytes], iterable: Iterable[ReadableBuffer]) -> None: ...
def __iter__(self) -> Iterator[AnyStr]: ... # type: ignore[override]
Expand Down
4 changes: 2 additions & 2 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,13 @@ class IO(Iterator[AnyStr], Generic[AnyStr]):
def writable(self) -> bool: ...
@abstractmethod
@overload
def write(self: IO[str], __s: str) -> int: ...
def write(self, __s: AnyStr) -> int: ...
@abstractmethod
@overload
def write(self: IO[bytes], __s: ReadableBuffer) -> int: ...
@abstractmethod
@overload
def writelines(self: IO[str], __lines: Iterable[str]) -> None: ...
def writelines(self, __lines: Iterable[AnyStr]) -> None: ...
@abstractmethod
@overload
def writelines(self: IO[bytes], __lines: Iterable[ReadableBuffer]) -> None: ...
Expand Down
19 changes: 19 additions & 0 deletions test_cases/stdlib/typing/check_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pickle
from typing import AnyStr, IO


def check_write(io_bytes: IO[bytes], io_str: IO[str], io_anystr: IO[AnyStr], any_str: AnyStr, buf: pickle.PickleBuffer) -> None:
io_bytes.write(b"")
io_bytes.write(buf)
io_bytes.write("") # type: ignore
io_bytes.write(any_str) # type: ignore

io_str.write(b"") # type: ignore
io_str.write(buf) # type: ignore
io_str.write("")
io_str.write(any_str) # type: ignore

io_anystr.write(b"") # type: ignore
io_anystr.write(buf) # type: ignore
io_anystr.write("") # type: ignore
io_anystr.write(any_str)