Skip to content

[1.16 regression] Incorrect inferred type when using a classmethod from a different class as an attribute #19146

Closed
@delfick

Description

@delfick

Hello,

I decided to try out the new mypy 1.16 branch on the codebase I work on and I found what I think is a bug.

Let's say you have the following code (makes more sense in the concrete example it comes from)

from collections.abc import Iterable
from typing import Protocol


class GetActions(Protocol):
    def __call__(self, event_type: str) -> Iterable[str]: ...


class Parent:
    get_actions: GetActions


class ActionsGetter:
    @classmethod
    def get_actions(self, event_type: str) -> Iterable[str]:
        return ("1",)


class Child(Parent):
    get_actions = ActionsGetter.get_actions


print(Child().get_actions("one"))

This works at runtime but at static time it complains with

a.py:20: error: Incompatible types in assignment (expression has type "Callable[[], Iterable[str]]", base class "Parent" defined the type as "GetActions")  [assignment]
a.py:20: note: "GetActions.__call__" has type "Callable[[Arg(str, 'event_type')], Iterable[str]]"
Found 1 error in 1 file (checked 1 source file)

Where it believes that the type of Child.get_actions is Callable[[], Iterable[str]] instead of Callable[[Arg(str, 'event_type')], Iterable[str]]

Your Environment

  • Mypy version used: release-1.16 (revision 96525a2, built locally with mypy_mypyc-wheels for cp312-macosx_arm64)
  • Mypy command-line flags: mypy a.py
  • Mypy configuration options from mypy.ini (and other config files): no config
  • Python version used: python3.12

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