Skip to content

Commit 23b0bcc

Browse files
committed
style: apply pre-commit formatting to modified files
1 parent 20592e6 commit 23b0bcc

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

python/cocoindex/convert.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def load_engine_object(expected_type: Any, v: Any) -> Any:
784784
dc_init_kwargs: dict[str, Any] = {}
785785
field_types = {f.name: f.type for f in dataclasses.fields(struct_type)}
786786
dataclass_fields = {f.name: f for f in dataclasses.fields(struct_type)}
787-
787+
788788
for name, f_type in field_types.items():
789789
if name in v:
790790
dc_init_kwargs[name] = load_engine_object(f_type, v[name])
@@ -800,7 +800,9 @@ def load_engine_object(expected_type: Any, v: Any) -> Any:
800800
else:
801801
# No explicit default, try to get auto-default
802802
type_info = analyze_type_info(f_type)
803-
auto_default, is_supported = _get_auto_default_for_type(type_info)
803+
auto_default, is_supported = _get_auto_default_for_type(
804+
type_info
805+
)
804806
if is_supported:
805807
dc_init_kwargs[name] = auto_default
806808
# If not supported, skip the field (let dataclass constructor handle the error)
@@ -813,7 +815,7 @@ def load_engine_object(expected_type: Any, v: Any) -> Any:
813815
field_names = list(getattr(struct_type, "_fields", ()))
814816
field_defaults = getattr(struct_type, "_field_defaults", {})
815817
nt_init_kwargs: dict[str, Any] = {}
816-
818+
817819
for name in field_names:
818820
f_type = annotations.get(name, Any)
819821
if name in v:
@@ -826,7 +828,9 @@ def load_engine_object(expected_type: Any, v: Any) -> Any:
826828
else:
827829
# No explicit default, try to get auto-default
828830
type_info = analyze_type_info(f_type)
829-
auto_default, is_supported = _get_auto_default_for_type(type_info)
831+
auto_default, is_supported = _get_auto_default_for_type(
832+
type_info
833+
)
830834
if is_supported:
831835
nt_init_kwargs[name] = auto_default
832836
# If not supported, skip the field (let NamedTuple constructor handle the error)
@@ -844,23 +848,30 @@ def load_engine_object(expected_type: Any, v: Any) -> Any:
844848
field_types = {
845849
name: field.annotation for name, field in model_fields.items()
846850
}
847-
851+
848852
for name, f_type in field_types.items():
849853
if name in v:
850854
pydantic_init_kwargs[name] = load_engine_object(f_type, v[name])
851855
else:
852856
# Field is missing from input, check if it has a default or can use auto-default
853857
field = model_fields[name]
854-
if hasattr(field, "default") and field.default is not ...: # ... is Pydantic's sentinel for no default
858+
if (
859+
hasattr(field, "default") and field.default is not ...
860+
): # ... is Pydantic's sentinel for no default
855861
# Field has an explicit default value
856862
pydantic_init_kwargs[name] = field.default
857-
elif hasattr(field, "default_factory") and field.default_factory is not None:
863+
elif (
864+
hasattr(field, "default_factory")
865+
and field.default_factory is not None
866+
):
858867
# Field has a default factory
859868
pydantic_init_kwargs[name] = field.default_factory()
860869
else:
861870
# No explicit default, try to get auto-default
862871
type_info = analyze_type_info(f_type)
863-
auto_default, is_supported = _get_auto_default_for_type(type_info)
872+
auto_default, is_supported = _get_auto_default_for_type(
873+
type_info
874+
)
864875
if is_supported:
865876
pydantic_init_kwargs[name] = auto_default
866877
# If not supported, skip the field (let Pydantic constructor handle the error)

python/cocoindex/tests/test_load_convert.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,61 +120,61 @@ def test_namedtuple_roundtrip_via_dump_load() -> None:
120120

121121
def test_dataclass_missing_fields_with_auto_defaults() -> None:
122122
"""Test that missing fields are automatically assigned safe default values."""
123-
123+
124124
@dataclasses.dataclass
125125
class TestClass:
126126
required_field: str
127127
optional_field: str | None # Should get None
128-
list_field: list[str] # Should get []
128+
list_field: list[str] # Should get []
129129
dict_field: dict[str, int] # Should get {}
130130
explicit_default: str = "default" # Should use explicit default
131-
131+
132132
# Input missing optional_field, list_field, dict_field (but has explicit_default via class definition)
133133
input_data = {"required_field": "test_value"}
134-
134+
135135
loaded = load_engine_object(TestClass, input_data)
136-
136+
137137
assert isinstance(loaded, TestClass)
138138
assert loaded.required_field == "test_value"
139-
assert loaded.optional_field is None # Auto-default for Optional
140-
assert loaded.list_field == [] # Auto-default for list
141-
assert loaded.dict_field == {} # Auto-default for dict
139+
assert loaded.optional_field is None # Auto-default for Optional
140+
assert loaded.list_field == [] # Auto-default for list
141+
assert loaded.dict_field == {} # Auto-default for dict
142142
assert loaded.explicit_default == "default" # Explicit default from class
143143

144144

145145
def test_namedtuple_missing_fields_with_auto_defaults() -> None:
146146
"""Test that missing fields in NamedTuple are automatically assigned safe default values."""
147147
from typing import NamedTuple
148-
148+
149149
class TestTuple(NamedTuple):
150150
required_field: str
151151
optional_field: str | None # Should get None
152-
list_field: list[str] # Should get []
152+
list_field: list[str] # Should get []
153153
dict_field: dict[str, int] # Should get {}
154-
154+
155155
# Input missing optional_field, list_field, dict_field
156156
input_data = {"required_field": "test_value"}
157-
157+
158158
loaded = load_engine_object(TestTuple, input_data)
159-
159+
160160
assert isinstance(loaded, TestTuple)
161161
assert loaded.required_field == "test_value"
162-
assert loaded.optional_field is None # Auto-default for Optional
163-
assert loaded.list_field == [] # Auto-default for list
164-
assert loaded.dict_field == {} # Auto-default for dict
162+
assert loaded.optional_field is None # Auto-default for Optional
163+
assert loaded.list_field == [] # Auto-default for list
164+
assert loaded.dict_field == {} # Auto-default for dict
165165

166166

167167
def test_dataclass_unsupported_type_still_fails() -> None:
168168
"""Test that fields with unsupported types still cause errors when missing."""
169-
169+
170170
@dataclasses.dataclass
171171
class TestClass:
172172
required_field1: str
173173
required_field2: int # No auto-default for int
174-
174+
175175
# Input missing required_field2 which has no safe auto-default
176176
input_data = {"required_field1": "test_value"}
177-
177+
178178
# Should still raise an error because int has no safe auto-default
179179
try:
180180
load_engine_object(TestClass, input_data)

0 commit comments

Comments
 (0)