Skip to content
Open
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
19 changes: 14 additions & 5 deletions tests/test_toml_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import json
import os
import re

from datetime import date
from datetime import datetime
from datetime import time
from typing import Any
from typing import Callable

Expand All @@ -9,7 +13,6 @@
from tomlkit import load
from tomlkit import parse
from tomlkit._compat import decode
from tomlkit._utils import parse_rfc3339
from tomlkit.exceptions import TOMLKitError


Expand All @@ -23,15 +26,21 @@ def to_bool(s: str) -> bool:
return s == "true"


def normalize_isoformat(value: str) -> str:
# Python 3.9/3.10 require three or six fractional digits and a numeric UTC offset.
value = re.sub(r"\.(\d+)", lambda m: "." + m[1][:6].ljust(6, "0"), value)
return value.replace("Z", "+00:00")


stypes: dict[str, Callable[[str], Any]] = {
"string": str,
"bool": to_bool,
"integer": int,
"float": float,
"datetime": parse_rfc3339,
"datetime-local": parse_rfc3339,
"date-local": parse_rfc3339,
"time-local": parse_rfc3339,
"datetime": lambda s: datetime.fromisoformat(normalize_isoformat(s)),
"datetime-local": lambda s: datetime.fromisoformat(normalize_isoformat(s)),
"date-local": date.fromisoformat,
"time-local": lambda s: time.fromisoformat(normalize_isoformat(s)),
}


Expand Down
8 changes: 8 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
"1979-05-27T07:32:00-07:00",
dt(1979, 5, 27, 7, 32, 0, tzinfo=tz(td(seconds=-7 * 3600), "-07:00")),
),
(
"1979-05-27T07:32:00+05:30",
dt(1979, 5, 27, 7, 32, 0, tzinfo=tz(td(hours=5, minutes=30))),
),
(
"1979-05-27T07:32:00-03:30",
dt(1979, 5, 27, 7, 32, 0, tzinfo=tz(td(hours=-3, minutes=-30))),
),
(
"1979-05-27T00:32:00.999999-07:00",
dt(
Expand Down