classification
Title: TypedDict(...) as function does not respect "total" when setting __required_keys__ and __optional_keys__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex.gronholm, brandtbucher, domdfcoding, gvanrossum, levkivskyi, miss-islington
Priority: normal Keywords: patch

Created on 2020-10-17 09:23 by alex.gronholm, last changed 2021-04-12 17:17 by gvanrossum. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22736 merged alex.gronholm, 2020-10-17 09:39
PR 23747 merged miss-islington, 2020-12-13 00:44
Messages (9)
msg378805 - (view) Author: Alex Grönholm (alex.gronholm) * Date: 2020-10-17 09:23
>>> DummyDict = TypedDict('DummyDict', {'x': int, 'y': str}, total=False)
>>> DummyDict.__required_keys__
frozenset({'x', 'y'})

This happens because the TypedDict function does not pass the "total" metaclass argument to _TypedDictMeta() (instead passing "__total__" in the attribute namespace) and the new code that sets __required_keys__ and __optional_keys__ only checks the metaclass argument.
msg378819 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-10-17 15:08
Can you submit a PR to fix this? It looks like you already have a good understanding of the root cause of the problem, so it should be easy to fix. (Be sure to add a unit test.)

It can be backported to earlier Python versions that have typing.TypedDict (3.8+).
msg378820 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-10-17 15:09
Whoops, sorry, didn't see that you already have a PR. I'll review it next week during the core sprint.
msg382702 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-12-08 00:31
Looking for another reviewer.
msg382851 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2020-12-10 21:49
New changeset 67b769f5157c9dad1c7dd6b24e067b9fdab5b35d by Alex Grönholm in branch 'master':
bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736)
https://github.com/python/cpython/commit/67b769f5157c9dad1c7dd6b24e067b9fdab5b35d
msg383010 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2020-12-14 22:33
New changeset dbb00062dc3afb12c41c87564e6faefe60766b01 by Miss Islington (bot) in branch '3.9':
bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) (GH-23747)
https://github.com/python/cpython/commit/dbb00062dc3afb12c41c87564e6faefe60766b01
msg387885 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-03-01 23:40
We're looking for a volunteer to port this code to typing_extensions:
https://github.com/python/typing/issues/761
msg390822 - (view) Author: Dominic Davis-Foster (domdfcoding) * Date: 2021-04-12 06:15
I have backported this to typing_extensions in https://github.com/python/typing/pull/778

On Python 3.9.2 and above typing.TypedDict is used, otherwise it's typing_extensions.TypedDict.
msg390870 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-04-12 17:17
Dominic, thanks so much for the backport!
History
Date User Action Args
2021-04-12 17:17:21gvanrossumsetmessages: + msg390870
2021-04-12 06:15:24domdfcodingsetnosy: + domdfcoding
messages: + msg390822
2021-03-01 23:40:45gvanrossumsetmessages: + msg387885
2020-12-15 00:08:30brandtbuchersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-12-14 22:33:37brandtbuchersetmessages: + msg383010
2020-12-13 00:44:57miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22605
2020-12-10 21:49:14brandtbuchersetmessages: + msg382851
2020-12-08 18:12:27brandtbuchersetnosy: + brandtbucher
2020-12-08 00:31:53gvanrossumsetmessages: + msg382702
2020-12-07 19:50:16brandtbucherlinkissue42592 superseder
2020-10-17 15:09:46gvanrossumsetmessages: + msg378820
2020-10-17 15:08:29gvanrossumsetmessages: + msg378819
2020-10-17 10:01:16xtreaksetnosy: + gvanrossum, levkivskyi
2020-10-17 09:39:24alex.gronholmsetkeywords: + patch
stage: patch review
pull_requests: + pull_request21699
2020-10-17 09:23:29alex.gronholmcreate