diff --git a/src/marshmallow/fields.py b/src/marshmallow/fields.py index 42e529c95..ceb32aa91 100644 --- a/src/marshmallow/fields.py +++ b/src/marshmallow/fields.py @@ -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: diff --git a/src/marshmallow/utils.py b/src/marshmallow/utils.py index 1194bb3a7..a5fe72624 100644 --- a/src/marshmallow/utils.py +++ b/src/marshmallow/utils.py @@ -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") diff --git a/tests/test_deserialization.py b/tests/test_deserialization.py index 6c32feadb..442b5efbd 100644 --- a/tests/test_deserialization.py +++ b/tests/test_deserialization.py @@ -422,6 +422,9 @@ def test_field_toggle_show_invalid_value_in_error_message(self): [ "not-a-datetime", 42, + True, + False, + 0, "", [], "2018", @@ -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)