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'm working on a framework with various storage backends. Those backends all implement an abstract base class. The backend classes are stored in a mapping from the backend name to the class implementing that backend.
We want to be able to perform type checking with mypy, and annotate as follows:
importabcimporttypingclassA(metaclass=abc.ABCMeta): # The abstract base classdef__init__(self, name: str) ->None:
self.name=name@abc.abstractmethoddefget_name(self):
passclassB(A): # Some non-abstract backenddefget_name(self):
returnf'B: {self.name}'classC(A): # Another non-abstract backenddefget_name(self):
returnf'C: {self.name}'backends: typing.Mapping[str, typing.Type[A]] = {
'backend-b': B,
'backend-c': C,
}
if__name__=='__main__':
backend_cls=backends['backend-c']
# The following line causes an error with mypy:instance=backend_cls('demo-name')
print(f'Name is: {instance.get_name()}')
Running mypy-0.501 gives this error:
typingtest.py:32: error: Cannot instantiate abstract class 'A' with abstract attribute 'get_name'
My question: How can we annotate the mapping backends such that mypy understands it only contains non-abstract subclasses of A?
The text was updated successfully, but these errors were encountered:
Hello there,
I'm working on a framework with various storage backends. Those backends all implement an abstract base class. The backend classes are stored in a mapping from the backend name to the class implementing that backend.
We want to be able to perform type checking with mypy, and annotate as follows:
Running mypy-0.501 gives this error:
typingtest.py:32: error: Cannot instantiate abstract class 'A' with abstract attribute 'get_name'
My question: How can we annotate the mapping
backends
such that mypy understands it only contains non-abstract subclasses ofA
?The text was updated successfully, but these errors were encountered: