Loading behave_utils/json.py +10 −5 Original line number Diff line number Diff line Loading @@ -36,12 +36,17 @@ class JSONPathMixin: 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: result = JSONPath(path).parse(self)[0] if convert is not None: return convert(result) elif isinstance(result, kind): result = JSONPath(path).parse(self) if "*" not in path: try: result = result[0] except IndexError: raise KeyError(path) from None if not isinstance(result, kind): raise TypeError(f"{path} is wrong type; expected {kind}; got {type(result)}") if convert is None: return result raise ValueError(f"{path} is wrong type; expected {kind}; got {type(result)}") return convert(result) class JSONObject(JSONPathMixin, dict[str, Any]): Loading Loading
behave_utils/json.py +10 −5 Original line number Diff line number Diff line Loading @@ -36,12 +36,17 @@ class JSONPathMixin: 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: result = JSONPath(path).parse(self)[0] if convert is not None: return convert(result) elif isinstance(result, kind): result = JSONPath(path).parse(self) if "*" not in path: try: result = result[0] except IndexError: raise KeyError(path) from None if not isinstance(result, kind): raise TypeError(f"{path} is wrong type; expected {kind}; got {type(result)}") if convert is None: return result raise ValueError(f"{path} is wrong type; expected {kind}; got {type(result)}") return convert(result) class JSONObject(JSONPathMixin, dict[str, Any]): Loading