Skip to content

Commit 1efa2f7

Browse files
committed
(BaseConverter) Do not untructure attrs.NOTHING instead of to 1 (int)
1 parent f5d0594 commit 1efa2f7

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/cattrs/converters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pathlib import Path
1212
from typing import Any, Optional, Tuple, TypeVar, overload
1313

14+
from attr._make import _Nothing
1415
from attrs import Attribute, resolve_types
1516
from attrs import has as attrs_has
1617
from typing_extensions import Self
@@ -223,7 +224,7 @@ def __init__(
223224
unstructure_fallback_factory, self
224225
)
225226
self._unstructure_func.register_cls_list(
226-
[(bytes, identity), (str, identity), (Path, str)]
227+
[(bytes, identity), (str, identity), (Path, str), (_Nothing, identity)]
227228
)
228229
self._unstructure_func.register_func_list(
229230
[

tests/test_enums.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for enums."""
22

3+
from attrs import NOTHING
34
from hypothesis import given
45
from hypothesis.strategies import data, sampled_from
56
from pytest import raises
@@ -29,3 +30,17 @@ def test_enum_failure(enum):
2930
converter.structure("", type)
3031

3132
assert exc_info.value.args[0] == f" not in literal {type!r}"
33+
34+
35+
def test_nothing_from_attrs():
36+
"""Test that `NOTHING` from attrs does not unstructure to `1` (int), but remains `NOTHING`."""
37+
converter = BaseConverter()
38+
39+
assert (
40+
converter.unstructure(NOTHING) != 1
41+
), "NOTHING should not unstructure to 1 (int)."
42+
assert not isinstance(converter.unstructure(NOTHING), int)
43+
assert not converter.unstructure(
44+
NOTHING
45+
) # bool(NOTHING) should be False although `bool(1)` is True
46+
assert converter.unstructure(NOTHING) is NOTHING

0 commit comments

Comments
 (0)