Skip to content

Commit b3d39f7

Browse files
committed
feat(convert): refine JSON type handling
1 parent 086622d commit b3d39f7

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

python/cocoindex/convert.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import datetime
77
import inspect
88
from enum import Enum
9-
from typing import Any, Callable, Mapping, Type, get_args, get_origin
9+
from typing import Any, Callable, Mapping, Type, get_origin
1010

1111
import numpy as np
1212

@@ -21,7 +21,6 @@
2121
AnalyzedTypeInfo,
2222
AnalyzedUnionType,
2323
AnalyzedUnknownType,
24-
TypeKind,
2524
analyze_type_info,
2625
encode_enriched_type,
2726
is_namedtuple_type,
@@ -56,13 +55,17 @@ def encode_engine_value(
5655
if isinstance(value, (list, tuple)):
5756
return [encode_engine_value(v, in_struct) for v in value]
5857
if isinstance(value, dict):
59-
is_json_type = type_hint and any(
60-
isinstance(arg, TypeKind) and arg.kind == "Json"
61-
for arg in get_args(type_hint)[1:]
62-
)
58+
is_json_type = False
59+
if type_hint:
60+
type_info = analyze_type_info(type_hint)
61+
is_json_type = (
62+
isinstance(type_info.variant, AnalyzedBasicType)
63+
and type_info.variant.kind == "Json"
64+
)
6365

6466
# For empty dicts, check type hints if in a struct context
6567
# when no contexts are provided, return an empty dict as default
68+
# TODO: always pass in the type annotation to make this robust
6669
if not value:
6770
if in_struct:
6871
return value if is_json_type else []

0 commit comments

Comments
 (0)