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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

### Fixed

- Allow `Table.setdefault()` and `InlineTable.setdefault()` to retrieve existing values without supplying a default, matching document and mapping behavior.

- Fix `item()` and `dumps()` writing a `datetime` whose UTC offset is not a whole number of minutes (e.g. the historical LMT offsets `zoneinfo` gives, such as `-00:44:30`) as invalid TOML like `1900-01-01T12:00:00Z:30`, which no parser accepts. Such a value is now written as the same instant in UTC; this also covers `DateTime.replace()` and `astimezone()`.
- 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.
Expand Down
32 changes: 32 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1901,3 +1901,35 @@ def test_out_of_order_table_proxy_membership() -> None:
_ = 123 in table
# membership must not resolve values or mutate the document
assert doc.as_string() == content


@pytest.mark.parametrize("inline", [False, True])
@pytest.mark.parametrize("literal, expected", [("0", 0), ("false", False), ('""', "")])
def test_table_setdefault_existing_key_without_default(
inline: bool, literal: str, expected: int | bool | str
) -> None:
if inline:
source = f"settings = {{ value = {literal} }} # preserve formatting\n"
else:
source = f"[settings] # preserve formatting\nvalue = {literal}\n"
document = parse(source)
settings = document["settings"]
assert isinstance(settings, (Table, InlineTable))

assert settings.setdefault("value") == expected
assert api.dumps(document) == source


@pytest.mark.parametrize("inline", [False, True])
def test_table_setdefault_missing_key_without_default_preserves_document(
inline: bool,
) -> None:
from tomlkit.exceptions import ConvertError

source = "settings = { value = 1 }\n" if inline else "[settings]\nvalue = 1\n"
document = parse(source)
settings = document["settings"]
assert isinstance(settings, (Table, InlineTable))
with pytest.raises(ConvertError):
settings.setdefault("missing")
assert api.dumps(document) == source
2 changes: 1 addition & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ def remove(self: AT, key: Key | str) -> AT:
def item(self, key: Key | str) -> Item | OutOfOrderTableProxy:
return self._value.item(key)

def setdefault(self, key: Key | str, default: Any) -> Any: # type: ignore[override]
def setdefault(self, key: Key | str, default: Any = None) -> Any:
super().setdefault(key, default)
return self[key]

Expand Down
Loading