Skip to content

"BinaryIO" has no attribute "peek" #8433

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

Closed
jnsnow opened this issue Feb 22, 2020 · 2 comments
Closed

"BinaryIO" has no attribute "peek" #8433

jnsnow opened this issue Feb 22, 2020 · 2 comments

Comments

@jnsnow
Copy link

jnsnow commented Feb 22, 2020

  • I think this is a bug, but whether it's in mypy or typeshed I do not know.
  • I am running with mypy --strict.

Mypy version: 0.670

I've got a snippet like this:

with open(self.path, "rb") as infile:
    if infile.peek(2)[0:2] == b'\x1f\x8b':
        ...

mypy guesses that this type is BinaryIO, but that it does not have the peek() method. That type is correct with regard to the abstract types of things open() can return, but in this case it's an _io.BufferedReader, which does have peek().

@jnsnow
Copy link
Author

jnsnow commented Feb 22, 2020

I came across #2166, but this looks more like stub definition problems. This case appears to be that peek is really only applicable to a very specific subclass that open() can return to which BinaryIO is not meant to apply, necessarily.

For now, I suppose my workaround is this:

with cast(io.BufferedReader, open(self.path, "rb")) as infile:
    if infile.peek(2)[0:2] == b'\x1f\x8b':
        ...

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 28, 2020

BinaryIO and open are defined in typeshed. If you believe that this could be improved, you can file a typeshed issue.

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

No branches or pull requests

2 participants