为什么 Sequence 是 mypy 中不支持的 + 操作数类型? [英] Why is Sequence an unsupported operand type for + in mypy?

查看:47
本文介绍了为什么 Sequence 是 mypy 中不支持的 + 操作数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mypy 给出的错误是 Sequence[str] 不是 + 运算符支持的操作数类型:

mypy is giving an error that Sequence[str] is not a supported operand type for the + operator:

# test.py

from typing import Sequence


def test(x: Sequence[str], y: Sequence[str]) -> Sequence[str]:
    return x + y

$ mypy test.py
test.py:5: error: Unsupported left operand type for + ("Sequence[str]")
Found 1 error in 1 file (checked 1 source file)

pytype 给出了类似的错误:

pytype gives a similar error:

$ pytype test.py 

[...]

  No attribute '__add__' on Sequence[str] or '__radd__' on Sequence[str]

[...]

为什么 Sequence[str]+ 不支持的操作数类型?

Why is Sequence[str] an unsupported operand type for +?

推荐答案

根据文档,序列不一定实现 __add__:

According to the doc, sequences do not necessarily implement __add__:

https://docs.python.org/3/glossary.html#term-sequence

不支持连接的 Sequence 示例是 range.

An example of a Sequence that doesn't support concatenation would be a range.

这篇关于为什么 Sequence 是 mypy 中不支持的 + 操作数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆