Closed
Description
I am trying to get this code working:
from abc import ABC, abstractmethod
from typing import Any
class DataProperty:
def __get__(self, instance: Any, owner: Any) -> str:
return "X"
class BadBase1(ABC):
@property
@abstractmethod
def id(self) -> str:
raise NotImplementedError()
class BadBase2(ABC):
id: str
class GoodBase(ABC):
pass
class BadDerived1(BadBase1):
id = DataProperty() # Incompatible types in assignment (expression has type "DataProperty", base class "BadBase1" defined the type as "str")
class BadDerived2(BadBase2):
id = DataProperty() # Incompatible types in assignment (expression has type "DataProperty", base class "BadBase2" defined the type as "str")
class GoodDerived(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?