marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

v4 regression: Date field load() silently accepts datetime instances (time component kept)

Open simpleqt opened this issue 1 month ago • 4 comments

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._deserialize returns any isinstance(value, internal_type) value unchanged, and datetime.datetime subclasses datetime.date, so the fast path swallows it.
  • The dump direction is unaffected — dump correctly renders datetime → '2014-08-21' — which makes the load-side passthrough inconsistent within the same field.
  • DateTime and Time fields don't have the inverse problem (date is not a subclass of datetime/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.

simpleqt avatar Oct 06 '26 17:10 simpleqt

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.

lafrech avatar Oct 18 '26 15:10 lafrech

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.

simpleqt avatar Oct 20 '26 06:10 simpleqt

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.

r0nik123 avatar Oct 30 '26 09:10 r0nik123

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 datetime instance is also accepted here, because datetime is a subclass of date. 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 a date before deserialization.

Happy to adjust the wording if you prefer it phrased differently.

zheng-guannan577 avatar Nov 11 '26 01:11 zheng-guannan577