Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take make_dict_structure_fn.prefer_attrib_converters from converter #528

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
([#473](https://github.com/python-attrs/cattrs/pull/473))
- **Minor change**: Heterogeneous tuples are now unstructured into tuples instead of lists by default; this is significantly faster and widely supported by serialization libraries.
([#486](https://github.com/python-attrs/cattrs/pull/486))
- **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.
If you're using this function directly, the old behavior can be restored by passing in the desired values explicitly.
- Introduce {meth}`BaseConverter.get_structure_hook` and {meth}`BaseConverter.get_unstructure_hook` methods.
([#432](https://github.com/python-attrs/cattrs/issues/432) [#472](https://github.com/python-attrs/cattrs/pull/472))
- {meth}`BaseConverter.register_structure_hook`, {meth}`BaseConverter.register_unstructure_hook`,
Expand Down
9 changes: 8 additions & 1 deletion src/cattrs/gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ def make_dict_structure_fn(
converter: BaseConverter,
_cattrs_forbid_extra_keys: bool | Literal["from_converter"] = "from_converter",
_cattrs_use_linecache: bool = True,
_cattrs_prefer_attrib_converters: bool = False,
_cattrs_prefer_attrib_converters: (
bool | Literal["from_converter"]
) = "from_converter",
_cattrs_detailed_validation: bool | Literal["from_converter"] = "from_converter",
_cattrs_use_alias: bool = False,
_cattrs_include_init_false: bool = False,
Expand Down Expand Up @@ -289,6 +291,9 @@ def make_dict_structure_fn(
.. versionchanged:: 23.2.0
The `_cattrs_forbid_extra_keys` and `_cattrs_detailed_validation` parameters
take their values from the given converter by default.
.. versionchanged:: 24.1.0
The `_cattrs_prefer_attrib_converters` parameter takes its value from the given
converter by default.
"""

mapping = {}
Expand Down Expand Up @@ -344,6 +349,8 @@ def make_dict_structure_fn(
_cattrs_forbid_extra_keys = getattr(converter, "forbid_extra_keys", False)
if _cattrs_detailed_validation == "from_converter":
_cattrs_detailed_validation = converter.detailed_validation
if _cattrs_prefer_attrib_converters == "from_converter":
_cattrs_prefer_attrib_converters = converter._prefer_attrib_converters

if _cattrs_forbid_extra_keys:
globs["__c_a"] = allowed_fields
Expand Down
20 changes: 20 additions & 0 deletions tests/test_gen_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,26 @@ class A:
converter.structure({"a": "a"}, A)


@given(prefer=...)
def test_prefer_converters_from_converter(prefer: bool):
"""
`prefer_attrs_converters` is taken from the converter by default.
"""

@define
class A:
a: int = field(converter=lambda x: x + 1)

converter = BaseConverter(prefer_attrib_converters=prefer)
converter.register_structure_hook(int, lambda x, _: x + 1)
converter.register_structure_hook(A, make_dict_structure_fn(A, converter))

if prefer:
assert converter.structure({"a": 1}, A).a == 2
else:
assert converter.structure({"a": 1}, A).a == 3


def test_fields_exception():
"""fields() raises on a non-attrs, non-dataclass class."""
with pytest.raises(Exception): # noqa: B017
Expand Down
Loading