Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime field: allow timestamp 0 #2264

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,6 @@ def _serialize(self, value, attr, obj, **kwargs) -> str | float | None:
return value.strftime(data_format)

def _deserialize(self, value, attr, data, **kwargs) -> dt.datetime:
if not value: # Falsy values, e.g. '', None, [] are not valid
raise self.make_error("invalid", input=value, obj_type=self.OBJ_TYPE)
data_format = self.format or self.DEFAULT_FORMAT
func = self.DESERIALIZATION_FUNCS.get(data_format)
try:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ def test_field_toggle_show_invalid_value_in_error_message(self):
[
"not-a-datetime",
42,
0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought(non-blocking): I believe this will also make False a valid value which isn't totally intuitive. But I'm not sure there's a way around that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a condition to reject boolean inputs, copied from numeric fields above.

Copy link
Member

@deckar01 deckar01 May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be tempted to inherit Float to match datetime.fromtimestamp()'s type signature and avoid duplicating that logic. That would also allow removing value = float(value) from utils.from_timestamp() which is equally guilty for the unexpected bool to int promotion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought inheritance wouldn't really work with the other date formats.

I would push this logic down to utils.from_timestamp() since all of the other date time utils.from_* properly error on bools already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I added your commit to the PR.

"",
[],
"2018",
Expand Down Expand Up @@ -576,7 +577,7 @@ def test_timestamp_field_deserialization(self, fmt, value, expected):
@pytest.mark.parametrize("fmt", ["timestamp", "timestamp_ms"])
@pytest.mark.parametrize(
"in_value",
["", "!@#", 0, -1, dt.datetime(2013, 11, 10, 1, 23, 45)],
["", "!@#", -1, dt.datetime(2013, 11, 10, 1, 23, 45)],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: might be worth testing None here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None fails validation before reaching _deserialize.

ValidationError: Field may not be null.

)
def test_invalid_timestamp_field_deserialization(self, fmt, in_value):
field = fields.DateTime(format=fmt)
Expand Down