Given the following code:
from typing import Tuple
class Grandparent:
items: Tuple[str, ...] = ()
class Parent(Grandparent):
items = ('foo',)
class Child(Parent):
items = ('foo', 'bar')
mypy reports the following error:
error: Incompatible types in assignment (expression has type "Tuple[str, str]", base class "Parent" defined the type as "Tuple[str]")
Changing the code like this (specifying the same type again in the Parent class) satisfies mypy:
from typing import Tuple
class Grandparent:
items: Tuple[str, ...] = ()
class Parent(Grandparent):
items: Tuple[str, ...] = ('foo',)
class Child(Parent):
items = ('foo', 'bar')
Why do I need to re-specify the same type for items at multiple places in the class hierarchy, given the assignment of items in all places satisfies the same/original definition? And is there a way to avoid needing to do this?