Skip to content

Overridden attribute should inherit the type #7505

@jirkadanek

Description

@jirkadanek
from typing import List

class A(object):
    a = []  # type: List[int]

class B(A):
    a = []

This pattern of declaring an attribute in a superclass and then overriding the value with empty list or dict is fairly common in my codebase. Therefore, when mypy forces me to annotate all the places where override happens, it is fairly painful.

% mypy  ab.py
ab.py:7: error: Need type annotation for 'a' (hint: "a: List[<type>] = ...")

% mypy  --py2 ab.py
ab.py:7: error: Need type comment for 'a' (hint: "a = ...  # type: List[<type>]")

I am inclined to call the behavior a bug, I'd expect no error, and both A.a and B.a to be considered of type List[int] by mypy in the above code.

% mypy --version
mypy 0.720

Seems related to #3903

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