Skip to content

supertype incompatibility with subclass classmethod #8495

Closed
@bephinix

Description

@bephinix

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions