Fix spurious has-type error when unpacking a TypeVarTuple with a constrained TypeVar#21745
Open
omkar-334 wants to merge 1 commit into
Open
Fix spurious has-type error when unpacking a TypeVarTuple with a constrained TypeVar#21745omkar-334 wants to merge 1 commit into
omkar-334 wants to merge 1 commit into
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Fixes #21693.
Unpacking a
TypeVarTuplevariable likey = (*result,)gave a bogusCannot determine type of "result"error, but only when the enclosing function also had a value-constrained TypeVar. The result silently becametuple[Any, ...].The constrained TypeVar is what makes mypy re-check the body once per value, deep-copying the AST through
TransformVisitor. Every visitor there recurses into its children and remapsVarreferences so they stay consistent within a copy, exceptvisit_star_expr, which returnedStarExpr(node.expr)without touching the inner expression. So in the copy the assignment targetresultgot a freshVarwhile*resultstill pointed at the original, which is never assigned, so its type never resolves.The fix is a one-liner: recurse into the inner expression like everything else. I also carry over the
validflag, which the old code was dropping.Added a regression test that fails on master and passes here. Full check, semanal, fine-grained, and transform suites pass.