python - mypy Error TypeVar with Value Restriction and Union of Unions/Optional 无法传递通用容器类型

标签 python python-3.x generics mypy union-types

因此,下面的示例显然是人为设计的,但我试图对我的实际情况保持一些真实性。既然我已经削减了这一点,我确信我遗漏了一些明显的东西。考虑几种类型和一个受限联合:

from typing import Union, TypeVar, Optional, Generic, overload

class Foo:
    def __init__(self, x: int)-> None:
        self.x = x
    def frobnicate(self) -> 'Foo':
        return Foo((self.x + 42) // 42)

class Bar:
    def __init__(self, y: int) -> None:
        self.y = y
    def frobnicate(self) -> 'Bar':
        return Bar(self.y + 88)

MyType = TypeVar('MyType', Foo, Bar)
class Container(Generic[MyType]):
    val: Optional[MyType]
    def __init__(self, val: Optional[MyType]=None) -> None:
        self.val = val

def transmogrify(arg: Optional[MyType]) -> Optional[MyType]:
    if arg is None:
        return None
    else:
        return arg.frobnicate()

def extract_stuff(x: Optional[int], cont: Container[MyType]) -> Optional[MyType]:
    result: Optional[MyType]
    if x is None:
        result = None
    elif x == 88 or x == 42:
        result = transmogrify(cont.val)
    else:
        result = cont.val
    return result

当我尝试使用 mypy 对此进行类型检查时,出现以下错误:

mcve3.py:32: error: Value of type variable "MyType" of "transmogrify" cannot be "Optional[Foo]"
mcve3.py:32: error: Value of type variable "MyType" of "transmogrify" cannot be "Optional[Bar]"

我无法理解这一点。我怀疑这是许多嵌套联合的问题?请注意,在我的实际代码中,我使用的是自定义单例枚举 Null,因此无论您在哪里看到 Optional[Something],它实际上都是 Union[Something, Null],但我认为这没有什么不同。

现在,如果我删除 Optional,即 Union,一切都很好:

from typing import Union, TypeVar, Optional, Generic, overload

class Foo:
    def __init__(self, x: int)-> None:
        self.x = x
    def frobnicate(self) -> 'Foo':
        return Foo((self.x + 42) // 42)

class Bar:
    def __init__(self, y: int) -> None:
        self.y = y
    def frobnicate(self) -> 'Bar':
        return Bar(self.y + 88)

MyType = TypeVar('MyType', Foo, Bar)
class Container(Generic[MyType]):
    val: MyType
    def __init__(self, val: MyType) -> None:
        self.val = val

def transmogrify(arg: MyType) -> MyType:
    if arg is None:
        return None
    else:
        return arg.frobnicate()

def extract_stuff(x: int, cont: Container[MyType]) -> MyType:
    if x is None:
        return None
    elif x == 88 or x == 42:
        return transmogrify(cont.val)
    else:
        return cont.val

关于 Union 在这里我错过了什么?

请注意,我已经尝试抽象出一个基类,并让 FooBar 派生自抽象基类 class MyType(metaclass=abc .Meta),但会弹出一个非常相似的错误。

编辑添加:

(py37) Juans-MBP: juan$ mypy --version
mypy 0.620

最佳答案

这似乎是最近在 mypy 中修复的一个错误。我能够使用 mypy 0.630 重现你的第一个代码片段中的问题,但无法同时使用 mypy 0.641 和 master 上最新版本的 mypy 重现。

我非常怀疑这个错误是由 https://github.com/python/mypy/pull/5699 修复的,但不确定(并且不想检查,tbh)。

可以监控mypy's blog如果您希望收到 future 版本的通知以避免将来出现类似情况。新版本大约每 6 周到两个月左右发布一次。 -- 下一个版本预计在撰写本文时大约两周后发布。

关于python - mypy Error TypeVar with Value Restriction and Union of Unions/Optional 无法传递通用容器类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526814/

相关文章:

python - 收到 TypeError : Required argument 'object' (pos 1) not found

swift - Swift 中 X 的通用集合?

python - 根据另一列值修改数据框列

python - 在很多类 : DRY? 上定义相同的方法覆盖

python - 使用 bool AND 计算行中字符串的出现次数

python - 在 python3 字节对象中测试 ASCII 字符的最佳方法

java - 使用通配符和类型化泛型会生成 "is not applicable for the arguments"错误

java - 一般问题。遗产

python - 将 QTextEdit 的内容保存为 *.pdf?

python - 使用 Django-Haystack 和 ElasticSearch 捕获 ConnectionError