【问题标题】:Expected type Union[str, bytes, int] but got Sequence[Union[int, float, str]]预期类型 Union[str, bytes, int] 但得到 Sequence[Union[int, float, str]]
【发布时间】:2020-07-21 04:42:28
【问题描述】:

PyCharm 显示此警告,我不知道为什么。

def record(self, *data: Sequence[Union[int, float, str]]) -> None:

    for field, value in zip(self.fields, data):
        if field.type in {1, 3}:
            try:
                value = int(value)  # warning is here
            except ValueError:
                pass

    # other logic...

说解压后的zip 中的value 与参数data 的类型相同,但它不是也不应该是。如果它是 Sequence 的元素,则意味着它将是 Union[int, float, str] 类型。

PyCharm 是否没有意识到 zip 已解包?

【问题讨论】:

    标签: python linter


    【解决方案1】:

    根据PEP 484,类型提示适用于*data 的每个元素,而不是序列本身。你不需要Sequence* 已经暗示了这一点。

    def record(self, *data: Union[int, float, str]) -> None:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-29
      • 2019-03-07
      • 2016-01-27
      • 1970-01-01
      • 2017-01-19
      • 2018-12-29
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多