Fix required and optional keys inheritance for TypedDict#700
Fix required and optional keys inheritance for TypedDict#700gvanrossum merged 7 commits intopython:masterfrom
Conversation
dc2f5df to
ebfe321
Compare
|
Should this also be updated upstream in CPython? |
gvanrossum
left a comment
There was a problem hiding this comment.
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.
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.
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.
Pushed changes, updated tests.
| log_level: int | ||
| log_path: str | ||
|
|
||
| class _Animal(TypedDict): |
There was a problem hiding this comment.
I don't see a reason for this class name to start with _ -- maybe rename to BaseAnimal?
There was a problem hiding this comment.
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.
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.
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