Skip to content

fix foo = classmethod(foo) does not create a working classmethod #3362 - #4796

Open
asukaminato0721 wants to merge 2 commits into
meta:mainfrom
asukaminato0721:3362
Open

asukaminato0721 wants to merge 2 commits into
meta:mainfrom
asukaminato0721:3362

Conversation

@asukaminato0721

Copy link
Copy Markdown
Collaborator

Summary

Fixes #3362

Recognizes f = classmethod(f) during binding and types cls as type[Self], preserves subclass-aware return inference and class-attribute initialization, avoids triggering when classmethod is shadowed.

Test Plan

add test

@meta-cla meta-cla Bot added the cla signed label Sep 3, 2026
@github-actions github-actions Bot added the size/m label Sep 3, 2026
@meta-codesync

meta-codesync Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D118597715. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a rebase before we can merge it, since it has build failures.

In addition, there are some behavior issues:

  1. Wrong-scope lookup: an inner class’s f = classmethod(f) can mark an unrelated outer-class method. Outer().f() then incorrectly infers type[Outer] instead of Outer.
  2. Aliases change retroactively: saving g = f before wrapping f also converts g’s inferred behavior to a classmethod, although it remains an instance method at runtime.
  3. Decorator composition breaks: the flag changes the undecorated function’s receiver before existing decorators run. A valid adapter from an instance-taking function to a class-taking callable gains a false-positive argument error.
  4. Overloads become inconsistent: only the implementation is marked, producing new invalid-overload errors on otherwise valid overload declarations.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

foo = classmethod(foo) does not create a working classmethod

3 participants