Skip to content

supertype incompatibility with subclass classmethod #8495

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
bephinix opened this issue Mar 5, 2020 · 1 comment
Closed

supertype incompatibility with subclass classmethod #8495

bephinix opened this issue Mar 5, 2020 · 1 comment

Comments

@bephinix
Copy link

bephinix commented Mar 5, 2020

I am currently trying to apply type hints to a base class and its subclasses which override a classmethod:

from abc import ABCMeta, abstractmethod
from typing import TypeVar, Type

class MyBase(metaclass=ABCMeta):
    @classmethod
    @abstractmethod
    def foo(cls) -> MyBase:
        pass


class MyChild(MyBase):
    @classmethod
    def foo(cls) -> MyChild:
        return MyChild()

_MyT = TypeVar("_MyT", bound=MyBase)

def bar(clazz: Type[_MyT]) -> _MyT:
    return clazz.foo()

Error:

example.py:19: error: Incompatible return value type (got "MyBase", expected "_MyT")

This seems to be incorrect, as returing MyBase is in bounds of _MyT.

I have found #2511 and its fix #7474 which should be merged in 0.761 but I still get this error.

Python: 3.8.1
MyPy: 0.761 (no custom flags)

@bephinix
Copy link
Author

bephinix commented Mar 5, 2020

Using def foo(cls: Type[_MyT]) -> _MyT: solved this issue.

@bephinix bephinix closed this as completed Mar 5, 2020
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

1 participant