Skip to content

Commit

Permalink
Remove from_iso_* functions from utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Dec 29, 2024
1 parent bd61175 commit d6fa11b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 88 deletions.
22 changes: 18 additions & 4 deletions src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
from collections.abc import Mapping as _Mapping
from enum import Enum as EnumType

# Remove this when dropping Python 3.10
try:
from backports.datetime_fromisoformat import MonkeyPatch
except ImportError:
pass
else:
MonkeyPatch.patch_fromisoformat()

from marshmallow import class_registry, types, utils, validate
from marshmallow.base import FieldABC, SchemaABC
from marshmallow.exceptions import (
Expand Down Expand Up @@ -1248,8 +1256,8 @@ class DateTime(Field):
} # type: dict[str, typing.Callable[[typing.Any], str | float]]

DESERIALIZATION_FUNCS = {
"iso": utils.from_iso_datetime,
"iso8601": utils.from_iso_datetime,
"iso": dt.datetime.fromisoformat,
"iso8601": dt.datetime.fromisoformat,
"rfc": utils.from_rfc,
"rfc822": utils.from_rfc,
"timestamp": utils.from_timestamp,
Expand Down Expand Up @@ -1397,7 +1405,10 @@ class Time(DateTime):

SERIALIZATION_FUNCS = {"iso": utils.to_iso_time, "iso8601": utils.to_iso_time}

DESERIALIZATION_FUNCS = {"iso": utils.from_iso_time, "iso8601": utils.from_iso_time}
DESERIALIZATION_FUNCS = {
"iso": dt.time.fromisoformat,
"iso8601": dt.time.fromisoformat,
}

DEFAULT_FORMAT = "iso"

Expand Down Expand Up @@ -1426,7 +1437,10 @@ class Date(DateTime):

SERIALIZATION_FUNCS = {"iso": utils.to_iso_date, "iso8601": utils.to_iso_date}

DESERIALIZATION_FUNCS = {"iso": utils.from_iso_date, "iso8601": utils.from_iso_date}
DESERIALIZATION_FUNCS = {
"iso": dt.date.fromisoformat,
"iso8601": dt.date.fromisoformat,
}

DEFAULT_FORMAT = "iso"

Expand Down
30 changes: 0 additions & 30 deletions src/marshmallow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
from email.utils import format_datetime, parsedate_to_datetime
from pprint import pprint as py_pprint

# Remove this when dropping Python 3.10
try:
from backports.datetime_fromisoformat import MonkeyPatch
except ImportError:
pass
else:
MonkeyPatch.patch_fromisoformat()

from marshmallow.base import FieldABC
from marshmallow.exceptions import FieldInstanceResolutionError
from marshmallow.warnings import RemovedInMarshmallow4Warning
Expand Down Expand Up @@ -133,28 +125,6 @@ def get_fixed_timezone(offset: int | float | dt.timedelta) -> dt.timezone:
return dt.timezone(dt.timedelta(minutes=offset), name)


def from_iso_datetime(value):
"""Parse a string and return a datetime.datetime.
This function supports time zone offsets. When the input contains one,
the output uses a timezone with a fixed offset from UTC.
"""
return dt.datetime.fromisoformat(value)


def from_iso_time(value):
"""Parse a string and return a datetime.time.
This function doesn't support time zone offsets.
"""
return dt.time.fromisoformat(value)


def from_iso_date(value):
"""Parse a string and return a datetime.date."""
return dt.date.fromisoformat(value)


def from_timestamp(value: typing.Any) -> dt.datetime:
if value is True or value is False:
raise ValueError("Not a valid POSIX timestamp")
Expand Down
55 changes: 1 addition & 54 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from marshmallow import Schema, fields, utils
from tests.base import assert_date_equal, assert_time_equal, central
from tests.base import central


def test_missing_singleton_copy():
Expand Down Expand Up @@ -175,59 +175,6 @@ def test_from_rfc(value, expected):
assert result == expected


@pytest.mark.parametrize(
("value", "expected"),
[
("2013-11-10T01:23:45", dt.datetime(2013, 11, 10, 1, 23, 45)),
(
"2013-11-10T01:23:45+00:00",
dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=dt.timezone.utc),
),
(
# Regression test for https://github.com/marshmallow-code/marshmallow/issues/1251
"2013-11-10T01:23:45.123+00:00",
dt.datetime(2013, 11, 10, 1, 23, 45, 123000, tzinfo=dt.timezone.utc),
),
(
"2013-11-10T01:23:45.123456+00:00",
dt.datetime(2013, 11, 10, 1, 23, 45, 123456, tzinfo=dt.timezone.utc),
),
(
"2013-11-10T01:23:45-06:00",
dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=central),
),
],
)
def test_from_iso_datetime(value, expected):
result = utils.from_iso_datetime(value)
assert type(result) is dt.datetime
assert result == expected


def test_from_iso_time_with_microseconds():
t = dt.time(1, 23, 45, 6789)
formatted = t.isoformat()
result = utils.from_iso_time(formatted)
assert type(result) is dt.time
assert_time_equal(result, t)


def test_from_iso_time_without_microseconds():
t = dt.time(1, 23, 45)
formatted = t.isoformat()
result = utils.from_iso_time(formatted)
assert type(result) is dt.time
assert_time_equal(result, t)


def test_from_iso_date():
d = dt.date(2014, 8, 21)
iso_date = d.isoformat()
result = utils.from_iso_date(iso_date)
assert type(result) is dt.date
assert_date_equal(result, d)


@pytest.mark.parametrize(
("value", "expected"),
[
Expand Down

0 comments on commit d6fa11b

Please sign in to comment.