diff --git a/CHANGELOG.md b/CHANGELOG.md index d36550d8..a5bb001d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixed +- Allow `DateTime.astimezone()` to use the local timezone when no timezone argument is supplied, matching `datetime.datetime`. + - Arithmetic and `replace()` results for `Integer`, `Float`, `String`, `DateTime`, `Date` and `Time` no longer share the original item's `Trivia`, so changing the result's comment or indentation no longer rewrites the item still stored in the document. ## [0.15.2] - 2026-10-16 diff --git a/tests/test_items.py b/tests/test_items.py index 36f4afd2..a5202775 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -828,6 +828,18 @@ def test_datetimes_behave_like_datetimes(tz_utc: tzinfo, tz_pst: tzinfo) -> None assert doc.as_string() == "dt = 2018-07-23T12:34:56-05:00" +def test_datetime_astimezone_defaults_to_local_timezone() -> None: + value = parse("time = 1979-05-27T07:32:00Z # birthday\n")["time"] + expected = datetime(1979, 5, 27, 7, 32, tzinfo=timezone.utc).astimezone() + + result = value.astimezone() + + assert isinstance(result, DateTime) + assert result.unwrap() == expected + assert result.as_string() == expected.isoformat() + assert result == value.astimezone(None) + + @pytest.mark.parametrize("fold", [0, 1]) def test_datetime_subtraction_preserves_input_fold(tz_fold: tzinfo, fold: int) -> None: value = datetime(2018, 11, 4, 1, 30, tzinfo=tz_fold, fold=fold) diff --git a/tomlkit/items.py b/tomlkit/items.py index a73fab8a..48203938 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -1184,7 +1184,7 @@ def __sub__(self, other: timedelta | datetime) -> DateTime | timedelta: def replace(self, *args: object, **kwargs: object) -> DateTime: return self._new(super().replace(*args, **kwargs)) # type: ignore[arg-type] - def astimezone(self, tz: tzinfo) -> DateTime: # type: ignore[override] + def astimezone(self, tz: tzinfo | None = None) -> DateTime: result = super().astimezone(tz) if PY38: return result