mypy icon indicating copy to clipboard operation
mypy copied to clipboard

1.11 Regression: `Self` in classmethod causes "type[Self] not callable" error

Open atugushev opened this issue 1 year ago • 2 comments

Bug Report

A regression bug has been identified in mypy version 1.11.1, where the use of typing.Self in class method factory functions triggers an error. This issue does not occur in mypy version 1.10.0. Specifically, attempting to use functools.partial with typing.Self results in the error: "type[Self]" not callable.

Description generated using AI.

To Reproduce

  1. Create t.py file with the following content down below.
import functools, time, typing


class LogEntry:
    def __init__(self, ts: float, msg: str) -> None:
        self.ts = ts
        self.msg = msg

    @classmethod
    async def from_msg_self_typed(cls, msg: str) -> typing.Self:
        factory = functools.partial(cls, ts=time.time())  # Fails here with: "type[Self]" not callable
        return factory(msg=msg)

    @classmethod
    async def from_msg_class_typed(cls, msg: str) -> "LogEntry":
        factory = functools.partial(cls, ts=time.time())  # Works fine
        return factory(msg=msg)
  1. Run mypy t.py

Expected Behavior

No errors.

Actual Behavior

$ pip install mypy==1.10.0
$ mypy t.py
Success: no issues found in 1 source file

$ pip install mypy==1.11.1
t.py:11: error: "type[Self]" not callable  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.11.1 (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 (main, Dec 25 2023, 16:41:22) [Clang 15.0.0 (clang-1500.1.0.2.5)]

atugushev avatar Aug 10 '24 11:08 atugushev

Thanks — this is a duplicate of #17646. Internally Self is just implemented as a special kind of TypeVar, and with mypy's new functools.partial plugin it's no longer respecting the upper bounds of TypeVars when deciding if an object is callable for the purposes of passing that object to functools.partial

AlexWaygood avatar Aug 10 '24 11:08 AlexWaygood

@AlexWaygood thanks!

atugushev avatar Aug 10 '24 11:08 atugushev