-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
✨ Added new Error to TypedDict #14225
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 4 commits
c7824be
f19a7f7
2a4220c
fcf4909
deb6543
222869d
70c0a18
eda0c8b
25f3dbc
c7c687f
a275c86
f7a7f4f
06a3866
780e10d
fb98524
708d322
e4b82b6
1c9a623
f4f0cd0
957ba17
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 |
|---|---|---|
|
|
@@ -455,7 +455,7 @@ class E(TypedDict): | |
| y: int | ||
|
|
||
| a: D = {'x': ''} # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int") [typeddict-item] | ||
| b: D = {'y': ''} # E: Extra key "y" for TypedDict "D" [typeddict-item] | ||
| b: D = {'y': ''} # E: Missing key "x" for TypedDict "D" [typeddict-item] # E: Extra key "y" for TypedDict "D" [typeddict-unknown-key] | ||
|
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. Could you please add a test verifying that values for known keys are still type-checked if unknown keys are present?
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. Good idea, will add it over the weekend
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. Done! I left them on the 'check-errorcodes' file but can come them to the typeddict specific file.
JoaquimEsteves marked this conversation as resolved.
Outdated
|
||
| c = D(x=0) if int() else E(x=0, y=0) | ||
| c = {} # E: Expected TypedDict key "x" but found no keys [typeddict-item] | ||
|
|
||
|
|
||
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.
As mentioned on the issue, for consistency this error code should be also used for errors in case like:
Uh oh!
There was an error while loading. Please reload this page.
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.
Indeed, that makes sense, but I'm afraid I don't like it. I'm worried about what would happen when the user would ignore this error. For example
Running
mypy --disable-error-code=typeddict-unknown-keyon the snippet:Would result in an obvious error being completely ignored by mypy.
So to my view we have to options:
1 - rename the unknown-key to something that better reflects the code (
unknown-anon-keyfor example)2 - Create another error code for this usecase.
Open to suggestions.
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.
OK, reading an unknown key may be too much, but setting it (i.e.
d["unknown"] = 42) should definitely use the same error code. I don't see a difference between:and
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 that's very reasonable. Will ping you when I have that sorted.
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.
Done!
Not sure if the solution is pretty. Added a test for it on the typeddict file.