|
2 | 2 | Title: Marking individual TypedDict items as required or potentially-missing
|
3 | 3 | Author: David Foster <david at dafoster.net>
|
4 | 4 | Sponsor: Guido van Rossum <guido at python.org>
|
5 |
| -Discussions-To: typing-sig at python.org |
| 5 | +Discussions-To: https://mail.python.org/archives/list/typing-sig@python.org/thread/53XVOD5ZUKJ263MWA6AUPEA6J7LBBLNV/ |
6 | 6 | Status: Draft
|
7 | 7 | Type: Standards Track
|
8 | 8 | Content-Type: text/x-rst
|
@@ -315,7 +315,7 @@ Usage in Python <3.11
|
315 | 315 | If your code supports Python <3.11 and wishes to use ``Required[]`` or
|
316 | 316 | ``NotRequired[]`` then it should use ``typing_extensions.TypedDict`` rather
|
317 | 317 | than ``typing.TypedDict`` because the latter will not understand
|
318 |
| -``(Not)Required[]``. In particular ``__required_keys__`` and |
| 318 | +``(Not)Required[]``. In particular ``__required_keys__`` and |
319 | 319 | ``__optional_keys__`` on the resulting TypedDict type will not be correct:
|
320 | 320 |
|
321 | 321 | Yes (Python 3.11+ only):
|
@@ -346,11 +346,11 @@ No (Python <3.11 and 3.11+):
|
346 | 346 |
|
347 | 347 | from typing import TypedDict # oops: should import from typing_extensions instead
|
348 | 348 | from typing_extensions import NotRequired
|
349 |
| - |
| 349 | + |
350 | 350 | class Movie(TypedDict):
|
351 | 351 | title: str
|
352 | 352 | year: NotRequired[int]
|
353 |
| - |
| 353 | + |
354 | 354 | assert Movie.__required_keys__ == frozenset({'title', 'year'}) # yikes
|
355 | 355 | assert Movie.__optional_keys__ == frozenset() # yikes
|
356 | 356 |
|
@@ -464,7 +464,7 @@ as the type of a variable which is only conditionally defined:
|
464 | 464 |
|
465 | 465 | class MyClass:
|
466 | 466 | attr: int|Missing
|
467 |
| - |
| 467 | + |
468 | 468 | def __init__(self, set_attr: bool) -> None:
|
469 | 469 | if set_attr:
|
470 | 470 | self.attr = 10
|
@@ -533,7 +533,7 @@ or a check against ``locals()`` for local variables:
|
533 | 533 | packet_bytes: Union[str, Missing]
|
534 | 534 | if packet_data is not None:
|
535 | 535 | packet_bytes = packet.data.encode('utf-8')
|
536 |
| - |
| 536 | + |
537 | 537 | if 'packet_bytes' in locals():
|
538 | 538 | reveal_type(packet_bytes) # bytes
|
539 | 539 | socket.send(packet_bytes)
|
|
0 commit comments