Skip to content

Commit d1a9297

Browse files
committed
formatting conditional statements and list comprehensions
1 parent e1a777a commit d1a9297

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

python/cocoindex/convert.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,38 @@ def _is_ktable_dict(value: dict[Any, Any]) -> bool:
3333
def encode_engine_value_with_type(value: Any, target_type: Any = None) -> Any:
3434
"""Encode a Python value to an engine value with optional type awareness."""
3535
# Handle dict-to-struct conversion when target type is a struct
36-
if (isinstance(value, dict) and
37-
target_type is not None and
38-
not _is_ktable_dict(value)):
39-
36+
if (
37+
isinstance(value, dict)
38+
and target_type is not None
39+
and not _is_ktable_dict(value)
40+
):
4041
from .typing import analyze_type_info
42+
4143
try:
4244
type_info = analyze_type_info(target_type)
4345
if type_info.kind == "Struct" and type_info.struct_type:
4446
# Convert dict to struct format
4547
if dataclasses.is_dataclass(type_info.struct_type):
4648
# Extract values in dataclass field order
4749
fields = dataclasses.fields(type_info.struct_type)
48-
return [encode_engine_value_with_type(value.get(f.name), f.type) for f in fields]
50+
return [
51+
encode_engine_value_with_type(value.get(f.name), f.type)
52+
for f in fields
53+
]
4954
elif is_namedtuple_type(type_info.struct_type):
5055
# Extract values in namedtuple field order
5156
field_names = getattr(type_info.struct_type, "_fields", ())
52-
return [encode_engine_value_with_type(value.get(name),
53-
type_info.struct_type.__annotations__.get(name)) for name in field_names]
57+
return [
58+
encode_engine_value_with_type(
59+
value.get(name),
60+
type_info.struct_type.__annotations__.get(name),
61+
)
62+
for name in field_names
63+
]
5464
except:
5565
# If type analysis fails, fall back to regular encoding
5666
pass
57-
67+
5868
return encode_engine_value(value)
5969

6070

@@ -75,8 +85,7 @@ def encode_engine_value(value: Any) -> Any:
7585
return [encode_engine_value(v) for v in value]
7686
if isinstance(value, dict) and _is_ktable_dict(value):
7787
return [
78-
[encode_engine_value(k)] + encode_engine_value(v)
79-
for k, v in value.items()
88+
[encode_engine_value(k)] + encode_engine_value(v) for k, v in value.items()
8089
]
8190
return value
8291

@@ -289,8 +298,10 @@ def _make_engine_struct_value_decoder(
289298
)
290299
for name in fields
291300
}
292-
elif dst_struct_type is dict or (hasattr(dst_struct_type, "__origin__") and
293-
getattr(dst_struct_type, "__origin__") is dict):
301+
elif dst_struct_type is dict or (
302+
hasattr(dst_struct_type, "__origin__")
303+
and getattr(dst_struct_type, "__origin__") is dict
304+
):
294305
parameters = {
295306
f["name"]: inspect.Parameter(
296307
name=f["name"],
@@ -330,13 +341,17 @@ def make_closure_for_value(
330341
field_value_decoder = [
331342
make_closure_for_value(name, param) for (name, param) in parameters.items()
332343
]
333-
if dst_struct_type is dict or (hasattr(dst_struct_type, "__origin__") and
334-
getattr(dst_struct_type, "__origin__") is dict):
344+
if dst_struct_type is dict or (
345+
hasattr(dst_struct_type, "__origin__")
346+
and getattr(dst_struct_type, "__origin__") is dict
347+
):
348+
335349
def dict_decoder(values: list[Any]) -> dict[str, Any]:
336350
return {
337351
name: decoder(values)
338352
for (name, _), decoder in zip(parameters.items(), field_value_decoder)
339353
}
354+
340355
return dict_decoder
341356
else:
342357
return lambda values: dst_struct_type(

python/cocoindex/tests/test_convert.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,7 @@ def test_dict_struct_encoding() -> None:
12351235
# Test encoding dict as struct - this should only happen in type-aware context
12361236
# For generic encoding, dict is passed through as-is
12371237
from cocoindex.convert import encode_engine_value
1238+
12381239
dict_value = {"name": "Alice", "age": 30, "city": "New York"}
12391240
result = encode_engine_value(dict_value)
12401241
assert result == {"name": "Alice", "age": 30, "city": "New York"} # Dict preserved
@@ -1244,13 +1245,13 @@ def test_dict_struct_decoding() -> None:
12441245
# Test decoding to dict as struct
12451246
from cocoindex.convert import _make_engine_struct_value_decoder
12461247
from typing import Dict, Any
1247-
1248+
12481249
src_fields = [
12491250
{"name": "name", "type": {"kind": "Str"}},
12501251
{"name": "age", "type": {"kind": "Int64"}},
12511252
{"name": "city", "type": {"kind": "Str"}},
12521253
]
1253-
1254+
12541255
decoder = _make_engine_struct_value_decoder([], src_fields, Dict[str, Any])
12551256
result = decoder(["Alice", 30, "New York"])
12561257
assert result == {"name": "Alice", "age": 30, "city": "New York"}
@@ -1259,13 +1260,13 @@ def test_dict_struct_decoding() -> None:
12591260
def test_dict_untyped_struct_decoding() -> None:
12601261
# Test decoding to untyped dict as struct
12611262
from cocoindex.convert import _make_engine_struct_value_decoder
1262-
1263+
12631264
src_fields = [
12641265
{"name": "name", "type": {"kind": "Str"}},
12651266
{"name": "age", "type": {"kind": "Int64"}},
12661267
{"name": "city", "type": {"kind": "Str"}},
12671268
]
1268-
1269+
12691270
decoder = _make_engine_struct_value_decoder([], src_fields, dict)
12701271
result = decoder(["Alice", 30, "New York"])
12711272
assert result == {"name": "Alice", "age": 30, "city": "New York"}
@@ -1274,19 +1275,19 @@ def test_dict_untyped_struct_decoding() -> None:
12741275
def test_dict_struct_vs_ktable() -> None:
12751276
# Test that dict-as-struct and dict-as-KTable are handled differently
12761277
from cocoindex.convert import encode_engine_value, _is_ktable_dict
1277-
1278+
12781279
# Dict as struct (simple values) - should be passed through in generic encoding
12791280
dict_struct = {"name": "Alice", "age": 30}
12801281
assert not _is_ktable_dict(dict_struct)
12811282
struct_result = encode_engine_value(dict_struct)
12821283
assert struct_result == {"name": "Alice", "age": 30} # Preserved as dict
1283-
1284+
12841285
# Dict as KTable (struct values)
12851286
@dataclass
12861287
class Person:
12871288
name: str
12881289
age: int
1289-
1290+
12901291
dict_ktable = {"p1": Person("Alice", 30), "p2": Person("Bob", 25)}
12911292
assert _is_ktable_dict(dict_ktable)
12921293
ktable_result = encode_engine_value(dict_ktable)
@@ -1297,15 +1298,15 @@ def test_dict_to_struct_conversion() -> None:
12971298
# Test dict to struct conversion with existing struct types
12981299
from cocoindex.convert import encode_engine_value_with_type
12991300
from dataclasses import dataclass
1300-
1301+
13011302
@dataclass
13021303
class Person:
13031304
name: str
13041305
age: int
13051306
city: str
1306-
1307+
13071308
dict_value = {"name": "Alice", "age": 30, "city": "New York"}
1308-
1309+
13091310
# Test encoding dict as struct
13101311
result = encode_engine_value_with_type(dict_value, Person)
13111312
assert result == ["Alice", 30, "New York"]
@@ -1316,24 +1317,24 @@ def test_struct_to_dict_conversion() -> None:
13161317
from cocoindex.convert import _make_engine_struct_value_decoder
13171318
from dataclasses import dataclass
13181319
from typing import Dict, Any
1319-
1320+
13201321
@dataclass
13211322
class Person:
13221323
name: str
13231324
age: int
13241325
city: str
1325-
1326+
13261327
src_fields = [
13271328
{"name": "name", "type": {"kind": "Str"}},
1328-
{"name": "age", "type": {"kind": "Int64"}},
1329+
{"name": "age", "type": {"kind": "Int64"}},
13291330
{"name": "city", "type": {"kind": "Str"}},
13301331
]
1331-
1332+
13321333
# Test decoding struct to dict
13331334
decoder = _make_engine_struct_value_decoder([], src_fields, Dict[str, Any])
13341335
result = decoder(["Alice", 30, "New York"])
13351336
assert result == {"name": "Alice", "age": 30, "city": "New York"}
1336-
1337+
13371338
# Test decoding struct to untyped dict
13381339
decoder = _make_engine_struct_value_decoder([], src_fields, dict)
13391340
result = decoder(["Alice", 30, "New York"])

0 commit comments

Comments
 (0)