Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from tomlkit.items import Array
from tomlkit.items import Bool
from tomlkit.items import Comment
from tomlkit.items import DateTime
from tomlkit.items import InlineTable
from tomlkit.items import Integer
from tomlkit.items import Item
Expand All @@ -35,6 +36,7 @@
from tomlkit.items import String
from tomlkit.items import StringType
from tomlkit.items import Table
from tomlkit.items import Time
from tomlkit.items import Trivia
from tomlkit.items import item
from tomlkit.parser import Parser
Expand Down Expand Up @@ -802,6 +804,46 @@ def test_times_behave_like_times() -> None:
assert i.as_string() == "13:34:56"


def test_times_behave_like_times_fold() -> None:
# issue #619: the stdlib `fold` argument is missing from these types.
i = item(time(12, 34, 56, fold=1))

assert i.fold == 1
assert i.as_string() == "12:34:56"

i = i.replace(hour=13)
assert i == time(13, 34, 56)
assert i.fold == 1
assert i.as_string() == "13:34:56"

j = item(time(12, 34, 56)).replace(fold=1)
assert j.fold == 1

# exact call shape used by pure-python (PyPy) datetime implementations
# in `replace()`: type(self)(..., fold=fold)
k = Time(12, 34, 56, 0, None, fold=1)
assert k.fold == 1


def test_datetimes_behave_like_datetimes_fold() -> None:
# issue #619: the stdlib `fold` argument is missing from these types.
i = item(datetime(2018, 7, 22, 12, 34, 56, fold=1))

assert i.fold == 1

i = i.replace(year=2019, fold=1)
assert i.fold == 1

i = i.replace(year=2020)
assert i.fold == 1

j = item(datetime(2018, 7, 22, 12, 34, 56)).replace(fold=1)
assert j.fold == 1

k = DateTime(2018, 7, 22, 12, 34, 56, 0, None, fold=1)
assert k.fold == 1


def test_strings_behave_like_strs() -> None:
i = item("foo")

Expand Down
11 changes: 10 additions & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def item(value: Any, _parent: Item | None = None, _sort_keys: bool = False) -> I
value.tzinfo,
Trivia(),
value.isoformat().replace("+00:00", "Z"),
fold=value.fold,
)
elif isinstance(value, date):
return Date(value.year, value.month, value.day, Trivia(), value.isoformat())
Expand All @@ -206,6 +207,7 @@ def item(value: Any, _parent: Item | None = None, _sort_keys: bool = False) -> I
value.tzinfo,
Trivia(),
value.isoformat(),
fold=value.fold,
)
else:
for encoder in CUSTOM_ENCODERS:
Expand Down Expand Up @@ -1040,6 +1042,7 @@ def __new__(
tzinfo: tzinfo | None,
trivia: Trivia | None = None,
raw: str | None = None,
fold: int = 0,
**kwargs: object,
) -> DateTime:
return datetime.__new__(
Expand All @@ -1052,6 +1055,7 @@ def __new__(
second,
microsecond,
tzinfo=tzinfo,
fold=fold,
)

def __init__(
Expand All @@ -1066,6 +1070,7 @@ def __init__(
tzinfo: tzinfo | None,
trivia: Trivia | None = None,
raw: str | None = None,
fold: int = 0,
**kwargs: object,
) -> None:
super().__init__(trivia or Trivia())
Expand Down Expand Up @@ -1164,6 +1169,7 @@ def _new(self, result: datetime) -> DateTime:
result.tzinfo,
self._trivia,
raw,
fold=result.fold,
)

def _getstate(
Expand Down Expand Up @@ -1276,8 +1282,9 @@ def __new__(
tzinfo: tzinfo | None,
trivia: Trivia | None = None,
raw: str = "",
fold: int = 0,
) -> Time:
return time.__new__(cls, hour, minute, second, microsecond, tzinfo)
return time.__new__(cls, hour, minute, second, microsecond, tzinfo, fold=fold)

def __init__(
self,
Expand All @@ -1288,6 +1295,7 @@ def __init__(
tzinfo: tzinfo | None,
trivia: Trivia | None = None,
raw: str = "",
fold: int = 0,
) -> None:
super().__init__(trivia or Trivia())

Expand Down Expand Up @@ -1322,6 +1330,7 @@ def _new(self, result: time) -> Time:
result.tzinfo,
self._trivia,
raw,
fold=result.fold,
)

def _getstate(
Expand Down
Loading