Skip to content

Commit 0b9cbce

Browse files
authored
pass ignore_unknown_fields to nested structs (#153)
1 parent ff4b611 commit 0b9cbce

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

betterproto2/src/betterproto2/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,11 @@ def _value_to_dict(
606606
return value, not bool(value)
607607

608608

609-
def _value_from_dict(value: Any, meta: FieldMetadata, field_type: type) -> Any:
609+
def _value_from_dict(value: Any, meta: FieldMetadata, field_type: type, ignore_unknown_fields: bool) -> Any:
610610
if meta.proto_type == TYPE_MESSAGE:
611611
msg_cls = meta.unwrap() if meta.unwrap else field_type
612612

613-
msg = msg_cls.from_dict(value)
613+
msg = msg_cls.from_dict(value, ignore_unknown_fields=ignore_unknown_fields)
614614

615615
if meta.unwrap:
616616
return msg.to_wrapped()
@@ -1152,9 +1152,9 @@ def _from_dict_init(cls, mapping: Mapping[str, Any] | Any, *, ignore_unknown_fie
11521152

11531153
if meta.proto_type == TYPE_MESSAGE:
11541154
if meta.repeated:
1155-
value = [_value_from_dict(item, meta, field_cls) for item in value]
1155+
value = [_value_from_dict(item, meta, field_cls, ignore_unknown_fields) for item in value]
11561156
else:
1157-
value = _value_from_dict(value, meta, field_cls)
1157+
value = _value_from_dict(value, meta, field_cls, ignore_unknown_fields)
11581158

11591159
elif meta.proto_type == TYPE_MAP:
11601160
assert meta.map_meta
@@ -1163,15 +1163,17 @@ def _from_dict_init(cls, mapping: Mapping[str, Any] | Any, *, ignore_unknown_fie
11631163
value_cls = cls._betterproto.cls_by_field[f"{field_name}.value"]
11641164

11651165
value = {
1166-
_value_from_dict(k, meta.map_meta[0], type(None)): _value_from_dict(v, meta.map_meta[1], value_cls)
1166+
_value_from_dict(k, meta.map_meta[0], type(None), ignore_unknown_fields): _value_from_dict(
1167+
v, meta.map_meta[1], value_cls, ignore_unknown_fields
1168+
)
11671169
for k, v in value.items()
11681170
}
11691171

11701172
elif meta.repeated:
1171-
value = [_value_from_dict(item, meta, field_cls) for item in value]
1173+
value = [_value_from_dict(item, meta, field_cls, ignore_unknown_fields) for item in value]
11721174

11731175
else:
1174-
value = _value_from_dict(value, meta, field_cls)
1176+
value = _value_from_dict(value, meta, field_cls, ignore_unknown_fields)
11751177

11761178
init_kwargs[field_name] = value
11771179
return init_kwargs

betterproto2/tests/test_nested.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def test_nested_from_dict():
2+
"""
3+
Make sure that from_dict() arguments are passed recursively
4+
"""
5+
from tests.outputs.nested.nested import Test
6+
7+
data = {
8+
"nested": {"count": 1},
9+
"sibling": {"foo": 2},
10+
}
11+
Test.from_dict(data)
12+
13+
data["bar"] = 3
14+
Test.from_dict(data, ignore_unknown_fields=True)
15+
16+
data["nested"]["bar"] = 3
17+
Test.from_dict(data, ignore_unknown_fields=True)
18+
19+
data["sibling"]["bar"] = 4
20+
Test.from_dict(data, ignore_unknown_fields=True)

0 commit comments

Comments
 (0)