diff --git a/tests/test_api.py b/tests/test_api.py index 741f8956..14d245b6 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -245,6 +245,19 @@ def test_date() -> None: tomlkit.date("12:34:56") +@pytest.mark.parametrize( + "raw", + [ + "1979-05-13T12:34:56", + "1979-05-13T12:34:56Z", + "1979-05-13T12:34:56+02:00", + ], +) +def test_date_rejects_datetime_strings(raw: str) -> None: + with pytest.raises(ValueError, match=r"date\(\) only accepts date strings"): + tomlkit.date(raw) + + def test_time() -> None: dt = tomlkit.time("12:34:56") diff --git a/tomlkit/api.py b/tomlkit/api.py index a7f3abdd..e19d6635 100644 --- a/tomlkit/api.py +++ b/tomlkit/api.py @@ -182,7 +182,7 @@ def string( def date(raw: str) -> Date: """Create a TOML date.""" value = parse_rfc3339(raw) - if not isinstance(value, _datetime.date): + if isinstance(value, _datetime.datetime) or not isinstance(value, _datetime.date): raise ValueError("date() only accepts date strings.") return item(value)