cattrs: naive datetimes on the wire

Merged into cattrs. The msgpack and cbor2 converters unstructured a datetime with datetime.timestamp, which reads a naive value as local time. The number written to the wire then depended on the timezone of the machine doing the unstructuring, so the same datetime round-tripped to a different instant on a different host. I reported the issue and sent the fix.

September 3, 2026

What was wrong

Both converters serialize a datetime through datetime.timestamp. On a naive datetime that method interprets the value as local time, so the float written to the wire moved with the host timezone.

The same 12:30 datetime dumped to one number under UTC, another under Asia/Tokyo, and a third under America/Toronto, and each loaded back to a different instant. An aware datetime was the control and produced the same bytes in all three.

The change

The structure hooks already declare the wire contract, datetime.fromtimestamp with timezone.utc, so the unstructure hooks now pin a naive datetime to UTC before converting, and the two ends agree. The date hook sitting right below the datetime hook in msgpack.py already did exactly this, so the change brings the datetime hook in line with it.

The shared helper lives next to validate_datetime in preconf, since both converters need it and both already import from there. A naive input still comes back aware, because a bare float has nowhere to record awareness. What changes is that the value survives and the payload no longer depends on the host.

Tests

  • The preconf strategy never generated naive datetimes, so the gap was untested until now.
  • The regression test sets TZ explicitly rather than trusting the host, because on a UTC machine, including the CI runners, a naive datetime assertion passes whether or not the bug is present. It restores the previous TZ on the way out and skips where tzset is unavailable.
  • The two new tests fail on unpatched source, off by the nine hour JST offset, and pass with the change. The full suite is 992 passed and 15 xfailed, with ruff clean.

python-attrs/cattrs #775, merged