A simple typed namespace. At runtime it is equivalent to a plain dict. TypedDict creates a dictionary type such that a type checker will expect all instances to have a certain set of keys, where each key is associated with a value of a consistent type. This expectation is not checke
(typename, fields, /, *, total=True, closed=None,
extra_items=NoExtraItems)
| 3312 | |
| 3313 | |
| 3314 | def TypedDict(typename, fields, /, *, total=True, closed=None, |
| 3315 | extra_items=NoExtraItems): |
| 3316 | """A simple typed namespace. At runtime it is equivalent to a plain dict. |
| 3317 | |
| 3318 | TypedDict creates a dictionary type such that a type checker will expect all |
| 3319 | instances to have a certain set of keys, where each key is |
| 3320 | associated with a value of a consistent type. This expectation |
| 3321 | is not checked at runtime. |
| 3322 | |
| 3323 | Usage:: |
| 3324 | |
| 3325 | >>> class Point2D(TypedDict): |
| 3326 | ... x: int |
| 3327 | ... y: int |
| 3328 | ... label: str |
| 3329 | ... |
| 3330 | >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK |
| 3331 | >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check |
| 3332 | >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') |
| 3333 | True |
| 3334 | |
| 3335 | The type info can be accessed via the Point2D.__annotations__ dict, and |
| 3336 | the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets. |
| 3337 | TypedDict supports an additional equivalent form:: |
| 3338 | |
| 3339 | Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) |
| 3340 | |
| 3341 | By default, all keys must be present in a TypedDict. It is possible |
| 3342 | to override this by specifying totality:: |
| 3343 | |
| 3344 | class Point2D(TypedDict, total=False): |
| 3345 | x: int |
| 3346 | y: int |
| 3347 | |
| 3348 | This means that a Point2D TypedDict can have any of the keys omitted. A type |
| 3349 | checker is only expected to support a literal False or True as the value of |
| 3350 | the total argument. True is the default, and makes all items defined in the |
| 3351 | class body be required. |
| 3352 | |
| 3353 | The Required and NotRequired special forms can also be used to mark |
| 3354 | individual keys as being required or not required:: |
| 3355 | |
| 3356 | class Point2D(TypedDict): |
| 3357 | x: int # the "x" key must always be present (Required is the default) |
| 3358 | y: NotRequired[int] # the "y" key can be omitted |
| 3359 | |
| 3360 | See PEP 655 for more details on Required and NotRequired. |
| 3361 | |
| 3362 | The ReadOnly special form can be used |
| 3363 | to mark individual keys as immutable for type checkers:: |
| 3364 | |
| 3365 | class DatabaseUser(TypedDict): |
| 3366 | id: ReadOnly[int] # the "id" key must not be modified |
| 3367 | username: str # the "username" key can be changed |
| 3368 | |
| 3369 | The closed argument controls whether the TypedDict allows additional |
| 3370 | non-required items during inheritance and assignability checks. |
| 3371 | If closed=True, the TypedDict does not allow additional items:: |
searching dependent graphs…