与可呼叫的结合的Mypy类型混乱
我对Python Type提示和Mypy相对较新,并且想知道为什么以下代码失败了MyPy验证。
from typing import Callable, Union
class A:
a: int = 1
class B:
b: int = 1
def foo(cb: Union[Callable[[A], None], Callable[[B], None]]):
a = A()
cb(a)
def bar(a: A) -> None:
print(a)
foo(bar)
由于阅读本文,这会失败
test.py:11: error: Argument 1 has incompatible type "A"; expected "B"
Found 1 error in 1 file (checked 1 source file)
,我假设参数cb
可以采用callable [[a],none]
或callable [ [b],无]
,但似乎并非如此?有人可以解释这里发生了什么吗?这是一件杂乱无章的事情,还是实现怪癖,我的假设是错误的,还是以某种方式期望的?
谢谢!
I'm relatively new to Python type hinting and mypy and was wondering why the following code fails mypy verification.
from typing import Callable, Union
class A:
a: int = 1
class B:
b: int = 1
def foo(cb: Union[Callable[[A], None], Callable[[B], None]]):
a = A()
cb(a)
def bar(a: A) -> None:
print(a)
foo(bar)
This fails with the following message
test.py:11: error: Argument 1 has incompatible type "A"; expected "B"
Found 1 error in 1 file (checked 1 source file)
From reading this, I would assume that the argument cb
can either take a Callable[[A], None]
or a Callable[[B], None]
but it seems that's not the case? Can someone explain what's going on here? Is this a mypy thing and an implementation quirk, are my assumptions wrong or is this somehow expected?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foo
只知道您对cb
>:要么是将a
值作为参数的函数,要么是一个函数这将b
值作为参数。它是不是可以使用a
或b
的函数。按原样输入,您无法真正调用
CB
,因为您不知道它接受了哪种类型。 (您知道这是两个,但不是一个。 /em>
a
的实例或b
的实例。您可以将其称为两个实例的值。例如,以下类型检查是因为
c
满足任何一个条件。foo
only knows what you told it aboutcb
: either it is a function that takes anA
value as an argument, or it is a function that takes aB
values as an argument. It is not a function that can take either anA
or aB
.Typed as it is, you can't really call
cb
at all, because you don't know what type it accepts. (You know it's one of two, but not which one.)Update: to be precise, as pointed out by @joel, you can't call it with a value that is only an instance of
A
or an instance ofB
. You could call it with a value that is an instance of both. For example, the following type checksbecause
c
satisfies either condition.