-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Improve invalid-argument-type diagnostics where a union type was provided
#21044
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
2 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
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
135 changes: 135 additions & 0 deletions
135
...d_-_Call_expression_-_Wrong_argument_type_-_Diagnostics_for_unio…_(5396a8f9e7f88f71).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,135 @@ | ||
| --- | ||
| source: crates/ty_test/src/lib.rs | ||
| expression: snapshot | ||
| --- | ||
| --- | ||
| mdtest name: function.md - Call expression - Wrong argument type - Diagnostics for union types where the union is not assignable | ||
| mdtest path: crates/ty_python_semantic/resources/mdtest/call/function.md | ||
| --- | ||
|
|
||
| # Python source files | ||
|
|
||
| ## mdtest_snippet.py | ||
|
|
||
| ``` | ||
| 1 | from typing import Sized | ||
| 2 | | ||
| 3 | class Foo: ... | ||
| 4 | class Bar: ... | ||
| 5 | class Baz: ... | ||
| 6 | | ||
| 7 | def f(x: Sized): ... | ||
| 8 | def g( | ||
| 9 | a: str | Foo, | ||
| 10 | b: list[str] | str | dict[str, str] | tuple[str, ...] | bytes | frozenset[str] | set[str] | Foo, | ||
| 11 | c: list[str] | str | dict[str, str] | tuple[str, ...] | bytes | frozenset[str] | set[str] | Foo | Bar, | ||
| 12 | d: list[str] | str | dict[str, str] | tuple[str, ...] | bytes | frozenset[str] | set[str] | Foo | Bar | Baz, | ||
| 13 | ): | ||
| 14 | f(a) # error: [invalid-argument-type] | ||
| 15 | f(b) # error: [invalid-argument-type] | ||
| 16 | f(c) # error: [invalid-argument-type] | ||
| 17 | f(d) # error: [invalid-argument-type] | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-argument-type]: Argument to function `f` is incorrect | ||
| --> src/mdtest_snippet.py:14:7 | ||
| | | ||
| 12 | d: list[str] | str | dict[str, str] | tuple[str, ...] | bytes | frozenset[str] | set[str] | Foo | Bar | Baz, | ||
| 13 | ): | ||
| 14 | f(a) # error: [invalid-argument-type] | ||
| | ^ Expected `Sized`, found `str | Foo` | ||
| 15 | f(b) # error: [invalid-argument-type] | ||
| 16 | f(c) # error: [invalid-argument-type] | ||
| | | ||
| info: Element `Foo` of this union is not assignable to `Sized` | ||
| info: Function defined here | ||
| --> src/mdtest_snippet.py:7:5 | ||
| | | ||
| 5 | class Baz: ... | ||
| 6 | | ||
| 7 | def f(x: Sized): ... | ||
| | ^ -------- Parameter declared here | ||
| 8 | def g( | ||
| 9 | a: str | Foo, | ||
| | | ||
| info: rule `invalid-argument-type` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-argument-type]: Argument to function `f` is incorrect | ||
| --> src/mdtest_snippet.py:15:7 | ||
| | | ||
| 13 | ): | ||
| 14 | f(a) # error: [invalid-argument-type] | ||
| 15 | f(b) # error: [invalid-argument-type] | ||
| | ^ Expected `Sized`, found `list[str] | str | dict[str, str] | ... omitted 5 union elements` | ||
| 16 | f(c) # error: [invalid-argument-type] | ||
| 17 | f(d) # error: [invalid-argument-type] | ||
| | | ||
| info: Element `Foo` of this union is not assignable to `Sized` | ||
| info: Function defined here | ||
| --> src/mdtest_snippet.py:7:5 | ||
| | | ||
| 5 | class Baz: ... | ||
| 6 | | ||
| 7 | def f(x: Sized): ... | ||
| | ^ -------- Parameter declared here | ||
| 8 | def g( | ||
| 9 | a: str | Foo, | ||
| | | ||
| info: rule `invalid-argument-type` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-argument-type]: Argument to function `f` is incorrect | ||
| --> src/mdtest_snippet.py:16:7 | ||
| | | ||
| 14 | f(a) # error: [invalid-argument-type] | ||
| 15 | f(b) # error: [invalid-argument-type] | ||
| 16 | f(c) # error: [invalid-argument-type] | ||
| | ^ Expected `Sized`, found `list[str] | str | dict[str, str] | ... omitted 6 union elements` | ||
| 17 | f(d) # error: [invalid-argument-type] | ||
| | | ||
| info: Union elements `Foo` and `Bar` are not assignable to `Sized` | ||
| info: Function defined here | ||
| --> src/mdtest_snippet.py:7:5 | ||
| | | ||
| 5 | class Baz: ... | ||
| 6 | | ||
| 7 | def f(x: Sized): ... | ||
| | ^ -------- Parameter declared here | ||
| 8 | def g( | ||
| 9 | a: str | Foo, | ||
| | | ||
| info: rule `invalid-argument-type` is enabled by default | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-argument-type]: Argument to function `f` is incorrect | ||
| --> src/mdtest_snippet.py:17:7 | ||
| | | ||
| 15 | f(b) # error: [invalid-argument-type] | ||
| 16 | f(c) # error: [invalid-argument-type] | ||
| 17 | f(d) # error: [invalid-argument-type] | ||
| | ^ Expected `Sized`, found `list[str] | str | dict[str, str] | ... omitted 7 union elements` | ||
| | | ||
| info: Union element `Foo`, and 2 more union elements, are not assignable to `Sized` | ||
| info: Function defined here | ||
| --> src/mdtest_snippet.py:7:5 | ||
| | | ||
| 5 | class Baz: ... | ||
| 6 | | ||
| 7 | def f(x: Sized): ... | ||
| | ^ -------- Parameter declared here | ||
| 8 | def g( | ||
| 9 | a: str | Foo, | ||
| | | ||
| info: rule `invalid-argument-type` 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
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 assumed we would fix this somewhere else, e.g. when actually checking assignability in the first place. This code here only affects invalid-argument-type, but we should do the same thing in other situations, like for actual assignments to variables. Consider:
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.
A more general solution would probably be #19580 (so that we always propagate up the reason why an assignability/subtyping relation did not hold). I do think we need something like that eventually, for better diagnostics in cases where Protocol subtyping doesn't hold (our diagnostics are currently terrible for protocols with lots of members — you can't tell which specific member of the protocol wasn't satisfied). But that would be a big change, and I'm not yet sure how it would fit in with Doug's work on the new constraint solver.
We already do ad-hoc special-casing of union types like this for several other diagnostics. I agree it's not great but I think it's the best we can do for now, and it's better than nothing in the short term.