Skip to content

Preserve class tparams when binding a classmethod to a callable#3599

Closed
fangyi-zhou wants to merge 2 commits into
facebook:mainfrom
fangyi-zhou:fix-generic-class-callable
Closed

Preserve class tparams when binding a classmethod to a callable#3599
fangyi-zhou wants to merge 2 commits into
facebook:mainfrom
fangyi-zhou:fix-generic-class-callable

Conversation

@fangyi-zhou
Copy link
Copy Markdown
Contributor

@fangyi-zhou fangyi-zhou commented May 27, 2026

Summary

While investigating #2450 (#2450 (comment)), I noticed that generics handling via classmethod + paramspec is lossy -- the inferred type carries an Unknown type instead of a generic type. This diff attempts to fix the gap.

sandbox

P.S. This was insufficient for the original constructor issue, as the code path was not shared after all. Nonetheless worth fixing

Test Plan

Regression test + test.py

Converting a generic classmethod reference like `Cls.method` to a callable
went through `bind_boundmethod`, which always asked
`bind_bound_method_type` to instantiate the method's tparams. When those
tparams referenced the enclosing class's tparams, the instantiation
substituted them away to `Unknown`, so the resulting callable lost the
class's generic parameters.

Detect that case in `bind_boundmethod`: when the bound object is a bare
`ClassDef` and the method's tparams mention the class's tparams, skip
instantiation so those tparams survive into the callable's signature.
The bug-marked test now passes without expecting an `Unknown`.
@meta-cla meta-cla Bot added the cla signed label May 27, 2026
@fangyi-zhou fangyi-zhou changed the title Fix generic class callable Preserve class tparams when binding a classmethod to a callable May 27, 2026
@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. ✅

@fangyi-zhou fangyi-zhou marked this pull request as ready for review May 27, 2026 22:59
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 27, 2026

@fangyi-zhou has imported this pull request. If you are a Meta employee, you can view this in D106576942.

Copy link
Copy Markdown
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in b15abe7 May 28, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 28, 2026

@fangyi-zhou merged this pull request in b15abe7.

@fangyi-zhou fangyi-zhou deleted the fix-generic-class-callable branch May 29, 2026 23:48
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.

2 participants