Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support buffers in IO[bytes].write #9082

Closed
dvarrazzo opened this issue Nov 4, 2022 · 1 comment · Fixed by #9861
Closed

Support buffers in IO[bytes].write #9082

dvarrazzo opened this issue Nov 4, 2022 · 1 comment · Fixed by #9861
Assignees

Comments

@dvarrazzo
Copy link

Trying to annotate psycopg code in order to pass mypy check with disabled bytearray/memoryview promotion to bytes see branch mypy-0990.

Everything was smooth, except one questionable point:

from typing import IO, Union
from typing_extensions import TypeAlias

Buffer: TypeAlias = Union[bytes, bytearray, memoryview]

def write1(f: IO[bytes], data: Buffer) -> None:
    f.write(data)

Mypy 0.990 reports:

error: Argument 1 to "write" of "IO" has incompatible type "Union[bytes, bytearray, memoryview]"; expected "bytes"  [arg-type]

Same using BinaryIO instead of IO[bytes].

As far as I can see, a binary file supports any object supporting the buffer protocol (or maybe less than that? However, the three types in the union tested are definitely supported).

Declaring f: IO[Buffer] almost works: write() doesn't complain anymore, but IO does: Invalid type argument value for "IO".

Maybe something could be relaxed here?

@JelleZijlstra JelleZijlstra self-assigned this Nov 4, 2022
@JelleZijlstra
Copy link
Member

IO.write is declared as def write(self, __s: AnyStr) -> int: ..., so it only accepts exactly bytes if self is an IO[str]. Concrete binary IO classes do support any ReadableBuffer in their write methods, so it makes sense for IO[str] to do too. We can achieve that with overloads on self, similar to how we have handled re.Pattern.

The IO class is problematic because it doesn't exactly represent concrete IO classes well, and typeshed's recommendation is generally to use more specific protocols. However, IO is still widely used, so I think we should fix it. I'll submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants