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

File streams: "BufferedWriter" has incompatible type "BinaryIO"; expected "RawIOBase" #10271

Closed
h6197627 opened this issue Mar 31, 2021 · 7 comments
Labels
bug mypy got something wrong

Comments

@h6197627
Copy link

Bug Report
This code

import io

bf = open('binary.file', 'wb', buffering=0)
print(isinstance(bf, io.RawIOBase)) # > True
bw = io.BufferedWriter(bf)

throws:

test.py:5: error: Argument 1 to "BufferedWriter" has incompatible type "BinaryIO"; expected "RawIOBase"

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.6.9
  • Operating system and version: Ubuntu 18.04
@h6197627 h6197627 added the bug mypy got something wrong label Mar 31, 2021
@JelleZijlstra
Copy link
Member

I fixed this in typeshed a while ago with a serious of overloads for open that return precise types. Maybe that didn't make it into the last mypy release? It should pick the overload at https://github.com/python/typeshed/blob/master/stdlib/builtins.pyi#L1084.

@h6197627
Copy link
Author

Just checked, I have

# Unbuffered binary mode: returns a FileIO
@overload
def open(
    file: _OpenFile,
    mode: OpenBinaryMode,
    buffering: Literal[0],
    encoding: None = ...,
    errors: None = ...,
    newline: None = ...,
    closefd: bool = ...,
    opener: Optional[_Opener] = ...,
) -> FileIO: ...

in /usr/local/lib/python3.6/dist-packages/mypy/typeshed/stdlib/3/builtins.pyi

@srittau
Copy link
Contributor

srittau commented Apr 1, 2021

I can confirm. With mypy 0.812 it doesn't pick up the correct overload, although it exists in the bundled typeshed and it looks like it should pick it.

@JelleZijlstra
Copy link
Member

It's actually because mypy still has a custom plugin for open that produces less precise types. #9275 removes it, but hasn't been merged yet.

@h6197627
Copy link
Author

Hi,
any progress on this issue? :)

@h6197627
Copy link
Author

Fixed with #9275

@lifepillar
Copy link

lifepillar commented Jun 7, 2022

I can still reproduce it with mypy 0.931:

$ mypy test.py
foo.py:5:24: error: Argument 1 to "BufferedWriter" has incompatible type "BinaryIO"; expected "RawIOBase"
Found 1 error in 1 file (checked 1 source file)
$ mypy --version
mypy 0.931

Edit: …but not with mypy 0.961. Ok then, I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants