-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Enable generic TypedDicts #13389
Enable generic TypedDicts #13389
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
It is great to have |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The |
This comment has been minimized.
This comment has been minimized.
Hm, something came to my mind, I only support class syntax for generic TypedDicts, but it seems to me we can also allow the assignment-based syntax (it would be similar to how we define generic type aliases): TD = TypedDict("TD", {"key": int, "value": T}) # T must be unbound here. This should be not hard to add here. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Some potential merge conflicts already start to appear, it would be great if someone can take a look at this soon. |
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.
Looks good, just one suggestion in the tests.
x: str | ||
y: str | ||
|
||
reveal_type(TD.__iter__) # N: Revealed type is "def (typing._TypedDict) -> typing.Iterator[builtins.str]" |
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.
Isn't this just the keys? Maybe test .values()
too.
This comment has been minimized.
This comment has been minimized.
1 similar comment
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Fixes #3863
This builds on top of some infra I added for recursive types (Ref #13297). Implementation is quite straightforward. The only non-trivial thing is that when extending/merging TypedDicts, the item types need to me mapped to supertype during semantic analysis. This means we can't call
is_subtype()
etc., and can in theory get types likeUnion[int, int]
. But OTOH this equally applies to type aliases, and doesn't seem to cause problems.Btw the diff in
semanal_typeddict.py
is not as big as it looks. There was a giantif possible: ...
that I found a bit annoying and replaced it withif not possible: return
(IIRC GitHub has an option to ignore whitespace).An important note on runtime support for this: IIUC this should work in Python 3.11, and should work soon with
typing_extensions
on earlier versions. I didn't add any version checks here. If someone is interested in adding those, please do this.