-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Add a new assert-type-unspellable-subtype diagnostic
#22815
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
+329
−90
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
83 changes: 83 additions & 0 deletions
83
...test/snapshots/assert_type.md_-_`assert_type`_-_Unspellable_types_(385d082f9803b184).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,83 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
|
|
||
| --- | ||
| mdtest name: assert_type.md - `assert_type` - Unspellable types | ||
| 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 | class Foo: ... | ||
| 4 | class Bar: ... | ||
| 5 | class Baz: ... | ||
| 6 | | ||
| 7 | def f(x: Foo): | ||
| 8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`" | ||
| 9 | if isinstance(x, Bar): | ||
| 10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`" | ||
| 11 | | ||
| 12 | # The actual type must be a subtype of the asserted type, as well as being unspellable, | ||
| 13 | # in order for `assert-type-unspellable-subtype` to be emitted instead of `type-assertion-failure` | ||
| 14 | assert_type(x, Baz) # error: [type-assertion-failure] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Bar` | ||
| --> src/mdtest_snippet.py:8:5 | ||
| | | ||
| 7 | def f(x: Foo): | ||
| 8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`" | ||
| | ^^^^^^^^^^^^-^^^^^^ | ||
| | | | ||
| | Inferred type is `Foo` | ||
| 9 | if isinstance(x, Bar): | ||
| 10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`" | ||
| | | ||
| info: `Bar` and `Foo` are not equivalent types | ||
| info: rule `type-assertion-failure` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[assert-type-unspellable-subtype]: Argument does not have asserted type `Bar` | ||
| --> src/mdtest_snippet.py:10:9 | ||
| | | ||
| 8 | assert_type(x, Bar) # error: [type-assertion-failure] "Type `Bar` does not match asserted type `Foo`" | ||
| 9 | if isinstance(x, Bar): | ||
| 10 | assert_type(x, Bar) # error: [assert-type-unspellable-subtype] "Type `Bar` does not match asserted type `Foo & Bar`" | ||
| | ^^^^^^^^^^^^-^^^^^^ | ||
| | | | ||
| | Inferred type is `Foo & Bar` | ||
| 11 | | ||
| 12 | # The actual type must be a subtype of the asserted type, as well as being unspellable, | ||
| | | ||
| info: `Foo & Bar` is a subtype of `Bar`, but they are not equivalent | ||
| info: rule `assert-type-unspellable-subtype` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[type-assertion-failure]: Argument does not have asserted type `Baz` | ||
| --> src/mdtest_snippet.py:14:9 | ||
| | | ||
| 12 | # The actual type must be a subtype of the asserted type, as well as being unspellable, | ||
| 13 | # in order for `assert-type-unspellable-subtype` to be emitted instead of `type-assertion-failure` | ||
| 14 | assert_type(x, Baz) # error: [type-assertion-failure] | ||
| | ^^^^^^^^^^^^-^^^^^^ | ||
| | | | ||
| | Inferred type is `Foo & Bar` | ||
| | | ||
| info: `Baz` and `Foo & Bar` 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
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.
Should this be a warning by default, given that this is only a suspicious pattern?
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 it should still be an error — the assertion is still failing. The spec says that assert_type should only pass if the two types are equivalent, and we only emit this error if the two types are not equivalent