gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:12:03 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com