Skip to content

Commit

Permalink
Code cleanups for basic type encoding in Python SDK.
Browse files Browse the repository at this point in the history
  • Loading branch information
badmonster0 committed Mar 6, 2025
1 parent 4ddb948 commit 0994ac7
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions python/cocoindex/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
class Vector(NamedTuple):
dim: int | None

class NumBits(NamedTuple):
bits: int

class TypeKind(NamedTuple):
kind: str
class TypeAttr:
key: str
value: Any
Expand All @@ -16,10 +15,10 @@ def __init__(self, key: str, value: Any):
self.key = key
self.value = value

Float32 = Annotated[float, NumBits(32)]
Float64 = Annotated[float, NumBits(64)]
Range = Annotated[tuple[int, int], 'range']
Json = Annotated[Any, 'json']
Float32 = Annotated[float, TypeKind('Float32')]
Float64 = Annotated[float, TypeKind('Float64')]
Range = Annotated[tuple[int, int], TypeKind('Range')]
Json = Annotated[Any, TypeKind('Json')]

def _find_annotation(metadata, cls):
for m in iter(metadata):
Expand All @@ -32,36 +31,36 @@ def _get_origin_type_and_metadata(t):
return (t.__origin__, t.__metadata__)
return (t, ())

def _basic_type_to_json_value(t, metadata):
def _type_to_json_value(t, metadata):
origin_type = typing.get_origin(t)
if origin_type is collections.abc.Sequence or origin_type is list:
dim = _find_annotation(metadata, Vector)
if dim is None:
raise ValueError(f"Vector dimension not found for {t}")
args = typing.get_args(t)
origin_type, metadata = _get_origin_type_and_metadata(args[0])
type_json = {
'kind': 'Vector',
'element_type': _basic_type_to_json_value(*_get_origin_type_and_metadata(args[0])),
'element_type': _type_to_json_value(origin_type, metadata),
'dimension': dim.dim,
}
else:
if t is bytes:
kind = 'Bytes'
elif t is str:
kind = 'Str'
elif t is bool:
kind = 'Bool'
elif t is int:
kind = 'Int64'
elif t is float:
num_bits = _find_annotation(metadata, NumBits)
kind = 'Float32' if num_bits is not None and num_bits.bits <= 32 else 'Float64'
elif t is Range:
kind = 'Range'
elif t is Json:
kind = 'Json'
type_kind = _find_annotation(metadata, TypeKind)
if type_kind is not None:
kind = type_kind.kind
else:
raise ValueError(f"type unsupported yet: {t}")
if t is bytes:
kind = 'Bytes'
elif t is str:
kind = 'Str'
elif t is bool:
kind = 'Bool'
elif t is int:
kind = 'Int64'
elif t is float:
kind = 'Float64'
else:
raise ValueError(f"type unsupported yet: {t}")
type_json = { 'kind': kind }

return type_json
Expand All @@ -70,7 +69,7 @@ def _enriched_type_to_json_value(t) -> dict[str, Any] | None:
if t is None:
return None
t, metadata = _get_origin_type_and_metadata(t)
enriched_type_json = {'type': _basic_type_to_json_value(t, metadata)}
enriched_type_json = {'type': _type_to_json_value(t, metadata)}
attrs = None
for attr in metadata:
if isinstance(attr, TypeAttr):
Expand Down

0 comments on commit 0994ac7

Please sign in to comment.