Consider this code
from abc import ABCMeta, abstractmethod class C(): @abstractmethod def my_abstract_method(self): print('foo') class D(C): pass x = C() y = D() Neither x nor y is allowed by mypy yielding me a
test.py:13: error: Cannot instantiate abstract class 'C' with abstract attribute 'my_abstract_method' test.py:15: error: Cannot instantiate abstract class 'D' with abstract attribute 'my_abstract_method' I'm testing this with mypy 0.570 and python 3.6.3
However, the documentation says that I'd need to set metaclass=ABCMeta for that to work. What am I missing?
0.570. Btw, I'm getting no python errors directly no matter if I apply themetaclass=ABCMetaor not. Somypyis the only tool that actually helps me to catch this error anyway.mypyandpythondo two very different things.mypyis a static analyzer, and is free to make the assumption that you forgot to useABCMetaif@abstractmethodis present in the source of your code.pythonjust interprets the code, and withoutABCMeta, it has no reason to do anything with the list of methods populated by@abstractmethod. (Indeed, at runtime there is no evidence in the method itself that it was decorated.)mypycatches the error without themetaclassbeing set and I didn't further check whatpythonactually does. I just stopped there left wondering whymypycatches the error when I thought it shouldn't. Thanks for the background info!