Skip to content

Overridden attribute should inherit the type #7505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jirkadanek opened this issue Sep 12, 2019 · 1 comment
Closed

Overridden attribute should inherit the type #7505

jirkadanek opened this issue Sep 12, 2019 · 1 comment

Comments

@jirkadanek
Copy link

jirkadanek commented Sep 12, 2019

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

@jirkadanek jirkadanek changed the title Overriden attribute should inherit the type Overridden attribute should inherit the type Sep 12, 2019
@ilevkivskyi
Copy link
Member

Duplicate of #4547

@ilevkivskyi ilevkivskyi marked this as a duplicate of #4547 Sep 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants