mypy Error TypeVar with Value Restriction 和 Union of Unions / Optional Cannot Pass generic container type

mypy Error TypeVar with Value Restriction and Union of Unions / Optional Cannot Pass generic container type

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

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 的什么?

请注意,我已经尝试抽象出一个基础-class,并让 FooBar 从抽象基础 class 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 修复,但不确定(并且不想检查,待定)。

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