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

Fix dict value unstructuring #462

Merged
merged 3 commits into from
Nov 27, 2023
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
10 changes: 8 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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": {}}