v4 regression: Date field load() silently accepts datetime instances (time component kept)
Problem
fields.Date() silently accepts datetime instances on load and returns them unchanged, time component intact:
>>> from marshmallow import Schema, fields
>>> import datetime
>>> class S(Schema):
... d = fields.Date()
>>> S().load({"d": datetime.datetime(2014, 8, 21, 12, 30)})
{'d': datetime.datetime(2014, 8, 21, 12, 30)} # expected: ValidationError
Context
- marshmallow 3.26 rejects this input (
ValidationError: Not a valid date.), so this is a v4 behavior regression, not a long-standing quirk. - Root cause:
_TemporalField._deserializereturns anyisinstance(value, internal_type)value unchanged, anddatetime.datetimesubclassesdatetime.date, so the fast path swallows it. - The dump direction is unaffected —
dumpcorrectly rendersdatetime → '2014-08-21'— which makes the load-side passthrough inconsistent within the same field. DateTimeandTimefields don't have the inverse problem (dateis not a subclass ofdatetime/time).
Proposal
Exclude the datetime-under-date case from the fast path so Date raises ValidationError again, while date instances keep passing through unchanged. Implementated in #3065 — happy to adjust or drop the PR if this is considered intended v4 behavior (e.g. if accepting datetime-shaped input is now deliberate).
AI Disclosure
Issue filed with the assistance of an AI coding agent (ZCode, GLM-based) — used to verify the 3.26 vs 4.x behavior difference and isolate the isinstance fast path. I reviewed the analysis.
Datetime being a Date subclass, it is normal (although arguably surprising) that it is accepted if Date is accepted. Trying to filter which subclasses are accepted or not is a smell. The point is it behaves as a Date object, so it is a (specific kind of) Date.
I realize it serializes with hour, minutes, etc. which won't load back. But what code loads a date as an object, then dumps it as json, then loads it from json?
I'm tempted to consider that a code passing a Datetime instance here is responsible for the issue.
Thanks for the detailed reasoning — agreed that explicitly filtering subclasses is a smell, and I won't push for the filter.
The one nuance that motivated the report: this is a 3.26 -> v4 behavior change (3.26 raised ValidationError for the same input and a user hit it in the wild), so if the passthrough is intended, a changelog/docs note would help people who relied on the strict behavior.
As promised when opening this issue, I'll close #3065 now — the branch is still around if you ever decide the strict reading wins.
Hi @lafrech,
Makes sense regarding the inheritance model.
Given that 3.26 raised a ValidationError here, would you be open to a PR adding a short note to the fields.Date docstring/changelog to clarify that datetime objects are accepted as-is in v4?
If that sounds reasonable, I'd be happy to open a PR.
Hi, I reproduced this on current main: fields.Date().deserialize(datetime.datetime(2024, 12, 31, 15, 45, 30)) returns the datetime unchanged, with the time component preserved. I understand from the discussion that a code-level strict fix is not wanted, and that documenting the behavior was suggested instead.
Would you accept a documentation clarification along these lines, and may I open a PR for it? Here is the draft wording I would add to the Date field docstring (and note in the changelog):
Note that a
datetimeinstance is also accepted here, becausedatetimeis a subclass ofdate. In that case the time component is preserved as-is in the deserialized value; only the date part is not extracted. If you need the time stripped, normalize the value to adatebefore deserialization.
Happy to adjust the wording if you prefer it phrased differently.