Skip to content

parse_stream gives incompatible type "IO[bytes]"; expected "BinaryIO" #19

@shearer12345

Description

@shearer12345

I'm parsing a stream from a zip file, and mypy shows the following error
Argument 1 to "parse_stream" of "Construct" has incompatible type "IO[bytes]"; expected "BinaryIO" [arg-type] (11:32)

I believe that parse_stream should also be happy with IO[bytes] - see python/typeshed#7843 for some discussion - but I'm a little out of my depth on this part of Python typing.

The following examples will reproduce this mypy error

from zipfile import ZipFile

from construct import Struct, GreedyBytes

s = Struct(
    'test' / GreedyBytes
)

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        parsed = s.parse_stream(myfile)

Changing StreamType = t.BinaryIO to StreamType = t.Union[t.BinaryIO, t.IO[bytes]] in construct-stubs/core.pyi resolves this.
I'm happy to create a PR if that seems like the appropriate solution

Thanks so much for construct-typing :)

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