Skip to content

False positive when calling a method from a subclass #15097

Closed
@Aegdesil

Description

@Aegdesil

Bug Report

I encounter a false positive using mypy when a Generic class indirectly calls a method defined in its GrandParent.

To Reproduce

from typing import Generic, TypeVar

T = TypeVar("T")


class Grandparent(Generic[T]):
    def say_hello(self) -> str:
        return "Hello"


class Parent(Grandparent[T]):
    pass


class Child(Parent[T]):
    def say_hello(self) -> str:
        return Parent.say_hello(self)

Expected Behavior

No error when running mypy.

Actual Behavior

I get this error:
error: Argument 1 to "say_hello" of "Grandparent" has incompatible type "Child[T]"; expected "Grandparent[T]" [arg-type]

Removing the Generic types, calling Grandparent.say_hello(self) from the child, redefining the method in the Parent fixes the error.

Your Environment

  • Mypy version used: 1.2.0
  • Python version used: 3.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions