@@ -84,28 +84,21 @@ Additionally, positional parameters may be combined with an unpacked
8484Semantics
8585---------
8686
87- * Each key in the ``TypedDict `` must be accepted as a keyword parameter.
88- * TypedDict keys cannot be positional-only; they must be valid keyword
89- parameters.
87+ For type-checking purposes, ``Callable[Unpack[TD], R] `` behaves as if it were
88+ specified via a callback protocol whose ``__call__ `` method has
89+ ``**kwargs: Unpack[TD] ``.
90+ The semantics of ``Unpack `` itself are exactly those described in the typing
91+ specification's `Unpack for keyword arguments
92+ <https://typing.python.org/en/latest/spec/callables.html#unpack-for-keyword-arguments> `_
93+ section and :pep: `692 `, together with :pep: `728 ` for ``extra_items `` and
94+ ``closed ``.
95+
96+ This PEP only adds the following Callable-specific rules:
97+
98+ * ``Unpack[TD] `` may appear inside the parameter list of
99+ ``Callable ``.
90100* Positional parameters may appear in ``Callable `` before ``Unpack[TD] `` and
91- follow normal ``Callable `` semantics.
92- * Required keys must be accepted, but may correspond to parameters with a
93- default value.
94- * ``NotRequired `` keys must still be accepted, but may be omitted at call sites.
95- This respectively applies to ``TypedDict `` with ``total=False ``.
96- * Functions with ``**kwargs `` are compatible if the annotation of ``**kwargs ``
97- matches or is a supertype of the ``TypedDict `` values.
98- * ``extra_items `` from :pep: `728 ` is respected: functions accepting additional
99- ``**kwargs `` are valid if their annotation is compatible with the declared
100- type.
101- * If neither ``extra_items `` nor ``closed `` (:pep: `728 `) is specified on the
102- ``TypedDict ``, additional keyword arguments are implicitly permitted with
103- type ``object `` (i.e., compatible with ``**kwargs: object ``). Setting
104- ``closed=True `` forbids any additional keyword arguments beyond the keys
105- declared in the ``TypedDict ``. Setting ``extra_items `` to a specific type
106- requires that any additional keyword arguments match that type.
107- * Only a single ``TypedDict `` may be unpacked inside a ``Callable ``. Support
108- for multiple unpacks may be considered in the future.
101+ follow existing ``Callable `` semantics.
109102
110103Examples
111104--------
@@ -281,9 +274,13 @@ taught that with ::
281274 ``__call__(self, **kwargs: Unpack[Signature]) -> R ``
282275 or
283276 ``__call__(self, a: int, b: str = ..., **kwargs: object) -> R ``.
277+ * Teachers might want to introduce the concept of ``TypedDict `` with
278+ ``Callable `` first before introducing ``Protocol ``.
284279* The implicit addition of ``**kwargs: object `` might come surprising to users,
285280 using ``closed=True `` for definitions will create the more intuitive
286281 equivalence of ``__call__(self, a: int, b: str = ...) -> R ``
282+ * Users should be made aware of the interaction with ``extra_items `` from
283+ :pep: `728 `.
287284
288285
289286Reference Implementation
@@ -311,9 +308,6 @@ Open Questions
311308 how to handle overlapping keys of non-identical types? Which restrictions
312309 should apply in such a case? Should the order matter?
313310* Is there a necessity to differentiate between normal and ``ReadOnly `` keys?
314- * Is it necessary to specify generic behavior for ``TypedDict `` and the
315- resulting ``Callable `` when the ``TypedDict `` itself is generic?
316-
317311
318312Acknowledgements
319313================
0 commit comments