Skip to content

Commit

Permalink
Merge pull request #2264 from marshmallow-code/allow_timestamp_0
Browse files Browse the repository at this point in the history
DateTime field: allow timestamp 0
  • Loading branch information
lafrech authored May 1, 2024
2 parents 183c411 + 58fbbcd commit 03f56a4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
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
2 changes: 2 additions & 0 deletions src/marshmallow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def from_iso_date(value):


def from_timestamp(value: typing.Any) -> dt.datetime:
if value is True or value is False:
raise ValueError("Not a valid POSIX timestamp")
value = float(value)
if value < 0:
raise ValueError("Not a valid POSIX timestamp")
Expand Down
5 changes: 4 additions & 1 deletion tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ def test_field_toggle_show_invalid_value_in_error_message(self):
[
"not-a-datetime",
42,
True,
False,
0,
"",
[],
"2018",
Expand Down Expand Up @@ -576,7 +579,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)],
)
def test_invalid_timestamp_field_deserialization(self, fmt, in_value):
field = fields.DateTime(format=fmt)
Expand Down

0 comments on commit 03f56a4

Please sign in to comment.