TimeDelta misleading 'precision' argument & v4 breaking change
Author: OJFordCreated Apr 21, 2025Updated Dec 25, 2025
Labelsfeedback welcome
The docstring is correct:
:param precision: The time unit used for (de)serialization. Must be one of 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds' or 'microseconds'.
it's the units (de)serialised to; it doesn't effect precision.
That is, 123 microseconds with the default "seconds" precision will serialise to 0.000123, not 0.
This seems to be a breaking change in v4.0.0, perhaps somehow related to the date/time/isoformat change, I think in 0c901108945573c825e33960707426c80e19290e as previously it would return a timedelta(microseconds=123) in the _serialize method, but now returns a float via utils.timedelta_to_microseconds divided by the 'precision''s mapping value, i.e. 1000000 for seconds.
Source: marshmallow-code/marshmallow