Skip to content

Commit 61ff60e

Browse files
PEP 728: Expand previous discussions and "How to teach this" (#4539)
The SC reached out to ask for more motivation and a better "How to teach this" section. I gathered some previous discussions of this feature and rewrote the teaching section.
1 parent f7d08a0 commit 61ff60e

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

peps/pep-0728.rst

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ Given the usage of pre-:pep:`692` type annotation for ``**kwargs`` in existing
123123
codebases, it will be valuable to accept and type extra items on TypedDict so
124124
that the old typing behavior can be supported in combination with ``Unpack``.
125125

126+
Previous Discussions
127+
--------------------
128+
129+
The new features introduced in this PEP would address several long-standing feature
130+
requests in the type system. Previous discussions include:
131+
132+
- `Mypy issue <https://github.com/python/mypy/issues/7981>`__ asking for a "final TypedDict"
133+
(2019). While the discussion focuses on the ``@final`` decorator, the underlying feature request
134+
would be addressed by this PEP.
135+
- `Mailing list thread <https://mail.python.org/archives/list/[email protected]/thread/66RITIHDQHVTUMJHH2ORSNWZ6DOPM367/>`__
136+
asking for a way to say that a ``TypedDict`` can contain arbitrary extra keys (2020).
137+
- `Discussion <https://discuss.python.org/t/pep-692-using-typeddict-for-more-precise-kwargs-typing/17314/87>`__
138+
about an extension of the ``Unpack`` mechanism introduced by :pep:`692` (2023).
139+
- `PEP 705 <https://discuss.python.org/t/pep-705-typeddict-read-only-and-other-keys/36457>`__ in an earlier
140+
draft proposed a similar feature (2023); it was removed to keep that PEP simpler.
141+
- `Discussion <https://discuss.python.org/t/do-we-want-an-exact-typeddict-if-so-how-final-extras-never/44418>`__
142+
about an "exact" ``TypedDict`` (2024).
143+
126144
Rationale
127145
=========
128146

@@ -747,12 +765,22 @@ valid definition that indicates all extra items must be ``None``.)
747765
How to Teach This
748766
=================
749767

750-
The choice of the spelling ``"extra_items"`` is intended to make this
751-
feature more understandable to new users compared to shorter alternatives like
752-
``"extra"``.
753-
754-
Details of this should be documented in both the typing spec and the
755-
:mod:`typing` documentation.
768+
The new features introduced in this PEP can be taught together with the concept
769+
of inheritance as it applies to ``TypedDict``. A possible outline could be:
770+
771+
- Basics of ``TypedDict``: a ``dict`` with a fixed set of keys and value types.
772+
- ``NotRequired``, ``Required``, and ``total=False``: keys that may be missing.
773+
- ``ReadOnly``: keys that cannot be modified.
774+
- Inheritance: subclasses can add new keys. As a corollary, a value of a ``TypedDict``
775+
type may contain additional keys at runtime that are not specified in the type.
776+
- ``closed=True``: disallowing additional keys and restricting inheritance.
777+
- ``extra_items=VT``: allowing additional keys with a specified value type.
778+
779+
The concept of a closed ``TypedDict`` should also be cross-referenced in documentation
780+
for related concepts. For example, type narrowing with the ``in`` operator works
781+
differently, perhaps more intuitively, with closed ``TypedDict`` types. In addition, when ``Unpack``
782+
is used for keyword arguments, a closed ``TypedDict`` can be useful to restrict the
783+
allowed keyword arguments.
756784

757785
Backwards Compatibility
758786
=======================

0 commit comments

Comments
 (0)