Skip to content

Commit 002eb00

Browse files
authored
fix: correctly encode type descriptions (#1272)
1 parent 5911622 commit 002eb00

File tree

2 files changed

+80
-22
lines changed

2 files changed

+80
-22
lines changed

python/cocoindex/tests/test_typing.py

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class SimpleDataclass:
3232
value: int
3333

3434

35+
@dataclasses.dataclass
36+
class SimpleDataclassWithDescription:
37+
"""This is a simple dataclass with a description."""
38+
39+
name: str
40+
value: int
41+
42+
3543
class SimpleNamedTuple(NamedTuple):
3644
name: str
3745
value: int
@@ -357,45 +365,96 @@ def test_encode_enriched_type_none() -> None:
357365
assert result is None
358366

359367

360-
def test_encode_enriched_type_struct() -> None:
368+
def test_encode_enriched_dataclass() -> None:
361369
typ = SimpleDataclass
362370
result = encode_enriched_type(typ)
363-
assert result["type"]["kind"] == "Struct"
364-
assert len(result["type"]["fields"]) == 2
365-
assert result["type"]["fields"][0]["name"] == "name"
366-
assert result["type"]["fields"][0]["type"]["kind"] == "Str"
367-
assert result["type"]["fields"][1]["name"] == "value"
368-
assert result["type"]["fields"][1]["type"]["kind"] == "Int64"
371+
assert result == {
372+
"type": {
373+
"kind": "Struct",
374+
"description": "SimpleDataclass(name: str, value: int)",
375+
"fields": [
376+
{"name": "name", "type": {"kind": "Str"}},
377+
{"name": "value", "type": {"kind": "Int64"}},
378+
],
379+
},
380+
}
381+
382+
383+
def test_encode_enriched_dataclass_with_description() -> None:
384+
typ = SimpleDataclassWithDescription
385+
result = encode_enriched_type(typ)
386+
assert result == {
387+
"type": {
388+
"kind": "Struct",
389+
"description": "This is a simple dataclass with a description.",
390+
"fields": [
391+
{"name": "name", "type": {"kind": "Str"}},
392+
{"name": "value", "type": {"kind": "Int64"}},
393+
],
394+
},
395+
}
396+
397+
398+
def test_encode_named_tuple() -> None:
399+
typ = SimpleNamedTuple
400+
result = encode_enriched_type(typ)
401+
assert result == {
402+
"type": {
403+
"kind": "Struct",
404+
"description": "SimpleNamedTuple(name, value)",
405+
"fields": [
406+
{"name": "name", "type": {"kind": "Str"}},
407+
{"name": "value", "type": {"kind": "Int64"}},
408+
],
409+
},
410+
}
369411

370412

371413
def test_encode_enriched_type_vector() -> None:
372414
typ = NDArray[np.float32]
373415
result = encode_enriched_type(typ)
374-
assert result["type"]["kind"] == "Vector"
375-
assert result["type"]["element_type"]["kind"] == "Float32"
376-
assert result["type"]["dimension"] is None
416+
assert result == {
417+
"type": {
418+
"kind": "Vector",
419+
"element_type": {"kind": "Float32"},
420+
"dimension": None,
421+
},
422+
}
377423

378424

379425
def test_encode_enriched_type_ltable() -> None:
380426
typ = list[SimpleDataclass]
381427
result = encode_enriched_type(typ)
382-
assert result["type"]["kind"] == "LTable"
383-
assert "fields" in result["type"]["row"]
384-
assert len(result["type"]["row"]["fields"]) == 2
428+
assert result == {
429+
"type": {
430+
"kind": "LTable",
431+
"row": {
432+
"description": "SimpleDataclass(name: str, value: int)",
433+
"fields": [
434+
{"name": "name", "type": {"kind": "Str"}},
435+
{"name": "value", "type": {"kind": "Int64"}},
436+
],
437+
},
438+
},
439+
}
385440

386441

387442
def test_encode_enriched_type_with_attrs() -> None:
388443
typ = Annotated[str, TypeAttr("key", "value")]
389444
result = encode_enriched_type(typ)
390-
assert result["type"]["kind"] == "Str"
391-
assert result["attrs"] == {"key": "value"}
445+
assert result == {
446+
"type": {"kind": "Str"},
447+
"attrs": {"key": "value"},
448+
}
392449

393450

394451
def test_encode_enriched_type_nullable() -> None:
395452
typ = str | None
396453
result = encode_enriched_type(typ)
397-
assert result["type"]["kind"] == "Str"
398-
assert result["nullable"] is True
454+
assert result == {
455+
"type": {"kind": "Str"},
456+
"nullable": True,
457+
}
399458

400459

401460
def test_encode_scalar_numpy_types_schema() -> None:
@@ -405,10 +464,9 @@ def test_encode_scalar_numpy_types_schema() -> None:
405464
(np.float64, "Float64"),
406465
]:
407466
schema = encode_enriched_type(np_type)
408-
assert schema["type"]["kind"] == expected_kind, (
409-
f"Expected {expected_kind} for {np_type}, got {schema['type']['kind']}"
410-
)
411-
assert not schema.get("nullable", False)
467+
assert schema == {
468+
"type": {"kind": expected_kind},
469+
}, f"Expected kind {expected_kind} for {np_type}, got {schema}"
412470

413471

414472
def test_annotated_struct_with_type_kind() -> None:

python/cocoindex/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def add_fields_from_struct(struct_info: AnalyzedStructType) -> None:
444444
add_fields_from_struct(struct_info)
445445

446446
result["fields"] = fields
447-
if doc := inspect.getdoc(struct_info):
447+
if doc := inspect.getdoc(struct_info.struct_type):
448448
result["description"] = doc
449449
return result, num_key_parts
450450

0 commit comments

Comments
 (0)