Skip to content

Non generic subclass of generic class do not resolve type correctly #5831

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

Closed
dswistowski opened this issue Oct 24, 2018 · 1 comment
Closed

Comments

@dswistowski
Copy link

dswistowski commented Oct 24, 2018

I have code

from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Dict, Type

T = TypeVar('T')


class GenericClass(Generic[T], ABC):
    children: Dict[str, Type[T]]

    @classmethod
    def by_name(cls, name: str) -> T:
        return cls.children[name]()

    def __init_subclass__(cls):
        super().__init_subclass__()
        try:
            cls.children[cls.__name__.lower()] = cls
        except AttributeError:
            cls.children = {}


class NonGenericBase(GenericClass['NonGenericBase'], ABC):
    @abstractmethod
    def my_method(self) -> bool:
        pass


class Child(NonGenericBase):
    def my_method(self) -> bool:
        return True


NonGenericBase.by_name('child').my_method()

Checking that code with mypy generates:
test.py:33: error: "T" has no attribute "my_method"

From my understanding type of NonGenericBase.by_name should be Callable[[str], NonGenericBase] (because NonGenericBase is GenericClass['NonGenericBase']) but it looks like it is Callable[[str], T]

@ilevkivskyi
Copy link
Member

This is a duplicate of #3645 (we already have a PR that aims to fix this, so hopefully this will be fixed in the next release).

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