-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Accept compatible dict in place of TypedDict #3035
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 1 commit
5bd90ea
3f21077
eb6914a
f980adc
f19eb1e
c0c9bfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,11 @@ Add the test in this format anywhere in the file: | |
- `# E: abc...` indicates that this line should result in type check error | ||
with text "abc..." | ||
- note a space after `E:` and `flags:` | ||
- lines without `# E: ` should cause no type check errors | ||
- `# E:12` adds column number to the expected error | ||
- `# E:-1` means that the error was expected 2 lines ago (helpful for multiple errors in a line) | ||
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. Can't you already do 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. @JelleZijlstra Yup ;) 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. |
||
- `# E:12:-1` combines the two options above | ||
- `W: ...` and `N: ...` works exactly like `E:`, but report a warning and a note respectively | ||
- lines that don't contain the above should cause no type check errors | ||
- optional `[builtins fixtures/...]` tells the type checker to use | ||
stubs from the indicated file (see Fixtures section below) | ||
- optional `[out]` is an alternative to the "# E:" notation: it indicates that | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
8000
|
@@ -610,3 +610,26 @@ def f() -> None: | |
A = TypedDict('A', {'x': int}) | ||
A # E: Name 'A' is not defined | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
||
-- Use dict literals | ||
|
||
[case testTypedDictDictLiterals] | ||
from mypy_extensions import TypedDict | ||
|
||
Point = TypedDict('Point', {'x': int, 'y': int}) | ||
|
||
def f(p: Point) -> None: | ||
p = {'x': 2, 'y': 3} | ||
p = {'x': 2} # E: Expected items ['x', 'y'] but found ['x']. | ||
p = dict(x=2, y=3) | ||
|
||
f({'x': 1, 'y': 3}) | ||
f({'x': 1, 'y': 'z'}) # E: Argument 1 to "f" has incompatible type "TypedDict(x=int, y=str)"; expected "Point" | ||
# E:-1: Incompatible types (expression has type "str", TypedDict item "y" has type "int") | ||
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 think only one of these error messages should be shown, not both. |
||
|
||
f(dict(x=1, y=3)) | ||
f(dict(x=1, y=3, z=4)) # E: Expected items ['x', 'y'] but found ['x', 'y', 'z']. | ||
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 would add one more test case with few tests where you assign to variables with explicit types, like: p: Point = {'x', 'hi'} and/or p: Point
p = dict(x='bye') 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. also maybe a test for p = Point(x=1, y=2)
p = {x='hi'} |
||
|
||
[builtins fixtures/dict.pyi] | ||
|
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 would prefer this as a separate PR, proposed syntax is debatable and could only delay this PR.