-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add experimental support for PEP-764 inline TypedDicts #18889
base: master
Are you sure you want to change the base?
Conversation
cc @Viicos |
This comment has been minimized.
This comment has been minimized.
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.
Thanks for implementing this! I left some comments
mypy/typeanal.py
Outdated
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'm assuming we'll have to update the logic whenever PEP 728 gets implemented, to make inlined TDs closed
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.
Ya, that's a good thing to note. I don't know if anyone has started a PEP 728 implementation yet. I think #18176 is the right tracking issue for that
[builtins fixtures/dict.pyi] | ||
[typing fixtures/typing-typeddict.pyi] | ||
|
||
[case testTypedDictInlinePEP764TypeVarValid] |
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.
Maybe we can add a test with PEP 695 type aliases?
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.
there's one in check-python312.test
- does that cover things well enough?
[case testTypedDictInlinePEP764NestedSchema] | ||
# flags: --enable-incomplete-feature=NewInlineTypedDict | ||
from typing import TypedDict | ||
def nested() -> TypedDict[{"one": str, "other": TypedDict[{"a": int, "b": int}]}]: |
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 assume it will error if an invalid nested form is used, e.g.:
def nested() -> TypedDict[{"one": str, "other": {"a": int, "b": int}}]:
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.
There will be an error, though mypy will still interpret it as a valid typeddict as an artifact of how the existing inline syntax is implemented. Added a test showing the mixed behavior
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/http.py:907: error: Inline TypedDict is experimental, must be enabled with --enable-incomplete-feature=InlineTypedDict [misc]
+ steam/http.py:907: error: Legacy inline TypedDict is experimental, must be enabled with --enable-incomplete-feature=InlineTypedDict [misc]
+ steam/http.py:907: note: Did you mean TypedDict[...]?
pydantic (https://github.com/pydantic/pydantic)
+ pydantic/v1/validators.py:630: error: Unused "type: ignore" comment [unused-ignore]
+ pydantic/v1/annotated_types.py:49: error: Unused "type: ignore" comment [unused-ignore]
|
Refs #18748
I opted to put this behavior behind a new feature flag (instead of tying it to the existing
InlineTypedDict
flag), since that helps avoid issues with mixing the existing syntax and PEP-764 syntax. Suggestions about how to name the flag / how to best reconcile this with the existingInlineTypedDict
flag would be welcome.