Skip to content
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

Incompatible types when calling generinc __init__ from generic @classmethod #17937

Open
bjoernpollex opened this issue Oct 14, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@bjoernpollex
Copy link

Bug Report

When calling a generic __init__ from a generic @classmethod on a class that is itself not generic, mypy reports incompatible types.

To Reproduce

from typing import TypeVar

T = TypeVar("T")


class Foo:
    @classmethod
    def make_foo(cls, arg: T) -> None:
        cls(arg)

    def __init__(self, arg: T) -> None:
        pass

Expected Behavior

mypy reports no errors.

Actual Behavior

error: Argument 1 to "Foo" has incompatible type "T@make_foo"; expected "T@__init__"

Your Environment

  • Mypy version used: mypy 1.11.2 (compiled: yes)
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.11.7
@bjoernpollex bjoernpollex added the bug mypy got something wrong label Oct 14, 2024
@aatle
Copy link
Contributor

aatle commented Oct 15, 2024

From more investigation, the specific problem is that a when a variable of type type[C] is called, it's constructor type variables reject all types except Any. (classmethod gives cls the type type[C].)

# --strict
from typing import TypeVar
from collections.abc import Callable

T = TypeVar("T")

class C:
    def __init__(self, arg1: T, arg2: T):
        pass

C(1, 1)  # okay

bar: Callable[[object, object], C] = C
bar(1, 1)  # okay

foo: type[C]
foo(1, 1)  # error
# main.py:17: error: Argument 1 to "C" has incompatible type "int"; expected "T"  [arg-type]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants