Skip to content

Annotation Overridden Through Multilevel Inheritance #6754

Closed
@WillAyd

Description

@WillAyd

Assuming a class structure as follows:

class Base:
    attr = None  # type: Union[int, str]

class Derived1(Base):
    attr = 1

class Derived2(Derived1):
    attr = 'foo'

I was surprised to be getting the following error with Mypy:

error: Incompatible types in assignment (expression has type "str", base class "Derived1" defined the type as "int”)

Within the scope of Derived1 it makes sense to me that mypy may restrict the type to just int, but in the subsequent child class of Dervied2 I was surprised that the implicit annotation in Dervied1 took precedence over the explicit annotation provided in the Base class

I was getting this error with Python 3.7.2 on mypy master

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions