Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Description

@atugushev

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)]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-self-typesTypes for self

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions