-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Faster subscript assignment checks for (unions of) TypedDicts
#21378
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,19 +22,39 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_dia | |
| 8 | legs: int | ||
| 9 | | ||
| 10 | def _(being: Person | Animal) -> None: | ||
| 11 | being["surname"] = "unknown" # error: [invalid-assignment] | ||
| 11 | # error: [invalid-key] | ||
| 12 | # error: [invalid-key] | ||
| 13 | being["surname"] = "unknown" | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
|
|
||
| ``` | ||
| error[invalid-assignment]: Method `__setitem__` of type `(key: Literal["name"], value: str, /) -> None` cannot be called with a key of type `Literal["surname"]` and a value of type `Literal["unknown"]` on object of type `Person | Animal` | ||
| --> src/mdtest_snippet.py:11:5 | ||
| error[invalid-key]: Invalid key for TypedDict `Person` | ||
| --> src/mdtest_snippet.py:13:5 | ||
| | | ||
| 10 | def _(being: Person | Animal) -> None: | ||
| 11 | being["surname"] = "unknown" # error: [invalid-assignment] | ||
| | ^^^^^ | ||
| 11 | # error: [invalid-key] | ||
| 12 | # error: [invalid-key] | ||
| 13 | being["surname"] = "unknown" | ||
| | ----- ^^^^^^^^^ Unknown key "surname" - did you mean "name"? | ||
| | | | ||
| | TypedDict `Person` in union type `Person | Animal` | ||
| | | ||
| info: rule `invalid-key` is enabled by default | ||
|
Comment on lines
+33
to
+43
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. What about including the name of the unknown key in the summary line here and taking it out of the primary-annotation message? We could also consider linking back to the definition of the
Contributor
Author
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.
I can try that, but it's always a bit tricky to get both the full diagnostic and the concise diagnostic to look good. I still wish we had an API to set a separate concise message. |
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| error[invalid-key]: Invalid key for TypedDict `Animal` | ||
| --> src/mdtest_snippet.py:13:5 | ||
| | | ||
| 11 | # error: [invalid-key] | ||
| 12 | # error: [invalid-key] | ||
| 13 | being["surname"] = "unknown" | ||
| | ----- ^^^^^^^^^ Unknown key "surname" - did you mean "name"? | ||
| | | | ||
| | TypedDict `Animal` in union type `Person | Animal` | ||
| | | ||
| info: rule `invalid-assignment` is enabled by default | ||
| info: rule `invalid-key` is enabled by default | ||
|
|
||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.