Total and non-total version of TypedDict #5141
Replies: 2 comments 4 replies
-
Yes, you're correct. You need to create two separate ones for this use case. There have been some discussions on the typing forums about providing a way to make this less cumbersome, but those discussions haven't produced any concrete proposals. |
Beta Was this translation helpful? Give feedback.
-
I have a follow up question on this regarding dict.update() which from looking around seems to be still an open topic on how it should be handled in all cases but here is my scenario anyway. I have now created two TypedDicts with the same set of keys and want to update the total one by the non-total one and im getting an error (i did not expect).
I understand that this can not necessarily type check because of subclassing. But even then the error i am getting was confusing me a bit.
Even more curious is that mypy seem to type check this without errors. The pyright error still remains even if i add Curiously mypy even passes this if i explicitly add more keys to But then again this seems to pass even pyright:
|
Beta Was this translation helpful? Give feedback.
-
Is there any shorter way to creating a total and non-total version of a TypedDict besides just creating two separate ones with the same keys and types?
My first try was to just inherit and have the child as non-total but according to the pep the totality is only supposed to be for the keys defined within that specific dict. Even though the stated use case it not needed anymore with the existence of
NotRequired
.So am i right that i have to create two separate ones for this use case?
Beta Was this translation helpful? Give feedback.
All reactions