Skip to content
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

Inherited, overridden attribute type reverted to the top parent class type #7394

Open
rooterkyberian opened this issue Aug 26, 2019 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-inheritance Inheritance and incompatible overrides

Comments

@rooterkyberian
Copy link

rooterkyberian commented Aug 26, 2019

The issue type is probably quite bad, but example should quickly clear things up:

class A:
    attr = None


class B(A):
    attr: str  # type: ignore


class C1(B):
    attr = "test"


class C2(B):
    pass


class D(C2):
    attr = "test"

mypy test.py outputs:

test.py:18: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "None")

where line 18 is the last line of above example

As to why I do attr: str # type: ignore - in a nutshell, in my case A class comes from external source and they botched type hints there so I try to fix them in B. Not sure if it should have worked for C1, but if it did I see it breaking in D as a bug due mypy not being consistent, but then again maybe there is a good reason for it?.

Tested with mypy 0.720

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 27, 2019

Yeah, this looks like a mypy bug. Thanks for reporting this!

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Aug 27, 2019
@JukkaL JukkaL changed the title bug: inhertied, overriden attribute type reverted to the top parent class type Inherited, overriden attribute type reverted to the top parent class type Aug 27, 2019
@JukkaL JukkaL changed the title Inherited, overriden attribute type reverted to the top parent class type Inherited, overridden attribute type reverted to the top parent class type Aug 27, 2019
@ilevkivskyi
Copy link
Member

Closely related to #7336 (although it is less straightforward for multiple inheritance).

@AlexWaygood AlexWaygood added the topic-inheritance Inheritance and incompatible overrides label Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

4 participants