Answers for "python create type for heterogeneous dict"

0

python create type for heterogeneous dict

from typing_extensions import TypedDict
class Point2D(TypedDict):
    x: int
    y: int
    label: str

a: Point2D = {'x': 1, 'y': 2, 'label': 'good'}  # OK
b: Point2D = {'z': 3, 'label': 'bad'}           # Fails type check

assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
Posted by: Guest on May-26-2021

Python Answers by Framework

Browse Popular Code Answers by Language