New submission from conchylicultor <etiennefg....@gmail.com>:
Currently, because `BinaryIO` is subclass of `IO[bytes]`, the 2 cannot be used interchangeably. Example with pytype: ``` def iter_zip(arch_f) -> List[typing.BinaryIO]]: with open(arch_f, 'rb') as fobj: z = zipfile.ZipFile(fobj) return [z.open(member) for member in z.infolist()] ``` Raise: ``` Expected: Tuple[str, BinaryIO] Actually returned: Tuple[Any, IO[bytes]] ``` Technically pytype is right as `ZipFile.open() -> IO[bytes]`: https://github.com/python/typeshed/blob/ca45cb21a8a0422cbb266cb25e08051fe481887c/stdlib/2and3/zipfile.pyi#L109 But this makes BinaryIO usage quite confusing. >From the implementation, it seems that the BinaryIO is slightly different >(https://github.com/python/cpython/blob/9f004634a2bf50c782e223e2eb386ffa769b901c/Lib/typing.py#L2094): But the documentation is unclear about the difference between the 2 https://docs.python.org/3/library/typing.html#typing.BinaryIO Additionally, `typing.IO` is implemented as Generic but shouldn't this be a Protocol instead ? ---------- messages: 382143 nosy: conchylicultor priority: normal severity: normal status: open title: Confusing that BinaryIO and IO[bytes] cannot be used interchangeably _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue42512> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com