fix(grok): implement official hook contract
This commit is contained in:
1 parent
978e15e251
commit
a5a250ac94
17 files changed
+2175
-785
No files matched your search
@@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, Dict, List, Tuple
|
||||
from typing import Any, Dict, List, NoReturn, Tuple
|
||||
|
||||
|
||||
JsonObject = Dict[str, Any]
|
||||
@@ -13,6 +13,10 @@ class _DuplicateKey(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
class _InvalidConstant(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _unique_object(pairs: List[Tuple[str, Any]]) -> JsonObject:
|
||||
result: JsonObject = {}
|
||||
for key, value in pairs:
|
||||
@@ -22,10 +26,18 @@ def _unique_object(pairs: List[Tuple[str, Any]]) -> JsonObject:
|
||||
return result
|
||||
|
||||
|
||||
def _reject_constant(value: str) -> NoReturn:
|
||||
raise _InvalidConstant(f'invalid numeric constant "{value}"')
|
||||
|
||||
|
||||
def parse_json_object(raw: str, label: str) -> JsonObject:
|
||||
try:
|
||||
value = json.loads(raw, object_pairs_hook=_unique_object)
|
||||
except (_DuplicateKey, json.JSONDecodeError) as error:
|
||||
value = json.loads(
|
||||
raw,
|
||||
object_pairs_hook=_unique_object,
|
||||
parse_constant=_reject_constant,
|
||||
)
|
||||
except (_DuplicateKey, _InvalidConstant, json.JSONDecodeError) as error:
|
||||
raise ValueError(f"Failed to parse {label}: {error}") from error
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(f"{label} must contain a JSON object")
|
||||
|
||||
Reference in new issue
Block a user