Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

brianschubert
Copy link
Collaborator

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 existing InlineTypedDict flag would be welcome.

@brianschubert
Copy link
Collaborator Author

cc @Viicos

This comment has been minimized.

Copy link
Contributor

@Viicos Viicos left a 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
Copy link
Contributor

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

Copy link
Collaborator Author

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]
Copy link
Contributor

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?

Copy link
Collaborator Author

@brianschubert brianschubert Apr 7, 2025

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}]}]:
Copy link
Contributor

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}}]:

Copy link
Collaborator Author

@brianschubert brianschubert Apr 7, 2025

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

Copy link
Contributor

github-actions bot commented Apr 7, 2025

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants