-
Notifications
You must be signed in to change notification settings - Fork 2k
[ty] Improve consistency and quality of diagnostics relating to invalid type forms #24325
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
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
93 changes: 93 additions & 0 deletions
93
...s_for_invalid_ty…_-_Diagnostics_for_comm…_-_AST_nodes_that_are_o…_(58a3839a9bc7026d).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,93 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - AST nodes that are only valid inside `Literal` | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | def bad( | ||
| 2 | # error: [invalid-type-form] | ||
| 3 | a: 42, | ||
| 4 | # error: [invalid-type-form] | ||
| 5 | b: b"42", | ||
| 6 | # error: [invalid-type-form] | ||
| 7 | c: True, | ||
| 8 | # error: [invalid-syntax-in-forward-annotation] | ||
| 9 | d: "invalid syntax", | ||
| 10 | ): ... | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-type-form]: Int literals are not allowed in this context in a type expression | ||
| --> src/mdtest_snippet.py:3:8 | ||
| | | ||
| 1 | def bad( | ||
| 2 | # error: [invalid-type-form] | ||
| 3 | a: 42, | ||
| | ^^ Did you mean `typing.Literal[42]`? | ||
| 4 | # error: [invalid-type-form] | ||
| 5 | b: b"42", | ||
| | | ||
| info: See the following page for a reference on valid type expressions: | ||
| info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions | ||
| info: rule `invalid-type-form` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-type-form]: Bytes literals are not allowed in this context in a type expression | ||
| --> src/mdtest_snippet.py:5:8 | ||
| | | ||
| 3 | a: 42, | ||
| 4 | # error: [invalid-type-form] | ||
| 5 | b: b"42", | ||
| | ^^^^^ Did you mean `typing.Literal[b"42"]`? | ||
| 6 | # error: [invalid-type-form] | ||
| 7 | c: True, | ||
| | | ||
| info: See the following page for a reference on valid type expressions: | ||
| info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions | ||
| info: rule `invalid-type-form` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-type-form]: Boolean literals are not allowed in this context in a type expression | ||
| --> src/mdtest_snippet.py:7:8 | ||
| | | ||
| 5 | b: b"42", | ||
| 6 | # error: [invalid-type-form] | ||
| 7 | c: True, | ||
| | ^^^^ Did you mean `typing.Literal[True]`? | ||
| 8 | # error: [invalid-syntax-in-forward-annotation] | ||
| 9 | d: "invalid syntax", | ||
| | | ||
| info: See the following page for a reference on valid type expressions: | ||
| info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions | ||
| info: rule `invalid-type-form` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-syntax-in-forward-annotation]: Syntax error in forward annotation: Unexpected token at the end of an expression | ||
| --> src/mdtest_snippet.py:9:8 | ||
| | | ||
| 7 | c: True, | ||
| 8 | # error: [invalid-syntax-in-forward-annotation] | ||
| 9 | d: "invalid syntax", | ||
| | ^^^^^^^^^^^^^^^^ Did you mean `typing.Literal["invalid syntax"]`? | ||
| 10 | ): ... | ||
| | | ||
| info: rule `invalid-syntax-in-forward-annotation` is enabled by default | ||
|
|
||
| ``` |
54 changes: 54 additions & 0 deletions
54
...s_for_invalid_ty…_-_Diagnostics_for_comm…_-_Dict-literal_or_set-…_(15737b0beb194b0e).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,54 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - Dict-literal or set-literal when you meant to use `dict[]`/`set[]` | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | def _( | ||
| 2 | x: {int: str}, # error: [invalid-type-form] | ||
| 3 | y: {str}, # error: [invalid-type-form] | ||
| 4 | ): ... | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-type-form]: Dict literals are not allowed in type expressions | ||
| --> src/mdtest_snippet.py:2:8 | ||
| | | ||
| 1 | def _( | ||
| 2 | x: {int: str}, # error: [invalid-type-form] | ||
| | ^^^^^^^^^^ Did you mean `dict[int, str]`? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rad |
||
| 3 | y: {str}, # error: [invalid-type-form] | ||
| 4 | ): ... | ||
| | | ||
| info: See the following page for a reference on valid type expressions: | ||
| info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions | ||
| info: rule `invalid-type-form` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-type-form]: Set literals are not allowed in type expressions | ||
| --> src/mdtest_snippet.py:3:8 | ||
| | | ||
| 1 | def _( | ||
| 2 | x: {int: str}, # error: [invalid-type-form] | ||
| 3 | y: {str}, # error: [invalid-type-form] | ||
| | ^^^^^ Did you mean `set[str]`? | ||
| 4 | ): ... | ||
| | | ||
| info: See the following page for a reference on valid type expressions: | ||
| info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions | ||
| info: rule `invalid-type-form` is enabled by default | ||
|
|
||
| ``` | ||
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
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.
Does the message differ here because bytes literals are sometimes allowed in type expressions (e.g., within
Literal)?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, exactly. It's not true to say they're always invalid in type expressions -- they're just only valid in certain specific contexts