有什么方法可以使Mypy意识到我知道要处理的对象是什么类型,而无需在每个if语句中调用isinstance
?我想使用辅助函数来执行此类操作,但是即使我在辅助函数中具有isinstance
,Mypy也会抱怨。我也尝试使用typing.Union
来解决此问题,但是也遇到了类似的问题。
import typing
class A:
def __init__(self, a):
self.a = a
def is_b(self):
return isinstance(self, B)
def is_c(self):
return isinstance(self, C)
class B(A):
def __init__(self, a, b):
self.b = b
super().__init__(a)
class C(A):
def __init__(self, a, c):
self.c = c
super().__init__(a)
a_list: typing.List[A] = []
for i in range(0, 10):
b_or_c: A
if i % 2 == 0:
b_or_c = B('a' + str(i), 'b' + str(i))
else:
b_or_c = C('a' + str(i), 'c' + str(i))
a_list.append(b_or_c)
for b_or_c in a_list:
print(type(b_or_c))
if b_or_c.is_b():
print(b_or_c.b) # Mypy Error: "A" has no attribute "b"
if b_or_c.is_c():
print(b_or_c.c) # Mypy Error: "A" has no attribute "c"
if isinstance(b_or_c, B):
print(b_or_c.b) # No Mypy Error
if isinstance(b_or_c, C):
print(b_or_c.c) # No Mypy Error
最佳答案
考虑从此更改您的API:
def is_c(self) -> bool:
return isinstance(self, C)
对此:
def as_c(self) -> Optional[C]:
if isinstance(self, C):
return self
return None
这样可以完全避免问题。您可以这样使用它:
c_opt = b_or_c.as_c()
if c_opt is not None:
print(c_opt.c)
关于python - 如何使Mypy意识到isinstance已经被调用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57015161/