Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type annotation for map with abstract classes as values #3048

Closed
sybrenstuvel opened this issue Mar 23, 2017 · 1 comment
Closed

Type annotation for map with abstract classes as values #3048

sybrenstuvel opened this issue Mar 23, 2017 · 1 comment

Comments

@sybrenstuvel
Copy link

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:

import abc
import typing


class A(metaclass=abc.ABCMeta):  # The abstract base class
    def __init__(self, name: str) -> None:
        self.name = name

    @abc.abstractmethod
    def get_name(self):
        pass


class B(A):  # Some non-abstract backend
    def get_name(self):
        return f'B: {self.name}'


class C(A):  # Another non-abstract backend
    def get_name(self):
        return f'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?

@gvanrossum
Copy link
Member

Should be fixed by #2853, so closing as a duplicate of #1843.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants