Skip to content

"BinaryIO" has no attribute "peek" #8433

Closed

Description

@jnsnow
  • 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().

Activity

  1. jnsnow commented on Feb 22, 2020

    @jnsnow
    Author

    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':
            ...
  2. JukkaL commented on Feb 28, 2020

    @JukkaL
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions