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

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

Open
atugushev opened this issue Aug 10, 2024 · 2 comments
Open
Labels
bug mypy got something wrong topic-self-types Types for self

Comments

@atugushev
Copy link
Contributor

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 atugushev added the bug mypy got something wrong label Aug 10, 2024
@AlexWaygood
Copy link
Member

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

@atugushev
Copy link
Contributor Author

@AlexWaygood thanks!

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

No branches or pull requests

3 participants