Commit f994bd31 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Support annotated types in json.py

parent b679dc77
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ JSON classes for container types (objects and arrays)

from __future__ import annotations

from types import GenericAlias
from typing import Any
from typing import Callable
from typing import TypeVar
@@ -35,13 +36,15 @@ class JSONPathMixin:
	@overload
	def path(self, path: str, kind: type[T], convert: Callable[[T], C]) -> C: ...

	def path(self, path: str, kind: type[T], convert: Callable[[T], C]|None = None) -> T|C:
	def path(self, path: str, kind: type[T]|GenericAlias, convert: Callable[[T], C]|None = None) -> T|C:
		result = JSONPath(path).parse(self)
		if "*" not in path:
			try:
				result = result[0]
			except IndexError:
				raise KeyError(path) from None
		if isinstance(kind, GenericAlias):
			kind = kind.__origin__
		if not isinstance(result, kind):
			raise TypeError(f"{path} is wrong type; expected {kind}; got {type(result)}")
		if convert is None: