This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: typing: Confusing that BinaryIO and IO[bytes] cannot be used interchangeably
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: conchylicultor
Priority: normal Keywords:

Created on 2020-11-30 11:17 by conchylicultor, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg382143 - (view) Author: Etienne POT (conchylicultor) * Date: 2020-11-30 11:17
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 ?
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86678
2020-11-30 11:34:54vstinnersettitle: Confusing that BinaryIO and IO[bytes] cannot be used interchangeably -> typing: Confusing that BinaryIO and IO[bytes] cannot be used interchangeably
2020-11-30 11:18:53conchylicultorsettype: behavior
components: + Library (Lib)
versions: + Python 3.10
2020-11-30 11:17:54conchylicultorcreate