Skip to content

Commit 5b1fa6a

Browse files
authored
Fix dict value unstructuring (#462)
* Fix dict value unstructuring * Update HISTORY
1 parent b47f446 commit 5b1fa6a

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

HISTORY.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# History
22

3+
## 23.2.3 (UNRELEASED)
4+
5+
- Fix a regression when unstructuring dictionary values typed as `Any`.
6+
([#453](https://github.com/python-attrs/cattrs/issues/453) [#462](https://github.com/python-attrs/cattrs/pull/462))
7+
- Generate unique files only in case of linecache enabled.
8+
([#445](https://github.com/python-attrs/cattrs/issues/445) [#441](https://github.com/python-attrs/cattrs/pull/461))
9+
310
## 23.2.2 (2023-11-21)
411

512
- Fix a regression when unstructuring `Any | None`.
6-
([#453](https://github.com/python-attrs/cattrs/issues/453))
7-
- Generate unique files only in case of linecache enabled. ([#445](https://github.com/python-attrs/cattrs/issues/445) [#441](https://github.com/python-attrs/cattrs/pull/461))
13+
([#453](https://github.com/python-attrs/cattrs/issues/453) [#454](https://github.com/python-attrs/cattrs/pull/454))
814

915
## 23.2.1 (2023-11-18)
1016

src/cattrs/gen/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,11 @@ def make_mapping_unstructure_fn(
743743
if kh == identity:
744744
kh = None
745745

746-
val_handler = converter._unstructure_func.dispatch(val_arg)
747-
if val_handler == identity:
748-
val_handler = None
746+
if val_arg is not Any:
747+
# TODO: Remove this once we have more consistent Any handling in place.
748+
val_handler = converter._unstructure_func.dispatch(val_arg)
749+
if val_handler == identity:
750+
val_handler = None
749751

750752
globs = {
751753
"__cattr_mapping_cl": unstructure_to or cl,

tests/test_any.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Tests for handling `typing.Any`."""
2+
from typing import Any, Dict
3+
4+
from attrs import define
5+
6+
7+
@define
8+
class A:
9+
pass
10+
11+
12+
def test_unstructuring_dict_of_any(converter):
13+
"""Dicts with Any values should use runtime dispatch for their values."""
14+
assert converter.unstructure({"a": A()}, Dict[str, Any]) == {"a": {}}

0 commit comments

Comments
 (0)