二进制类文件对象的正确类型

Correct type for binary file-like object

我使用 Python 类型提示定义了以下函数:

from typing import BinaryIO

def do_something(filename: str):
    my_file = open(filename, "rb")
    read_data(my_file)

def read_data(some_binary_readable_thing: BinaryIO):
    pass

但是我的 IDE (PyCharm 2017.2) 在我调用 read_file:

的行中给出了以下警告
Expected type 'BinaryIO', got 'FileIO[bytes]' instead

我在这里使用的正确类型是什么? PEP484BinaryIO 定义为 "a simple subtype of IO[bytes]"。 FileIO不符合IO吗?

这看起来像是 Pycharm 或 typing 模块中的错误。来自 typing.py 模块:

class BinaryIO(IO[bytes]):
    """Typed version of the return of open() in binary mode."""
    ...

此外 documentation 指定:

These represent the types of I/O streams such as returned by open().

所以它应该按规定工作。目前,解决方法是显式使用 FileIO.

from io import FileIO

def do_something(filename: str):
    my_file = open(filename, "rb")
    read_data(my_file)

def read_data(some_binary_readable_thing: FileIO[bytes]):
    pass