question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rationale behind fields.DateTime().deserialize('1916') returning datetime(1916, 11, 30, 0, 0)?

See original GitHub issue

I just noticed that when I deserialize a date expressed as a ‘YY’ or ‘YY-mm’ string, I get a datetime with month defaulting to 11 and day defaulting to 30 (except for february).

f = fields.DateTime()
f.deserialize('1916')
# datetime.datetime(1916, 11, 30, 0, 0)
f.deserialize('1916-2')
# datetime.datetime(1916, 2, 29, 0, 0)
f.deserialize('1916-3')
# datetime.datetime(1916, 3, 30, 0, 0)

Is this intended?

Shouldn’t this be either rejected or default to a documented value (could be january 1st)?

To prevent this seemingly non-deterministic behavior (it seems reproducible, but I don’t get the rationale), as a workaround, it is possible to set a format explicitly, which rejects such incomplete inputs, but this might not always be desired (be loose on inputs / strict on outputs).

f = fields.DateTime(format='%Y-%m-%d')
f.deserialize('1916-05-04')
# datetime.datetime(1916, 5, 4, 0, 0)
f.deserialize('1916')
# marshmallow.exceptions.ValidationError: Not a valid datetime.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
maximkulkincommented, Nov 30, 2016

Exactly

0reactions
sloriacommented, Jun 1, 2017

@lnielsen See the discussion on https://github.com/marshmallow-code/marshmallow/pull/605 :

I’m sorry for any inconvenience this change may have caused you. This was, however, a bug fix–DateTime was accepting invalid input, and this fixes that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Symfony Serializer of DateTime Response String
When storing the due date, the timezone is saved and there is an additional timezone field stored. Is there a way to pull...
Read more >
Fields — marshmallow 3.19.0 documentation
A field that takes the value returned by a Schema method. A formatted naive datetime string. Allows you to nest a Schema inside...
Read more >
eventsource.cs
Project path: ndp\clr\src\bcl\mscorlib.csproj. Files: 1,258. Lines of code: 484,913. Bytes: 20,489,855. Declared symbols: 38,440. Declared types: 3,122
Read more >
Module Code - Deform Demo
OneOf([x[0] for x in choices]), widget=deform.widget. ... Range( min=datetime.datetime( datetime.date.today().year, 1, 1, 12, 30, tzinfo=iso8601.
Read more >
Release Notes — Airflow Documentation
New to this release of Airflow is the concept of Datasets to Airflow, and with it a new way of scheduling dags: data-aware...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found