Comment Comment by LtWorf With Literal you can also do this: @dataclass class Message: event: Literal['message'] msg: str @dataclass class File: event: Literal['file'] url: str typedload.load(data, File | Message) Where data is something like `{'event': 'message', 'msg': 'bla'}`.After the load, you can trust that your objects are well formed and let mypy do its thing.So types can be useful at runtime as well. Otherwise the alternative would be to use raw dictionary, which mypy can't check.pydantic does a similar thing but it uses its own different typing, so it needs a mypy plugin or it flags everything as wrong. parent LtWorf 4 years ago
After the load, you can trust that your objects are well formed and let mypy do its thing.
So types can be useful at runtime as well. Otherwise the alternative would be to use raw dictionary, which mypy can't check.
pydantic does a similar thing but it uses its own different typing, so it needs a mypy plugin or it flags everything as wrong.