Preserve class tparams when binding a classmethod to a callable#3599
Closed
fangyi-zhou wants to merge 2 commits into
Closed
Preserve class tparams when binding a classmethod to a callable#3599fangyi-zhou wants to merge 2 commits into
fangyi-zhou wants to merge 2 commits into
Conversation
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`.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Contributor
|
@fangyi-zhou has imported this pull request. If you are a Meta employee, you can view this in D106576942. |
stroxler
approved these changes
May 28, 2026
Contributor
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Contributor
|
@fangyi-zhou merged this pull request in b15abe7. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
While investigating #2450 (#2450 (comment)), I noticed that generics handling via classmethod + paramspec is lossy -- the inferred type carries an
Unknowntype 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