-
-
Notifications
You must be signed in to change notification settings - Fork 112
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 __extra__ for PEP 728. #329
Conversation
I assume that I need to update |
fea586b
to
b05b4a5
Compare
Added documentation on this feature. I used 4.10.0 as the next version number, as I think |
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. |
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. |
@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. |
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. |
I was working on updating the PEP. Will probably have both this implementation and the PEP update ready today. Thanks for notifying me! |
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 |
An alternative would be defining However, for introspection, |
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: class TD1(TypedDict):
a: int
assert TD1.__closed__ is False
assert TD1.__extra_keys__ is None
assert TD1.__annotations__ == {'a': int}
class TD2(TypedDict, closed=True):
a: int
assert TD2.__closed__ is True
assert TD2.__extra_keys__ is Never
assert TD2.__annotations__ == {'a': int}
class TD3(TypedDict, closed=True):
a: int
__extra_keys__: str
assert TD3.__closed__ is True
assert TD3.__extra_keys__ is str
assert TD3.__annotations__ == {'a': int}
class TD4(TD2):
b: int
assert TD4.__closed__ is True
assert TD4.__extra_keys__ is Never
assert TD4.__annotations__ == {'a': int, 'b': int}
class TD5(TD3):
b: int
assert TD5.__closed__ is True
assert TD5.__extra_keys__ is str
assert TD5.__annotations__ == {'a': int, 'b': int}
class TD6(TD2, closed=False): # resets inheritance of __extra_keys__
b: int
assert TD6.__closed__ is False
assert TD6.__extra_keys__ is None
assert TD6.__annotations__ == {'a': int, 'b': int}
class TD7(TD3):
__extra_keys__: str # just a regular key
assert TD7.__closed__ is True
assert TD7.__extra_keys__ is int
assert TD7.__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 class TD6(TD2, closed=False):
b: int
assert TD6.__closed__ is False
assert TD6.__extra_keys__ is Never # instead of None
assert TD6.__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 |
Also reorganize the test cases and add coverage. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Also a few lint errors in CI |
Hm, fixing the errors now. |
This also fixes the lint errors. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
No description provided.