June 2019
Beginner to intermediate
770 pages
19h 24m
English
The as_dict() methods in both the Blog class and Post class shown earlier use a simplistic Dict[str, Any] type hint for a data structure that's compatible with JSON serialization. While the type hint was meaningful for those examples, this isn't an ideal general description of the types used by JSON serialization.
The actual type that can be readily serialized could be defined like this:
from typing import Union, Dict, List, TypeJSON = Union[Dict[str, 'JSON'], List['JSON'], int, str, float, bool, Type[None]]
Currently, mypy doesn't handle recursive types gracefully. Therefore, we're forced to use the following:
JSON = Union[Dict[str, Any], List[Any], int, str, float, bool, Type[None]]
The classes defined in this chapter don't ...
Read now
Unlock full access