Skip to content

Commit 2c5cbd1

Browse files
committed
Take make_dict_structure_fn.prefer_attrib_converters from converter
1 parent b3c6ba7 commit 2c5cbd1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

HISTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
1717
([#473](https://github.com/python-attrs/cattrs/pull/473))
1818
- **Minor change**: Heterogeneous tuples are now unstructured into tuples instead of lists by default; this is significantly faster and widely supported by serialization libraries.
1919
([#486](https://github.com/python-attrs/cattrs/pull/486))
20+
- **Minor change**: {py:func}`cattrs.gen.make_dict_structure_fn` will use the value for the `prefer_attrib_converters` parameter from the given converter by default now.
21+
If you're using this function directly, the old behavior can be restored by passing in the desired values explicitly.
2022
- Introduce {meth}`BaseConverter.get_structure_hook` and {meth}`BaseConverter.get_unstructure_hook` methods.
2123
([#432](https://github.com/python-attrs/cattrs/issues/432) [#472](https://github.com/python-attrs/cattrs/pull/472))
2224
- {meth}`BaseConverter.register_structure_hook`, {meth}`BaseConverter.register_unstructure_hook`,

src/cattrs/gen/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ def make_dict_structure_fn(
258258
converter: BaseConverter,
259259
_cattrs_forbid_extra_keys: bool | Literal["from_converter"] = "from_converter",
260260
_cattrs_use_linecache: bool = True,
261-
_cattrs_prefer_attrib_converters: bool = False,
261+
_cattrs_prefer_attrib_converters: (
262+
bool | Literal["from_converter"]
263+
) = "from_converter",
262264
_cattrs_detailed_validation: bool | Literal["from_converter"] = "from_converter",
263265
_cattrs_use_alias: bool = False,
264266
_cattrs_include_init_false: bool = False,
@@ -289,6 +291,9 @@ def make_dict_structure_fn(
289291
.. versionchanged:: 23.2.0
290292
The `_cattrs_forbid_extra_keys` and `_cattrs_detailed_validation` parameters
291293
take their values from the given converter by default.
294+
.. versionchanged:: 24.1.0
295+
The `_cattrs_prefer_attrib_converters` parameter takes its value from the given
296+
converter by default.
292297
"""
293298

294299
mapping = {}
@@ -344,6 +349,8 @@ def make_dict_structure_fn(
344349
_cattrs_forbid_extra_keys = getattr(converter, "forbid_extra_keys", False)
345350
if _cattrs_detailed_validation == "from_converter":
346351
_cattrs_detailed_validation = converter.detailed_validation
352+
if _cattrs_prefer_attrib_converters == "from_converter":
353+
_cattrs_prefer_attrib_converters = converter._prefer_attrib_converters
347354

348355
if _cattrs_forbid_extra_keys:
349356
globs["__c_a"] = allowed_fields

tests/test_gen_dict.py

+20
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,26 @@ class A:
633633
converter.structure({"a": "a"}, A)
634634

635635

636+
@given(prefer=...)
637+
def test_prefer_converters_from_converter(prefer: bool):
638+
"""
639+
`prefer_attrs_converters` is taken from the converter by default.
640+
"""
641+
642+
@define
643+
class A:
644+
a: int = field(converter=lambda x: x + 1)
645+
646+
converter = BaseConverter(prefer_attrib_converters=prefer)
647+
converter.register_structure_hook(int, lambda x, _: x + 1)
648+
converter.register_structure_hook(A, make_dict_structure_fn(A, converter))
649+
650+
if prefer:
651+
assert converter.structure({"a": 1}, A).a == 2
652+
else:
653+
assert converter.structure({"a": 1}, A).a == 3
654+
655+
636656
def test_fields_exception():
637657
"""fields() raises on a non-attrs, non-dataclass class."""
638658
with pytest.raises(Exception): # noqa: B017

0 commit comments

Comments
 (0)