Uh oh!
There was an error while loading. Please reload this page.
Support __extra__ for PEP 728. - #329
Conversation
PIG208
commented
Feb 8, 2024
I assume that I need to update |
fea586b to
b05b4a5ComparePIG208
commented
Feb 9, 2024
Added documentation on this feature. I used 4.10.0 as the next version number, as I think |
srittau
commented
Feb 9, 2024
Thanks! Currently, there's some discussion about this in https://discuss.python.org/t/pep-728-typeddict-with-typed-extra-items/45443/9. We should hold off merging this for a few days, until the discussion is resolved. |
JelleZijlstra
commented
Feb 9, 2024
Agree. My plan is to wait for a week or so for both this one and PEP 742 to reach a rough consensus in the discussion, then make a new typing-extensions release for both. |
JelleZijlstra
commented
Feb 16, 2024
@PIG208 What do you think of implementing Eric's proposal from https://discuss.python.org/t/pep-728-typeddict-with-typed-extra-items/45443/24 ? That seems the best option so far. |
JelleZijlstra
commented
Feb 16, 2024
In particular, if we can get the implementation ready by Sunday, it can go into the upcoming release 4.10.0. If you don't have time in the next few days, I can work on an implementation tomorrow. |
PIG208
commented
Feb 16, 2024
I was working on updating the PEP. Will probably have both this implementation and the PEP update ready today. Thanks for notifying me! |
PIG208
commented
Feb 17, 2024
I'm thinking about if and how we should include the special "extra_item" for runtime introspection. Perhaps it is not ideal to leave it in Should we instead use an attribute |
PIG208
commented
Feb 17, 2024
An alternative would be defining However, for introspection, |
JelleZijlstra
commented
Feb 17, 2024
I think the general principle should be that we should allow runtime typing tools to reconstruct what the user wrote as much as possible, without deciding for them what the exact semantics are. After all, there may be edge cases (e.g. involving forward references) where the runtime typing tool is able to use more advanced logic than typing-extensions to figure out the right behavior. However, we have to balance that argument with creating an intuitive interface for users. I do think we need two attributes: I'd want the following semantics: classTD1(TypedDict):
a: intassertTD1.__closed__isFalseassertTD1.__extra_keys__isNoneassertTD1.__annotations__== {'a': int}
classTD2(TypedDict, closed=True):
a: intassertTD2.__closed__isTrueassertTD2.__extra_keys__isNeverassertTD2.__annotations__== {'a': int}
classTD3(TypedDict, closed=True):
a: int__extra_keys__: strassertTD3.__closed__isTrueassertTD3.__extra_keys__isstrassertTD3.__annotations__== {'a': int}
classTD4(TD2):
b: intassertTD4.__closed__isTrueassertTD4.__extra_keys__isNeverassertTD4.__annotations__== {'a': int, 'b': int}
classTD5(TD3):
b: intassertTD5.__closed__isTrueassertTD5.__extra_keys__isstrassertTD5.__annotations__== {'a': int, 'b': int}
classTD6(TD2, closed=False): # resets inheritance of __extra_keys__b: intassertTD6.__closed__isFalseassertTD6.__extra_keys__isNoneassertTD6.__annotations__== {'a': int, 'b': int}
classTD7(TD3):
__extra_keys__: str# just a regular keyassertTD7.__closed__isTrueassertTD7.__extra_keys__isintassertTD7.__annotations__== {'a': int, '__extra_keys__': str} |
It makes sense to have both dunder attributes, but I'm surprised by the "resetting inheritance of extra_keys" behavior. I expected that classTD6(TD2, closed=False):
b: intassertTD6.__closed__isFalseassertTD6.__extra_keys__isNever# instead of NoneassertTD6.__annotations__== {'a': int, 'b': int}Was this decision driven by distinguishing TypedDict with Edit: I think I got it now. When |
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This current implementation inherits A drawback is that we have to avoid setting |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Also reorganize the test cases and add coverage. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
JelleZijlstra
commented
Feb 17, 2024
Also a few lint errors in CI |
PIG208
commented
Feb 18, 2024
Hm, fixing the errors now. |
This also fixes the lint errors. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
No description provided.