-
Notifications
You must be signed in to change notification settings - Fork 283
Fix required and optional keys inheritance for TypedDict #700
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
Conversation
dc2f5df to
ebfe321
Compare
|
Should this also be updated upstream in CPython? |
gvanrossum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM except I wish to rename _Animal and get rid of tail.
| class Cat(Animal): | ||
| fur_color: str | ||
| tail: int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't allowed by mypy (you can't change the type of an attribute in a subclass) so I recommend deleting the tail attribute altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, deleting it. I also will not remove conflicting keys from __required_keys__ and __optional_keys__ since conflicts are not allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed changes, updated tests.
| log_level: int | ||
| log_path: str | ||
| class _Animal(TypedDict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a reason for this class name to start with _ -- maybe rename to BaseAnimal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not aware of a convention how to name TypedDict that has both required and optional keys. Usually I prefix a TypedDict with required keys with an underscore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to BaseAnimal.
|
Travis CI build failed for Python 3.5.1, but it looks unrelated. File "/home/travis/virtualenv/python3.5.1/lib/python3.5/site-packages/importlib_metadata/__init__.py", line 9, in <module>
import zipp
ImportError: No module named 'zipp'Probably restarting Travis build will help. |
gvanrossum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I will land this despite the 3.5.1 failure.
|
@vemel - I think this should go upstream, as a follow-up to python/cpython#17214, since CPython is still using the pre-this-PR logic. |
|
Yes, please open a good issue and submit a PR.
On Mon, May 4, 2020 at 05:26 Zac Hatfield-Dodds ***@***.***> wrote:
@vemel <https://github.com/vemel> - I think this *should* go upstream, as
a follow-up to python/cpython#17214
<python/cpython#17214>, since CPython is still
using the pre-this-PR logic.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#700 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAWCWMWTHUVAENLMQLFPULLRP2X6BANCNFSM4KRQAMPA>
.
--
--Guido (mobile)
|
|
@Zac-HD Did you submit a PR to CPython for this? Having trouble finding it. |
|
According to https://github.com/python/cpython/pulls?q=is%3Apr+author%3AZac-HD I didn't, and remembering what May was like I believe it. If you'd like to, go for it 🙂 |
(For a complete description of the issue see python/typing#700.)
(For a complete description of the issue see python/typing#700.)
I realized that the inheritance of TypedDict does not work as it should.
I would assume that
Catshould have required keysnameandfur_colorand optionalvoice.But in reality, it will have required
fur_colorand optionalnameandvoice, becauseAnimalhastotal=FalseFixed
__required_keys__and__optional_keys__are copied from base classesTypedDictannotations are added to__annotations__after base annotationsTypedDictredefines them