You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromabcimportABC, abstractmethodfromtypingimportAnyclassDataProperty:
def__get__(self, instance: Any, owner: Any) ->str:
return"X"classBadBase1(ABC):
@property@abstractmethoddefid(self) ->str:
raiseNotImplementedError()
classBadBase2(ABC):
id: strclassGoodBase(ABC):
passclassBadDerived1(BadBase1):
id=DataProperty() # Incompatible types in assignment (expression has type "DataProperty", base class "BadBase1" defined the type as "str")classBadDerived2(BadBase2):
id=DataProperty() # Incompatible types in assignment (expression has type "DataProperty", base class "BadBase2" defined the type as "str")classGoodDerived(GoodBase):
id=DataProperty()
reveal_type(BadDerived1().id) # Revealed type is 'Any'reveal_type(BadDerived2().id) # Revealed type is 'Any'reveal_type(GoodDerived().id) # Revealed type is 'builtins.str'
I run mypy without any flags, i.e. "mypy test.py". The mypy version is 0.641, using Python 3.6.5.
Basically I want to have some kind of base class that says "any subclasses will have a member called id and the type of this member is str.
The two cases labelled with "Bad" result in the errors shown in the comments ("incompatible types in assignment" and "revealed type is 'Any'".
The only case that works is the "Good" one, but it only works because the base class does not define anything.
Am I doing something wrong or is this a bug/limitation in mypy?
tmp/descr.py:10: error: Incompatible types in assignment (expression has type "D", base class "A" defined the type as "int")
tmp/descr.py:12: error: Revealed type is 'builtins.int'
tmp/descr.py:13: error: Revealed type is 'Any'
tmp/descr.py:13: error: Cannot determine type of 'x'
I'd expect SuperA.x would be inferred as int, compatible with superclass A.x.
Activity
gvanrossum commentedon Oct 19, 2018
Looks like a bug in the inheritance checking to me. This is a special case, overriding a property with a descriptor.
kedder commentedon Jan 20, 2019
Here's a simpler example:
mypy-0.660 produces this output:
I'd expect
SuperA.xwould be inferred asint, compatible with superclassA.x.Use checkmember.py to check variable overrides (python#18847)