加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-3.x – 二进制文件类对象的正确类型

发布时间:2020-12-20 13:19:19 所属栏目:Python 来源:网络整理
导读:我使用 Python类型提示定义了以下函数: from typing import BinaryIOdef 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)在我调用
我使用 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

我在这里使用的正确类型是什么? PEP484将BinaryIO定义为“IO [字节]的简单子类型”. FileIO不符合IO吗?

解决方法

这看起来像是Pycharm或输入模块中的错误.从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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读