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

Support NotRequired in TypedDict when __future__.annotations is enabled #404

Closed
guacs opened this issue May 13, 2023 · 4 comments · Fixed by #410
Closed

Support NotRequired in TypedDict when __future__.annotations is enabled #404

guacs opened this issue May 13, 2023 · 4 comments · Fixed by #410

Comments

@guacs
Copy link

guacs commented May 13, 2023

Description

PEP 655 adds support for NotRequired and Required. Currently it seems msgspec doesn't support it, but it does support total=False when defining a TypedDict. Is this something that can be supported?

I actually encountered this when I was trying to create a typed dictionary for specifying the values that can be updated in a specific table in Postgres. That is, whichever values are provided would be updated. Initially I had something like this:

class UpdateRequest(TypedDict):

    id: str
    some_value: NotRequired[int]
    another_value: NotRequired[str]
    ...

This is what the body of the request from the client was going to be. I refactored this to make it work with msgpec by doing the following:

class UpdateEntity(TypedDict, total=False):

    some_value: int
    another_value: str
    ...

class UpdateRequest(TypedDict):

     id: str
     update_details: UpdateEntity
@jcrist
Copy link
Owner

jcrist commented May 14, 2023

Thanks for opening this issue. NotRequired/Required do work, but only if you don't use from __future__ import annotations. This is due to an upstream bug in CPython (see python/typing_extensions#55 and python/cpython#97727).

You can work around this issue for now by doing as you've done above, or avoiding the from __future__ import annotations import.

In the long run I think we'll move to parsing the Required/NotRequired types directly ourselves, rather than relying on the current buggy typing.TypedDict internals to do it for us.

@guacs
Copy link
Author

guacs commented May 15, 2023

Ohh I see. I guess I'll go with my workaround for now. Thank you very much, and thanks for a great library :)

@guacs guacs closed this as completed May 15, 2023
@jcrist
Copy link
Owner

jcrist commented May 15, 2023

No problem! I'm going to keep this open for now to remind me to update our logic to manually handle Required/NotRequired rather than relying on TypedDict to do it for us.

@jcrist jcrist reopened this May 15, 2023
@guacs
Copy link
Author

guacs commented May 16, 2023

Yeah sure that's a good idea.

@jcrist jcrist changed the title Support for NotRequired in TypedDict Support NotRequired in TypedDict when __future__.annotations is enabled May 16, 2023
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 a pull request may close this issue.

2 participants