Skip to content

Commit 0f32524

Browse files
committed
Use renamed fields in convert with from_attributes=True
If we need to support converting to the original attribute names, we can add another config option later. Using the renamed names provides better uniformity across the library.
1 parent 0761a06 commit 0f32524

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎msgspec/_core.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18363,7 +18363,7 @@ convert_object_to_struct(
1836318363
) {
1836418364
StructMetaObject *struct_type = info->class;
1836518365

18366-
Py_ssize_t nfields = PyTuple_GET_SIZE(struct_type->struct_fields);
18366+
Py_ssize_t nfields = PyTuple_GET_SIZE(struct_type->struct_encode_fields);
1836718367
Py_ssize_t ndefaults = PyTuple_GET_SIZE(struct_type->struct_defaults);
1836818368

1836918369
if (struct_type->struct_tag_value != NULL && !tag_already_read) {
@@ -18391,7 +18391,7 @@ convert_object_to_struct(
1839118391
bool should_untrack = is_gc;
1839218392

1839318393
for (Py_ssize_t i = 0; i < nfields; i++) {
18394-
PyObject *field = PyTuple_GET_ITEM(struct_type->struct_fields, i);
18394+
PyObject *field = PyTuple_GET_ITEM(struct_type->struct_encode_fields, i);
1839518395
PyObject *attr = getter(obj, field);
1839618396
PyObject *val;
1839718397
if (attr == NULL) {

‎tests/test_convert.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ class Example:
10941094
with pytest.raises(ValidationError, match=r"Expected `str` - at `key` in `\$`"):
10951095
convert({"a": 1, 1: 2}, Example)
10961096

1097-
def test_attributes_option_disables_attribute_coercion(self):
1097+
def test_from_attributes_option_disables_attribute_coercion(self):
10981098
class Bad:
10991099
def __init__(self):
11001100
self.x = 1
@@ -1236,7 +1236,7 @@ class Example:
12361236
with pytest.raises(ValidationError, match=r"Expected `str` - at `key` in `\$`"):
12371237
convert({"a": 1, 1: 2}, Example)
12381238

1239-
def test_attributes_option_disables_attribute_coercion(self):
1239+
def test_from_attributes_option_disables_attribute_coercion(self):
12401240
class Bad:
12411241
def __init__(self):
12421242
self.x = 1
@@ -1381,7 +1381,7 @@ def test_dict_to_struct_errors(self):
13811381
with pytest.raises(ValidationError, match=r"Expected `str` - at `key` in `\$`"):
13821382
convert({"age": 1, 1: 2}, self.Account)
13831383

1384-
def test_attributes_option_disables_attribute_coercion(self):
1384+
def test_from_attributes_option_disables_attribute_coercion(self):
13851385
class Bad:
13861386
def __init__(self):
13871387
self.x = 1
@@ -1396,6 +1396,13 @@ class Ex(Struct):
13961396

13971397
assert convert(msg, Ex, from_attributes=True) == Ex(1)
13981398

1399+
def test_from_attributes_option_uses_renamed_fields(self):
1400+
class Ex(Struct, rename="camel"):
1401+
field_one: int
1402+
1403+
msg = GetAttrObj(fieldOne=2)
1404+
assert convert(msg, Ex, from_attributes=True) == Ex(2)
1405+
13991406
@pytest.mark.parametrize("forbid_unknown_fields", [False, True])
14001407
@mapcls_and_from_attributes
14011408
def test_struct_extra_fields(self, forbid_unknown_fields, mapcls, from_attributes):

0 commit comments

Comments
 (0)