Improve JSON typing by including Mapping/Sequence variants.
Review Request #13125 — Created June 26, 2023 and submitted — Latest diff uploaded
The new JSON typing introduced in Djblets 3.3 worked fine if exclusively
JSONList, or simple primitives
bool). However, trying to assign an
List(such as a
List[int]) would result in typing errors.
This was due to the mutability of the type. A
Dict[str, str], for
example, cannot be assigned as a
JSONDict, because a
be able to set any
JSONValueas a value, and that would violate the
type of a
Dict[str, str]. Same with lists.
The solution to this is to accept a
Sequence. These are
both immutable types, and from a typing perspective, there's no risk to
storing the wrong value there. In part, this is because we already can't
deeply key into a nested
JSONDictwithout casting at each level
anyway, since a value could be any supported JSON type. So once we've
stored it, it's up to the callers to validate types anyway (and at this
complexity, we're wanting to represent structures as
JSONListImmutableprovide this typing
option. They're part of the
JSONValueunion, allowing type checkers to
check against them when assigning a
Going forward, we'll want to aim to use
Iterable, and other more generic immutable types over
Listany time we're accepting a value we don't plan to modify, to
avoid the sort of problems we've uncovered with our
Encountered this issue with in-progress code. Verified that the this
addressed all issues.
Played around a fair amount with creating and typing deeply-nested
JSON data from dicts and lists containing compatible values, and
verified that pyright and mypy were happy.