Visitar URL original
Fix required and optional keys inheritance for TypedDict by vemel · Pull Request #700 · python/typing · GitHub
The Wayback Machine - https://web.archive.org/web/20201019095653/https://github.com/python/typing/pull/700
Skip to content
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

Fix required and optional keys inheritance for TypedDict #700

Merged
merged 7 commits into from Feb 12, 2020

Conversation

@vemel
Copy link
Contributor

@vemel vemel commented Feb 7, 2020 •

I realized that the inheritance of TypedDict does not work as it should.

class _Animal(TypedDict):
     name: str

class Animal(_Animal, total=False):
     voice: str

class Cat(Animal):
     fur_color: str

I would assume that Cat should have required keys name and fur_color and optional voice.
But in reality, it will have required fur_color and optional name and voice, because Animal has total=False

Fixed

  • __required_keys__ and __optional_keys__ are copied from base classes
  • Own TypedDict annotations are added to __annotations__ after base annotations
  • Conflicting required and optional keys from base classes are removed if TypedDict redefines them
@vemel vemel force-pushed the vemel:typed-dict-keys branch from dc2f5df to ebfe321 Feb 7, 2020
@vemel vemel requested a review from ilevkivskyi Feb 7, 2020
vemel added 4 commits Feb 7, 2020
@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Feb 11, 2020

Should this also be updated upstream in CPython?

Copy link
Member

@gvanrossum gvanrossum left a comment

LGTM except I wish to rename _Animal and get rid of tail.

class Cat(Animal):
fur_color: str
tail: int

This comment has been minimized.

@gvanrossum

gvanrossum Feb 11, 2020
Member

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.

This comment has been minimized.

@vemel

vemel Feb 11, 2020
Author Contributor

Makes sense, deleting it. I also will not remove conflicting keys from __required_keys__ and __optional_keys__ since conflicts are not allowed.

This comment has been minimized.

@vemel

vemel Feb 11, 2020
Author Contributor

Pushed changes, updated tests.

@@ -436,6 +436,17 @@ class LabelPoint2D(Point2D, Label): ...
class Options(TypedDict, total=False):
log_level: int
log_path: str
class _Animal(TypedDict):

This comment has been minimized.

@gvanrossum

gvanrossum Feb 11, 2020
Member

I don't see a reason for this class name to start with _ -- maybe rename to BaseAnimal?

This comment has been minimized.

@vemel

vemel Feb 11, 2020
Author Contributor

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.

This comment has been minimized.

@vemel

vemel Feb 11, 2020
Author Contributor

Renamed to BaseAnimal.

…ct keys
@vemel
Copy link
Contributor Author

@vemel vemel commented Feb 11, 2020

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.

@vemel vemel requested a review from gvanrossum Feb 11, 2020
Copy link
Member

@gvanrossum gvanrossum left a comment

Thanks! I will land this despite the 3.5.1 failure.

@gvanrossum gvanrossum merged commit e796957 into python:master Feb 12, 2020
1 check failed
1 check failed
continuous-integration/travis-ci/pr The Travis CI build failed
Details
gvanrossum pushed a commit to python/cpython that referenced this pull request Feb 13, 2020
(Adapted from python/typing#700)
@Zac-HD
Copy link
Contributor

@Zac-HD Zac-HD commented May 4, 2020

@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.

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented May 4, 2020

@domdfcoding
Copy link

@domdfcoding domdfcoding commented Sep 11, 2020

@Zac-HD Did you submit a PR to CPython for this? Having trouble finding it.

@Zac-HD
Copy link
Contributor

@Zac-HD Zac-HD commented Sep 11, 2020

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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.