Skip to content

Commit

Permalink
Fix dict value unstructuring (#462)
Browse files Browse the repository at this point in the history
* Fix dict value unstructuring

* Update HISTORY
  • Loading branch information
Tinche committed Nov 27, 2023
1 parent b47f446 commit 5b1fa6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
10 changes: 8 additions & 2 deletions HISTORY.md
@@ -1,10 +1,16 @@
# History

## 23.2.3 (UNRELEASED)

- Fix a regression when unstructuring dictionary values typed as `Any`.
([#453](https://github.com/python-attrs/cattrs/issues/453) [#462](https://github.com/python-attrs/cattrs/pull/462))
- 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))

## 23.2.2 (2023-11-21)

- Fix a regression when unstructuring `Any | None`.
([#453](https://github.com/python-attrs/cattrs/issues/453))
- 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))
([#453](https://github.com/python-attrs/cattrs/issues/453) [#454](https://github.com/python-attrs/cattrs/pull/454))

## 23.2.1 (2023-11-18)

Expand Down
8 changes: 5 additions & 3 deletions src/cattrs/gen/__init__.py
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions 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": {}}

0 comments on commit 5b1fa6a

Please sign in to comment.