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
I've been trying to mypy to typecheck subclasses that need to provide a required value for a ClassVar in a base class that cannot provide the value itself (only has an unintialized attribute marker). The problem is that badly implemented subclasses can ignore this attribute without mypy raising errors about it (though interestingly if the value provided has incorrect type, mypy will raise errors about that).
To Reproduce
fromabcimportABCfromtypingimportClassVar, TypeclassBase(ABC):
my_super_important_value: ClassVar[str]
classGoodSubclass(Base):
my_super_important_value="1337"# I want mypy to error about an unimplemented variable but it doesn'tclassBadSubclass(Base):
passdefprint_abc(cls: Type[Base]) ->None:
print(cls.my_super_important_value)
print_abc(GoodSubclass)
# 1337print_abc(BadSubclass)
# AttributeError: type object 'BadSubclass' has no attribute 'my_super_important_value'
14:0: Uninitialized attribute [13]: Attribute `my_super_important_value` inherited from abstract class `Base` in class `BadSubclass` to have type `str` but is never initialized.
The text was updated successfully, but these errors were encountered:
Note that you can almost accomplish something like this this way:
fromtypingimportClassVarfromabcimportabstractmethodclassFoo:
@classmethod@property@abstractmethoddefx(cls) ->str:
pass@property@abstractmethoddefy(self) ->str:
passclassBar(Foo):
y='hi'print(Bar().y) # error: Cannot instantiate abstract class "Bar" with abstract attribute "x" [abstract]
But mypy will also complain that "Only instance methods can be decorated with @Property", and it will also accept an instance-level property definition for x.
Bug Report
I've been trying to mypy to typecheck subclasses that need to provide a required value for a
ClassVar
in a base class that cannot provide the value itself (only has an unintialized attribute marker). The problem is that badly implemented subclasses can ignore this attribute without mypy raising errors about it (though interestingly if the value provided has incorrect type, mypy will raise errors about that).To Reproduce
mypy playground
Expected Behavior
Expected
mypy
to show an error about theBadSubclass
class not providing a value formy_super_important_value
.Actual Behavior
No errors.
Your Environment
mypy.ini
(and other config files): N/ANote that the same code fails when using the Pyre type checker with this error:
The text was updated successfully, but these errors were encountered: