有没有办法让Mypy不打电话就知道我在处理什么类型的对象
isinstance
在每个if语句中?我希望有助手函数来做这类事情,但即使我有
存在
在helper函数中,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