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

Enable generic TypedDicts #13389

Merged
merged 17 commits into from
Aug 15, 2022
Merged

Conversation

ilevkivskyi
Copy link
Member

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 like Union[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 giant if possible: ... that I found a bit annoying and replaced it with if 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.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

It is great to have mypy_primer, will push a fix soon!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

The --disallow-any-generics is not strictly needed for this PR, but I found one of my new tests failed because this flag was a bit inconsistent for type aliases, so I decided to clean it up.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

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.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Some potential merge conflicts already start to appear, it would be great if someone can take a look at this soon.

Copy link
Member

@JelleZijlstra JelleZijlstra left a 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]"
Copy link
Member

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.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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.

Support for generic TypedDict
2 participants