Skip to content

Commit 281f324

Browse files
authored
Fix regression (#454)
* Fix regression * Run CI on every PR
1 parent c098cd8 commit 281f324

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ on:
55
push:
66
branches: ["main"]
77
pull_request:
8-
branches: ["main"]
98
workflow_dispatch:
109

1110
jobs:
@@ -47,7 +46,7 @@ jobs:
4746

4847
steps:
4948
- uses: "actions/checkout@v3"
50-
49+
5150
- uses: "actions/setup-python@v4"
5251
with:
5352
cache: "pip"
@@ -76,7 +75,7 @@ jobs:
7675
with:
7776
name: "html-report"
7877
path: "htmlcov"
79-
78+
8079
- name: "Make badge"
8180
if: github.ref == 'refs/heads/main'
8281
uses: "schneegans/[email protected]"

HISTORY.md

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

3+
## 23.2.2 (UNRELEASED)
4+
5+
- Fix a regression when unstructuring `Any | None`.
6+
([#453](https://github.com/python-attrs/cattrs/issues/453))
7+
38
## 23.2.1 (2023-11-18)
49

510
- Fix unnecessary `typing_extensions` import on Python 3.11.

src/cattrs/converters.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,12 @@ def gen_unstructure_optional(self, cl: Type[T]) -> Callable[[T], Any]:
954954
"""Generate an unstructuring hook for optional types."""
955955
union_params = cl.__args__
956956
other = union_params[0] if union_params[1] is NoneType else union_params[1]
957-
handler = self._unstructure_func.dispatch(other)
957+
958+
# TODO: Remove this special case when we make unstructuring Any consistent.
959+
if other is Any:
960+
handler = self.unstructure
961+
else:
962+
handler = self._unstructure_func.dispatch(other)
958963

959964
def unstructure_optional(val, _handler=handler):
960965
return None if val is None else _handler(val)

tests/test_optionals.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import NewType, Optional
1+
from typing import Any, NewType, Optional
22

33
import pytest
44
from attrs import define
55

6+
from cattrs import Converter
7+
68
from ._compat import is_py310_plus
79

810

@@ -39,3 +41,13 @@ class ModelWithFoo:
3941
"total_foo": "bar",
4042
"maybe_foo": "is it a bar?",
4143
}
44+
45+
46+
def test_optional_any(converter: Converter):
47+
"""Unstructuring Any|None is equivalent to unstructuring as v.__class__."""
48+
49+
@define
50+
class A:
51+
pass
52+
53+
assert converter.unstructure(A(), Optional[Any]) == {}

0 commit comments

Comments
 (0)