Skip to content

bpo-39627: Fix TypedDict totality check for inherited keys #18503

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

Merged
merged 3 commits into from
Feb 13, 2020

Conversation

vemel
Copy link
Contributor

@vemel vemel commented Feb 13, 2020

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

class BaseAnimal(TypedDict):
     name: str

class Animal(BaseAnimal, 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

https://bugs.python.org/issue39627

@gvanrossum
Copy link
Member

Can you fix the test? To run locally, build python.exe or python (by running ./configure; make -j) and then /.python[.exe] -m test test_typing -v

Also, please add a news item, and a reference to the fix in the python/typing repo.

@vemel
Copy link
Contributor Author

vemel commented Feb 13, 2020

I forgot to copy a couple of lines, now tests are passing.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@gvanrossum gvanrossum merged commit 10e87e5 into python:master Feb 13, 2020
@bedevere-bot
Copy link

@gvanrossum: Please replace # with GH- in the commit message next time. Thanks!

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

Successfully merging this pull request may close these issues.

4 participants