From e224f378d966faa72b6bd42f78dff4166a817ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20Tvrtkovi=C4=87?= Date: Thu, 23 Nov 2023 13:20:04 +0100 Subject: [PATCH 1/2] Fix dict value unstructuring --- HISTORY.md | 5 +++++ src/cattrs/gen/__init__.py | 8 +++++--- tests/test_any.py | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/test_any.py diff --git a/HISTORY.md b/HISTORY.md index 9c20081c..68b3af9e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,10 @@ # History +## 23.2.3 (UNRELEASED) + +- Fix a regression when unstructuring dictionary values typed as `Any`. + ([#453](https://github.com/python-attrs/cattrs/issues/453)) + ## 23.2.2 (2023-11-21) - Fix a regression when unstructuring `Any | None`. diff --git a/src/cattrs/gen/__init__.py b/src/cattrs/gen/__init__.py index ec7d01b1..6f73dda2 100644 --- a/src/cattrs/gen/__init__.py +++ b/src/cattrs/gen/__init__.py @@ -743,9 +743,11 @@ def make_mapping_unstructure_fn( if kh == identity: kh = None - val_handler = converter._unstructure_func.dispatch(val_arg) - if val_handler == identity: - val_handler = None + if val_arg is not Any: + # TODO: Remove this once we have more consistent Any handling in place. + val_handler = converter._unstructure_func.dispatch(val_arg) + if val_handler == identity: + val_handler = None globs = { "__cattr_mapping_cl": unstructure_to or cl, diff --git a/tests/test_any.py b/tests/test_any.py new file mode 100644 index 00000000..94fa0bd9 --- /dev/null +++ b/tests/test_any.py @@ -0,0 +1,14 @@ +"""Tests for handling `typing.Any`.""" +from typing import Any, Dict + +from attrs import define + + +@define +class A: + pass + + +def test_unstructuring_dict_of_any(converter): + """Dicts with Any values should use runtime dispatch for their values.""" + assert converter.unstructure({"a": A()}, Dict[str, Any]) == {"a": {}} From c3ac7840b656855cc992e64e0797d49498012045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20Tvrtkovi=C4=87?= Date: Thu, 23 Nov 2023 13:44:22 +0100 Subject: [PATCH 2/2] Update HISTORY --- HISTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 68b3af9e..78bb11dd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,12 +3,12 @@ ## 23.2.3 (UNRELEASED) - Fix a regression when unstructuring dictionary values typed as `Any`. - ([#453](https://github.com/python-attrs/cattrs/issues/453)) + ([#453](https://github.com/python-attrs/cattrs/issues/453) [#462](https://github.com/python-attrs/cattrs/pull/462)) ## 23.2.2 (2023-11-21) - Fix a regression when unstructuring `Any | None`. - ([#453](https://github.com/python-attrs/cattrs/issues/453)) + ([#453](https://github.com/python-attrs/cattrs/issues/453) [#454](https://github.com/python-attrs/cattrs/pull/454)) ## 23.2.1 (2023-11-18)