This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: TypedDict docs are incomplete
Type: Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: JelleZijlstra Nosy List: 97littleleaf11, AlexWaygood, CharlieZhao, JelleZijlstra, docs@python, sobolevn
Priority: normal Keywords: patch

Created on 2022-02-07 19:27 by JelleZijlstra, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31349 merged CharlieZhao, 2022-02-15 06:43
PR 31808 merged CharlieZhao, 2022-03-11 04:21
PR 31815 merged CharlieZhao, 2022-03-11 07:09
Messages (8)
msg412784 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-02-07 19:27
https://docs.python.org/3.10/library/typing.html#typing.TypedDict

It says:

> To allow using this feature with older versions of Python that do not support PEP 526, TypedDict supports two additional equivalent syntactic forms

But there is another reason to use the equivalent forms: if your keys aren't valid Python names. There's an example in typeshed that uses "in" (a keyword) as a TypedDict key, and I've seen others with keys that have hyphens in them.

Also:

- The docs mention attributes like `__required_keys__`, but don't clearly say what is in these attributes. We should document them explicitly with the standard syntax for attributes.
- There is no mention of one TypedDict inheriting from another.
msg413054 - (view) Author: Charlie Zhao (CharlieZhao) * Date: 2022-02-11 09:42
Indeed, if you use Python keywords or other invalid Python names as keys of TypedDict, we must use the equivalent forms as follows: 

```
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'in': str})  # OK

Point2D = TypedDict('Point2D', x=int, y=int, in=str)  # Error
```

Maybe we should add this to the docs.


And, it seems that adding examples for inheritance and attributes of TypedDict would make the docs clearer.

Would you mind if I submit a PR for those changes. :)
msg413078 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-02-11 15:37
Please do submit a PR! Agree that more examples of inheritance and attributes would be useful.

Note that we're about to deprecate the keyword argument syntax (issue46066).
msg414860 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-10 16:41
New changeset 8a207e0321db75f3342692905e342f1d5e1add54 by Charlie Zhao in branch 'main':
bpo-46677: Add examples of inheritance and attributes to `TypedDict` docs (GH-31349)
https://github.com/python/cpython/commit/8a207e0321db75f3342692905e342f1d5e1add54
msg414861 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-10 16:43
(assigning to myself to remind myself to see the backports through)
msg414959 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-12 01:04
New changeset 28f20a6613b9d9287848bb78369b881a72941a39 by Charlie Zhao in branch '3.10':
[3.10] bpo-46677: Add examples of inheritance and attributes to `TypedDict` docs (GH-31349) (GH-31815)
https://github.com/python/cpython/commit/28f20a6613b9d9287848bb78369b881a72941a39
msg414963 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-12 01:14
New changeset b5140a5811aa35f4b488849fb55d84504732d135 by Charlie Zhao in branch '3.9':
[3.9] bpo-46677: Add examples of inheritance and attributes to `TypedDict` docs. (GH-31349) (GH-31808)
https://github.com/python/cpython/commit/b5140a5811aa35f4b488849fb55d84504732d135
msg414964 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-12 01:15
Thanks @CharliieZhao for the improved documentation!
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90835
2022-03-12 01:15:11JelleZijlstrasetstatus: open -> closed
resolution: fixed
messages: + msg414964

stage: patch review -> resolved
2022-03-12 01:14:30JelleZijlstrasetmessages: + msg414963
2022-03-12 01:04:00JelleZijlstrasetmessages: + msg414959
2022-03-11 07:09:36CharlieZhaosetpull_requests: + pull_request29913
2022-03-11 04:21:57CharlieZhaosetstage: backport needed -> patch review
pull_requests: + pull_request29907
2022-03-10 16:54:12AlexWaygoodsetstage: patch review -> backport needed
2022-03-10 16:43:52JelleZijlstrasetassignee: docs@python -> JelleZijlstra
messages: + msg414861
2022-03-10 16:41:04JelleZijlstrasetmessages: + msg414860
2022-02-15 06:43:24CharlieZhaosetkeywords: + patch
stage: patch review
pull_requests: + pull_request29498
2022-02-11 15:37:28JelleZijlstrasetmessages: + msg413078
2022-02-11 09:42:36CharlieZhaosetnosy: + CharlieZhao
messages: + msg413054
2022-02-07 19:27:59JelleZijlstracreate