Skip to content

Commit 01789cf

Browse files
committed
Tweak only for dates
1 parent 51595e1 commit 01789cf

5 files changed

Lines changed: 29 additions & 32 deletions

File tree

HISTORY.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
2323
([#699](https://github.com/python-attrs/cattrs/issues/699))
2424
- _cattrs_ now tracks performance using [codspeed](https://codspeed.io/python-attrs/cattrs).
2525
([#703](https://github.com/python-attrs/cattrs/pull/703))
26-
- The {mod}`tomlkit <cattrs.preconf.tomlkit>` preconf converter now properly handles `date` and `datetime` objects when structuring.
26+
- The {mod}`tomlkit <cattrs.preconf.tomlkit>` preconf converter now properly handles native `date` objects when structuring.
27+
([#707](https://github.com/python-attrs/cattrs/issues/707) [#708](https://github.com/python-attrs/cattrs/pull/708))
28+
- The {mod}`tomlkit <cattrs.preconf.tomlkit>` preconf converter now passes date objects directly to _tomlkit_ for unstructuring.
2729
([#707](https://github.com/python-attrs/cattrs/issues/707) [#708](https://github.com/python-attrs/cattrs/pull/708))
2830

2931

docs/preconf.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Optional install targets should match the name of the {mod}`cattrs.preconf` modu
5555
# Using pip
5656
$ pip install cattrs[ujson]
5757

58+
# Usinv uv
59+
$ uv add cattrs[msgspec]
60+
5861
# Using pdm
5962
$ pdm add cattrs[orjson]
6063

@@ -203,5 +206,5 @@ Found at {mod}`cattrs.preconf.tomlkit`.
203206
Bytes are serialized as base 85 strings. Sets are serialized as lists, and deserialized back into sets.
204207
Tuples are serialized as lists, and deserialized back into tuples.
205208
_tomlkit_ only supports mappings with string keys so mappings will have their keys stringified before serialization, and destringified during deserialization.
206-
`date` s are serialized as ISO 8601 strings. `datetime` s are passed through to be unstructured by _tomlkit_ itself.
209+
[`date`](https://docs.python.org/3/library/datetime.html#datetime.date) and [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime) objects are passed through to be unstructured by _tomlkit_ itself.
207210

src/cattrs/preconf/tomlkit.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from .._compat import is_mapping, is_subclass
1414
from ..converters import BaseConverter, Converter
15+
from ..fns import identity
1516
from ..strategies import configure_union_passthrough
1617
from . import validate_datetime, wrap
1718

@@ -37,6 +38,9 @@ def configure_converter(converter: BaseConverter):
3738
* sets are serialized as lists
3839
* tuples are serializas as lists
3940
* mapping keys are coerced into strings when unstructuring
41+
42+
.. versionchanged:: NEXT
43+
date objects are now passed through to tomlkit without unstructuring.
4044
"""
4145
converter.register_structure_hook(bytes, lambda v, _: b85decode(v))
4246
converter.register_unstructure_hook(
@@ -67,11 +71,9 @@ def key_handler(k: bytes):
6771

6872
# datetime inherits from date, so identity unstructure hook used
6973
# here to prevent the date unstructure hook running.
70-
converter.register_unstructure_hook(datetime, lambda v: v)
71-
converter.register_structure_hook(
72-
datetime, lambda v, _: v if isinstance(v, datetime) else validate_datetime(v, _)
73-
)
74-
converter.register_unstructure_hook(date, lambda v: v.isoformat())
74+
converter.register_unstructure_hook(datetime, identity)
75+
converter.register_structure_hook(datetime, validate_datetime)
76+
converter.register_unstructure_hook(date, identity)
7577
converter.register_structure_hook(
7678
date, lambda v, _: v if isinstance(v, date) else date.fromisoformat(v)
7779
)

tests/preconf/test_tomlkit.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/test_preconf.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,21 @@ def test_tomlkit_unions(union_and_val: tuple, detailed_validation: bool):
767767
assert converter.structure(val, type) == val
768768

769769

770+
def test_tomlkit_dates():
771+
"""Native date objects from tomlkit are properly handled."""
772+
773+
@define
774+
class Event:
775+
event_date: date
776+
777+
toml_input = """
778+
event_date = 2025-12-16
779+
"""
780+
converter = tomlkit_make_converter()
781+
structured = converter.loads(toml_input, Event)
782+
assert structured.event_date == date(2025, 12, 16)
783+
784+
770785
@given(everythings(min_int=-9223372036854775808, max_int=18446744073709551615))
771786
def test_cbor2(everything: Everything):
772787
from cbor2 import dumps as cbor2_dumps

0 commit comments

Comments
 (0)