Allow unpacking from TypeVars with iterable bounds#13425
Merged
sobolevn merged 3 commits intopython:masterfrom Aug 19, 2022
Merged
Allow unpacking from TypeVars with iterable bounds#13425sobolevn merged 3 commits intopython:masterfrom
sobolevn merged 3 commits intopython:masterfrom
Conversation
TypeVars aren't iterable, but their bounds might be! Resolve a TypeVar to its bounds before trying to decide how to unpack one of its instances. Fixes python#13402.
This comment has been minimized.
This comment has been minimized.
sobolevn
reviewed
Aug 17, 2022
| from typing import Tuple, TypeVar | ||
| TupleT = TypeVar("TupleT", bound=Tuple[int, ...]) | ||
| def f(t: TupleT) -> None: | ||
| a, *b = t |
Member
There was a problem hiding this comment.
Let's add reveal_type(a) and reveal_type(b) here. This would catch some unexpected types.
sobolevn
reviewed
Aug 17, 2022
| rvalue_type = get_proper_type(rv_type or self.expr_checker.accept(rvalue)) | ||
|
|
||
| if isinstance(rvalue_type, TypeVarType): | ||
| rvalue_type = get_proper_type(rvalue_type.upper_bound) |
Member
There was a problem hiding this comment.
What if there's no upper bound? 🤔 Let's add a test case for this as well!
Contributor
Author
There was a problem hiding this comment.
That's a good thing to check 😅 It looks like it does the right thing!
I left the first test case in check-bound.test and added the new test case to check-typevar-unbound.test but let me know if they should be organized differently.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
Thanks for your help! |
This comment has been minimized.
This comment has been minimized.
1 similar comment
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
hauntsaninja
pushed a commit
to hauntsaninja/mypy
that referenced
this pull request
Sep 10, 2022
* Allow unpacking from TypeVars by resolving bounds TypeVars aren't iterable, but their bounds might be! Resolve a TypeVar to its bounds before trying to decide how to unpack one of its instances. Fixes python#13402.
hauntsaninja
added a commit
that referenced
this pull request
Sep 12, 2022
…13425) (#13644) TypeVars aren't iterable, but their bounds might be! Resolve a TypeVar to its bounds before trying to decide how to unpack one of its instances. Fixes #13402 Co-authored-by: Tim D. Smith <[email protected]>
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.
Description
TypeVars aren't iterable, but their bounds might be! Resolve a TypeVar to its bounds before trying to decide how to unpack one of its instances. This allows unpacking from instances of TypeVars with iterable bounds, which otherwise fails.
Fixes #13402.
Test Plan
Added a test for a minimal reproducer.