-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[ty] Validate PEP 695 type alias scope and redeclaration rules #24341
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
59 changes: 59 additions & 0 deletions
59
...pep695_type_aliases.…_-_PEP_695_type_aliases_-_Redeclared_aliases_(7e8460432a17eeae).snap
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,59 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: pep695_type_aliases.md - PEP 695 type aliases - Redeclared aliases | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | type Redeclared = int | ||
| 2 | # error: [invalid-type-alias] "Type alias `Redeclared` is already defined in this scope" | ||
| 3 | type Redeclared = str | ||
| 4 | from random import random | ||
| 5 | | ||
| 6 | flag = random() > 0.5 | ||
| 7 | if flag: | ||
| 8 | type BranchRedeclared = int | ||
| 9 | else: | ||
| 10 | # error: [invalid-type-alias] "Type alias `BranchRedeclared` is already defined in this scope" | ||
| 11 | type BranchRedeclared = str | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-type-alias]: Type alias `Redeclared` is already defined in this scope | ||
| --> src/mdtest_snippet.py:1:6 | ||
| | | ||
| 1 | type Redeclared = int | ||
| | ---------- `Redeclared` previously defined here | ||
| 2 | # error: [invalid-type-alias] "Type alias `Redeclared` is already defined in this scope" | ||
| 3 | type Redeclared = str | ||
| | ^^^^^^^^^^ | ||
| 4 | from random import random | ||
| | | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-type-alias]: Type alias `BranchRedeclared` is already defined in this scope | ||
| --> src/mdtest_snippet.py:8:10 | ||
| | | ||
| 6 | flag = random() > 0.5 | ||
| 7 | if flag: | ||
| 8 | type BranchRedeclared = int | ||
| | ---------------- `BranchRedeclared` previously defined here | ||
| 9 | else: | ||
| 10 | # error: [invalid-type-alias] "Type alias `BranchRedeclared` is already defined in this scope" | ||
| 11 | type BranchRedeclared = str | ||
| | ^^^^^^^^^^^^^^^^ | ||
| | | ||
|
|
||
| ``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
I was a bit surprised by this, but other type checkers do flag it.
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.
I don't like flagging this -- does the conformance suite require it?
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.
If we do flag this, we also need to ensure we don't flag it if one of the branches is statically unreachable.
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.
I agree with @carljm, but yes -- unfortunately the conformance suite does require it currently: https://github.com/python/typing/blob/1df1565c69730d88ce6877009d268ba1d602af1e/conformance/tests/aliases_type_statement.py#L52. Not sure if that's actually following the spec or if the conformance suite is going beyond the spec there
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.
I can't find any such requirement in PEP 695 or in the spec. I think we should PR the conformance suite to not require this error.
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.
I also don't see the rule against defining type aliases in function bodies specified anywhere, and that also seems sorta unnecessary... it's not totally clear to me why users should be prohibited from doing something like this, which seems fine: https://play.ty.dev/f918c85d-8bf3-440c-a5bf-1188cd72d053
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.
Just put up python/typing#2249. If that goes through, I think we can close this PR?
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.
Yup!
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.
To confirm, I'm holding off on reviewing the code on the assumption that we're about to close the PR. If that changes I'll take a look!
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.
That's correct. Given the approvals on that PR, I'm just going to close this out!