-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Type inference for comprehensions #20962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
crates/ty_python_semantic/resources/corpus/88_regression_pr_20962.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| name_1 | ||
| {0: 0 for unique_name_0 in unique_name_1 if name_1} | ||
|
|
||
|
|
||
| @[name_2 for unique_name_2 in name_2] | ||
| def name_2(): | ||
| pass | ||
|
|
||
|
|
||
| def name_2(): | ||
| pass | ||
|
|
||
|
|
||
| match 0: | ||
| case name_2(): | ||
| pass | ||
| case []: | ||
| name_1 = 0 |
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
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
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
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
50 changes: 50 additions & 0 deletions
50
...ty_python_semantic/resources/mdtest/regression/pr_20962_comprehension_panics.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Documentation of two fuzzer panics involving comprehensions | ||
|
|
||
| Type inference for comprehensions was added in <https://github.com/astral-sh/ruff/pull/20962>. It | ||
| added two new fuzzer panics that are documented here for regression testing. | ||
|
|
||
| ## Too many cycle iterations in `place_by_id` | ||
|
|
||
| <!-- expect-panic: too many cycle iterations --> | ||
|
|
||
| ```py | ||
| name_5(name_3) | ||
| [0 for unique_name_0 in unique_name_1 for unique_name_2 in name_3] | ||
|
|
||
| @{name_3 for unique_name_3 in unique_name_4} | ||
| class name_4[**name_3](0, name_2=name_5): | ||
| pass | ||
|
|
||
| try: | ||
| name_0 = name_4 | ||
| except* 0: | ||
| pass | ||
| else: | ||
| match unique_name_12: | ||
| case 0: | ||
| from name_2 import name_3 | ||
| case name_0(): | ||
|
|
||
| @name_4 | ||
| def name_3(): | ||
| pass | ||
|
|
||
| (name_3 := 0) | ||
|
|
||
| @name_3 | ||
| async def name_5(): | ||
| pass | ||
| ``` | ||
|
|
||
| ## Too many cycle iterations in `infer_definition_types` | ||
|
|
||
| <!-- expect-panic: too many cycle iterations --> | ||
|
|
||
| ```py | ||
| for name_1 in { | ||
| {{0: name_4 for unique_name_0 in unique_name_1}: 0 for unique_name_2 in unique_name_3 if name_4}: 0 | ||
| for unique_name_4 in name_1 | ||
| for name_4 in name_1 | ||
| }: | ||
| pass | ||
| ``` |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed with @ibraheemdev, solving this will require pushing down the type context into
infer_scope_typesin order to infer theNonein(x, y, None)asstr | Noneinside the comprehension. Similar for theTypedDicttest below. This change is a bit more invasive, as it requires us to skip comprehension scopes incheck_types, because we would not have the required type context available when simply looping over all scopes. This change would be fine because with this PR, we're now callinginfer_scope_typesfor comprehension scopes when we check the outer scope. A similar change would probably be required for lambdas as well.