@@ -12,8 +12,6 @@ Python-Version: 3.13
12
12
Post-History: `09-Feb-2024 <https://discuss.python.org/t/pep-728-typeddict-with-typed-extra-items/45443 >`__,
13
13
14
14
15
- .. highlight :: rst
16
-
17
15
Abstract
18
16
========
19
17
@@ -286,17 +284,17 @@ added in a subclass, all of the following conditions should apply:
286
284
287
285
- If ``__extra__ `` is read-only
288
286
289
- - The item can be either required or non-required
287
+ - The item can be either required or non-required
290
288
291
- - The item's value type is consistent with ``T ``
289
+ - The item's value type is consistent with ``T ``
292
290
293
291
- If ``__extra__ `` is not read-only
294
292
295
- - The item is non-required
293
+ - The item is non-required
296
294
297
- - The item's value type is consistent with ``T ``
295
+ - The item's value type is consistent with ``T ``
298
296
299
- - ``T `` is consistent with the item's value type
297
+ - ``T `` is consistent with the item's value type
300
298
301
299
- If ``__extra__ `` is not redeclared, the subclass inherits it as-is.
302
300
@@ -337,19 +335,19 @@ infinite set of items that all satisfy the following conditions:
337
335
338
336
- If ``__extra__ `` is read-only
339
337
340
- - The key's value type is consistent with ``T ``
338
+ - The key's value type is consistent with ``T ``
341
339
342
- - The key is not in ``S ``.
340
+ - The key is not in ``S ``.
343
341
344
342
- If ``__extra__ `` is not read-only
345
343
346
- - The key is non-required
344
+ - The key is non-required
347
345
348
- - The key's value type is consistent with ``T ``
346
+ - The key's value type is consistent with ``T ``
349
347
350
- - ``T `` is consistent with the key's value type
348
+ - ``T `` is consistent with the key's value type
351
349
352
- - The key is not in ``S ``.
350
+ - The key is not in ``S ``.
353
351
354
352
For type checking purposes, let ``__extra__ `` be a non-required pseudo-item to
355
353
be included whenever "for each ... item/key" is stated in
@@ -362,25 +360,25 @@ and we modify it as follows:
362
360
363
361
* For each item in ``B ``, ``A `` has the corresponding key, unless the item
364
362
in ``B `` is read-only, not required, and of top value type
365
- (``ReadOnly[NotRequired[object]] ``). [Edit: Otherwise, if the
363
+ (``ReadOnly[NotRequired[object]] ``). ** [Edit: Otherwise, if the
366
364
corresponding key with the same name cannot be found in ``A``, "__extra__"
367
- is considered the corresponding key.]
365
+ is considered the corresponding key.] **
368
366
369
- * For each item in ``B ``, if ``A `` has the corresponding key [Edit: or
370
- "__extra__"], the corresponding value type in ``A `` is consistent with the
367
+ * For each item in ``B ``, if ``A `` has the corresponding key ** [Edit: or
368
+ "__extra__"] ** , the corresponding value type in ``A `` is consistent with the
371
369
value type in ``B ``.
372
370
373
371
* For each non-read-only item in ``B ``, its value type is consistent with
374
- the corresponding value type in ``A ``. [Edit: if the corresponding key
372
+ the corresponding value type in ``A ``. ** [Edit: if the corresponding key
375
373
with the same name cannot be found in ``A``, "__extra__" is considered the
376
- corresponding key.]
374
+ corresponding key.] **
377
375
378
376
* For each required key in ``B ``, the corresponding key is required in ``A ``.
379
377
For each non-required key in ``B ``, if the item is not read-only in ``B ``,
380
378
the corresponding key is not required in ``A ``.
381
- [Edit: if the corresponding key with the same name cannot be found in
379
+ ** [Edit: if the corresponding key with the same name cannot be found in
382
380
``A``, "__extra__" is considered to be non-required as the corresponding
383
- key.]
381
+ key.] **
384
382
385
383
The following examples illustrate these checks in action.
386
384
@@ -412,7 +410,7 @@ corresponding key. ``"year"`` being required violates the rule "For each
412
410
required key in ``B ``, the corresponding key is required in ``A ``".
413
411
414
412
When ``__extra__ `` is defined to be read-only in a TypedDict type, it is possible
415
- for an item to have a narrower type than ``__extra__ ``'s value type.
413
+ for an item to have a narrower type than ``__extra__ ``'s value type::
416
414
417
415
class Movie(TypedDict):
418
416
name: str
@@ -450,7 +448,7 @@ Interaction with Mapping[KT, VT]
450
448
A TypedDict type can be consistent with ``Mapping[KT, VT] `` types other than
451
449
``Mapping[str, object] `` as long as the union of value types on the TypedDict
452
450
type is consistent with ``VT ``. It is an extension of this rule from the typing
453
- spec::
451
+ spec:
454
452
455
453
* A TypedDict with all ``int `` values is not consistent with
456
454
``Mapping[str, int] ``, since there may be additional non-``int ``
@@ -509,7 +507,7 @@ For example::
509
507
regular_dict: dict[str, int] = not_required_num # OK
510
508
f(not_required_num) # OK
511
509
512
- In this case, methods that are previously unavailable on a TypedDict are allowed.
510
+ In this case, methods that are previously unavailable on a TypedDict are allowed::
513
511
514
512
not_required_num.clear() # OK
515
513
@@ -631,7 +629,7 @@ Acknowledgments
631
629
632
630
Thanks to Jelle Zijlstra for sponsoring this PEP and providing review feedback,
633
631
Eric Traut who `proposed the original design
634
- <https://mail.python.org/archives/list/[email protected] /message/3Z72OQWVTOVS6UYUUCCII2UZN56PV5II/> `
632
+ <https://mail.python.org/archives/list/[email protected] /message/3Z72OQWVTOVS6UYUUCCII2UZN56PV5II/> `
__
635
633
this PEP iterates on, and Alice Purcell for offering their perspective as the
636
634
author of :pep: `705 `.
637
635
0 commit comments