by insanitybit · 3y ago · 0 comments
edit: I think this is actually a Python type annotation limitation but it's possible that's mypy, although I think in 99% of cases those are fine to conflate (I have used other Python type systems).
JSONObject = None | str | int | bool | list["JSONObject"] | dict[str, "JSONObject"]
# this type checks
a: JSONObject = {"a": [1, 2, "7", True, {"false": None}]}
# this doesn't type check
b: JSONObject = {"a": [1, 2, "7", True, {"false": object()}]} from typing import Dict, List
JSON = Dict[str, 'JSON'] | List['JSON'] | float | int | str | None
I would say the only thing ugly here is the string 'JSON' that is used for recursion. From a static safety perspective with an external type checker the static safety effect is identical.