Skip to content

Annotation Overridden Through Multilevel Inheritance #6754

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
WillAyd opened this issue May 2, 2019 · 1 comment
Closed

Annotation Overridden Through Multilevel Inheritance #6754

WillAyd opened this issue May 2, 2019 · 1 comment

Comments

@WillAyd
Copy link

WillAyd commented May 2, 2019

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

@WillAyd WillAyd changed the title Annotation Getting Overridden Through Multilevel Inheritance Annotation Overridden Through Multilevel Inheritance May 2, 2019
@ilevkivskyi
Copy link
Member

I may be equally (if not more) surprising if something like this would fail:

x: int = Derived1.attr  # if the type of attr would still be union, this would fail

There are some situation where keeping supertype context is desirable, but this is already discussed in #4547

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