I’m trying to define a type that supports the following dict:
test: NestedRecord = {
"one": "one",
"two": 2,
"three": True,
"four": [1, 2, 3],
"five": ["one", "two", "three"],
"another": {
"one": {"one": ["one"]},
},
"six": [{"one": "one"}],
"seven": [{"one": "one"}, {"two": 2, "three": True}],
"eight": [{"one": "one"}, {"two": 2, "three": True, "four": {"one": "one"}}],
}
It’s a Dict of strings and either strings, nums, bools, lists of them or itself / lists of itself.
I’ve created this definition:
ValidTypes = Union[str, bool, int, float]
ValidLists = Union[List[str], List[bool], List[int], List[float]]
NestedRecord = Dict[
str, Union[ValidTypes, ValidLists, "NestedRecord", List["NestedRecord"]]
]
But it errors on the six
entry and below:
Dict entry 6 has incompatible type "str": "list[dict[str, str]]"; expected "str": "str | int | float | list[str] | list[bool] | list[int] | list[float] | NestedRecord | list[NestedRecord]"
.
It’s basically on the typing.List['NestedRecord']
. It doesn’t support lists of the dictionaries