You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
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:
Mypy 0.990 reports:
Same using
BinaryIO
instead ofIO[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, butIO
does: Invalid type argument value for "IO".Maybe something could be relaxed here?
The text was updated successfully, but these errors were encountered: