-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Improve diagnostics for assert_type and assert_never
#18050
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| ## Basic | ||
|
|
||
| <!-- snapshot-diagnostics --> | ||
|
|
||
| ```py | ||
| from typing_extensions import assert_type | ||
|
|
||
|
|
||
189 changes: 189 additions & 0 deletions
189
...ic/resources/mdtest/snapshots/assert_never.md_-_`assert_never`_-_Basic_functionality.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,189 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: assert_never.md - `assert_never` - Basic functionality | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/directives/assert_never.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | from typing_extensions import assert_never, Never, Any | ||
| 2 | from ty_extensions import Unknown | ||
| 3 | | ||
| 4 | def _(never: Never, any_: Any, unknown: Unknown, flag: bool): | ||
| 5 | assert_never(never) # fine | ||
| 6 | | ||
| 7 | assert_never(0) # error: [type-assertion-failure] | ||
| 8 | assert_never("") # error: [type-assertion-failure] | ||
| 9 | assert_never(None) # error: [type-assertion-failure] | ||
| 10 | assert_never([]) # error: [type-assertion-failure] | ||
| 11 | assert_never({}) # error: [type-assertion-failure] | ||
| 12 | assert_never(()) # error: [type-assertion-failure] | ||
| 13 | assert_never(1 if flag else never) # error: [type-assertion-failure] | ||
| 14 | | ||
| 15 | assert_never(any_) # error: [type-assertion-failure] | ||
| 16 | assert_never(unknown) # error: [type-assertion-failure] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:7:5 | ||
| | | ||
| 5 | assert_never(never) # fine | ||
| 6 | | ||
| 7 | assert_never(0) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^-^ | ||
| | | | ||
| | Inferred type of argument is `Literal[0]` | ||
| 8 | assert_never("") # error: [type-assertion-failure] | ||
| 9 | assert_never(None) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `Literal[0]` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:8:5 | ||
| | | ||
| 7 | assert_never(0) # error: [type-assertion-failure] | ||
| 8 | assert_never("") # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^--^ | ||
| | | | ||
| | Inferred type of argument is `Literal[""]` | ||
| 9 | assert_never(None) # error: [type-assertion-failure] | ||
| 10 | assert_never([]) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `Literal[""]` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:9:5 | ||
| | | ||
| 7 | assert_never(0) # error: [type-assertion-failure] | ||
| 8 | assert_never("") # error: [type-assertion-failure] | ||
| 9 | assert_never(None) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^----^ | ||
| | | | ||
| | Inferred type of argument is `None` | ||
| 10 | assert_never([]) # error: [type-assertion-failure] | ||
| 11 | assert_never({}) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `None` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:10:5 | ||
| | | ||
| 8 | assert_never("") # error: [type-assertion-failure] | ||
| 9 | assert_never(None) # error: [type-assertion-failure] | ||
| 10 | assert_never([]) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^--^ | ||
| | | | ||
| | Inferred type of argument is `list[Unknown]` | ||
| 11 | assert_never({}) # error: [type-assertion-failure] | ||
| 12 | assert_never(()) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `list[Unknown]` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:11:5 | ||
| | | ||
| 9 | assert_never(None) # error: [type-assertion-failure] | ||
| 10 | assert_never([]) # error: [type-assertion-failure] | ||
| 11 | assert_never({}) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^--^ | ||
| | | | ||
| | Inferred type of argument is `dict[Unknown, Unknown]` | ||
| 12 | assert_never(()) # error: [type-assertion-failure] | ||
| 13 | assert_never(1 if flag else never) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `dict[Unknown, Unknown]` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:12:5 | ||
| | | ||
| 10 | assert_never([]) # error: [type-assertion-failure] | ||
| 11 | assert_never({}) # error: [type-assertion-failure] | ||
| 12 | assert_never(()) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^--^ | ||
| | | | ||
| | Inferred type of argument is `tuple[()]` | ||
| 13 | assert_never(1 if flag else never) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `tuple[()]` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:13:5 | ||
| | | ||
| 11 | assert_never({}) # error: [type-assertion-failure] | ||
| 12 | assert_never(()) # error: [type-assertion-failure] | ||
| 13 | assert_never(1 if flag else never) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^--------------------^ | ||
| | | | ||
| | Inferred type of argument is `Literal[1]` | ||
| 14 | | ||
| 15 | assert_never(any_) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `Literal[1]` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:15:5 | ||
| | | ||
| 13 | assert_never(1 if flag else never) # error: [type-assertion-failure] | ||
| 14 | | ||
| 15 | assert_never(any_) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^----^ | ||
| | | | ||
| | Inferred type of argument is `Any` | ||
| 16 | assert_never(unknown) # error: [type-assertion-failure] | ||
| | | ||
| info: `Never` and `Any` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Never` | ||
| --> src/mdtest_snippet.py:16:5 | ||
| | | ||
| 15 | assert_never(any_) # error: [type-assertion-failure] | ||
| 16 | assert_never(unknown) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^^-------^ | ||
| | | | ||
| | Inferred type of argument is `Unknown` | ||
| | | ||
| info: `Never` and `Unknown` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
38 changes: 38 additions & 0 deletions
38
...ty_python_semantic/resources/mdtest/snapshots/assert_type.md_-_`assert_type`_-_Basic.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,38 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: assert_type.md - `assert_type` - Basic | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/directives/assert_type.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | from typing_extensions import assert_type | ||
| 2 | | ||
| 3 | def _(x: int): | ||
| 4 | assert_type(x, int) # fine | ||
| 5 | assert_type(x, str) # error: [type-assertion-failure] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `str` | ||
| --> src/mdtest_snippet.py:5:5 | ||
| | | ||
| 3 | def _(x: int): | ||
| 4 | assert_type(x, int) # fine | ||
| 5 | assert_type(x, str) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^-^^^^^^ | ||
| | | | ||
| | Inferred type of argument is `int` | ||
| | | ||
| info: `str` and `int` are not equivalent types | ||
| info: rule `type-assertion-failure` 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
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 think this output is not ideal. In particular, it repeats the entire code snippet here. I'd suggest just this:
I think this has better information density, has less duplication and contains all relevant context.
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.
Yes, I agree that there's some unfortunate duplication here. I'd love to only have a single annotation that only highlights the arguments passed in rather than the whole call.
Did you see the concern I mentioned in my PR summary about how diagnostic ranges interact with suppression comments? That's why I held off from doing that in this PR. If the range of the diagnostic is only the range of the arguments passed into the call, rather than the whole call, it's going to mean that comments like this will not suppress the diagnostic:
I think it's especially a concern for
assert_type, which requires two arguments rather than one (so the call is more likely to be multiline).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.
Oh I see, I did miss that. In particular:
I would tentatively agree with that... But there's definitely downsides with that because it makes creating a diagnostic somewhat confusing.
What I'd suggest instead is to have two annotations on the top-level diagnostic. The primary annotation can be the range you want for suppression comments to work well. Then a secondary annotation for the specific argument that is invalid. IDK exactly how it would render, but something like this:
In this case, I left the message on the primary annotation blank. I'm not sure if that's the right call or not, but it seems reasonable.
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've pushed this change. The rendering looks a bit odd to me, in particular the way
_underlines are surrounded by^underlines:but it's probably worth it to keep the diagnostics concise, as you say.