Skip to content

Return type is Self when calling an inherited decorated classmethod that returns Self #19023

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

Closed
injust opened this issue May 4, 2025 · 1 comment · Fixed by #19025
Closed
Labels
bug mypy got something wrong topic-self-types Types for self

Comments

@injust
Copy link

injust commented May 4, 2025

Bug Report

When calling an inherited decorated classmethod that returns Self, mypy does not resolve the type and thinks the type of the return value is Self.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=664aeee2f4339ce869129dfba8017289

from __future__ import annotations

from collections.abc import Callable
from functools import wraps
from typing import Any, Self, TypeVar

type JSON = dict[str, Any]
T = TypeVar("T")


def debug(make: Callable[[type[T], JSON], T]) -> Callable[[type[T], JSON], T]:
    @wraps(make)
    def wrapper(cls: type[T], data: JSON) -> T:
        return make(cls, data)

    return wrapper


class Foo:
    data: JSON
    
    def __init__(self, data: JSON) -> None:
        self.data = data

    @classmethod
    @debug
    def make(cls, data: JSON) -> Self:
        return cls(data)


class Bar(Foo):
    pass


def test() -> Bar:
    return Bar.make({})

Expected Behavior

The code should type check.

Actual Behavior

main.py:36: error: Incompatible return value type (got "Self", expected "Bar")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: --strict
  • Python version used: 3.13
@injust injust added the bug mypy got something wrong label May 4, 2025
@sterliakov sterliakov added the topic-self-types Types for self label May 4, 2025
@sterliakov
Copy link
Collaborator

This is almost a duplicate of #18993 - if you reveal_type(Foo.make({})), that will be Self. We leak self types from decorated classmethods somewhere.

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

Successfully merging a pull request may close this issue.

2 participants