diff --git a/CHANGELOG.md b/CHANGELOG.md index e6e32c3d..d2e8c49c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixed +- `item()` and `dumps()` now write the underlying characters of a `str` subclass instead of calling its `__str__` method, so `str`-based `Enum` members and custom string wrappers round-trip again instead of being serialized as e.g. `"Color.RED"` or producing invalid TOML. This was a regression from the single-pass escaping added in 0.15.3. ([#725](https://github.com/python-poetry/tomlkit/issues/725)) - The parser now rejects numbers with non-ASCII digits, such as `1٠` or `10`. Previously `int()` and `float()` converted them, so an invalid document parsed to a wrong value. ([#728](https://github.com/python-poetry/tomlkit/pull/728)) - Fix deleting one of several nested dotted keys inside an inline table writing invalid TOML: deleting `c` from `x = {a.b.c = 1, a.b.d = 2}` gave `x = {a.b = {}, a.b.d = 2}`, which defines `a.b` twice. The emptied part is now removed from the document, as it already was for a single-level dotted key. ([#732](https://github.com/python-poetry/tomlkit/pull/732)) diff --git a/tests/test_utils.py b/tests/test_utils.py index d80cc9b1..4352de0e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -3,11 +3,15 @@ from datetime import time from datetime import timedelta as td from datetime import timezone as tz +from enum import Enum from typing import Union import pytest +import tomlkit + from tomlkit._utils import _utc +from tomlkit._utils import escape_string from tomlkit._utils import parse_rfc3339 @@ -62,3 +66,54 @@ def test_parse_rfc3339_date(string: str, expected: Union[dt, date, time]) -> Non ) def test_parse_rfc3339_time(string: str, expected: Union[dt, date, time]) -> None: assert parse_rfc3339(string) == expected + + +class _Color(str, Enum): + RED = "red" + + +class _Quoted(str): + def __str__(self) -> str: + return f'"{super().__str__()}"' + + +def test_escape_string_returns_plain_str() -> None: + result = escape_string("hello") + + assert result == "hello" + assert type(result) is str + + +def test_escape_string_str_subclass_stays_plain_str() -> None: + """Issue #725: a ``str`` subclass must keep its characters, not its ``__str__``.""" + result = escape_string(_Quoted("plain")) + + assert result == "plain" + assert type(result) is str + + +def test_escape_string_str_subclass_with_sequences_stays_plain_str() -> None: + """Issue #725: the escaping branch must not leak the subclass either.""" + result = escape_string(_Quoted("a\nb")) + + assert result == "a\\nb" + assert type(result) is str + + +def test_dumps_str_enum_writes_value() -> None: + """Issue #725: ``dumps`` wrote ``"Color.RED"`` instead of ``"red"``.""" + document = tomlkit.document() + document["color"] = _Color.RED + + assert tomlkit.dumps(document) == 'color = "red"\n' + + +def test_dumps_str_subclass_with_custom_str_round_trips() -> None: + """Issue #725: the emitted document could not be re-parsed.""" + document = tomlkit.document() + document["k"] = _Quoted("shown") + + dumped = tomlkit.dumps(document) + + assert dumped == 'k = "shown"\n' + assert tomlkit.parse(dumped)["k"] == "shown" diff --git a/tomlkit/_utils.py b/tomlkit/_utils.py index 1aa374f9..3f9a79a0 100644 --- a/tomlkit/_utils.py +++ b/tomlkit/_utils.py @@ -147,7 +147,12 @@ def escape_string(s: str, escape_sequences: Collection[str] = _basic_escapes) -> found_sequences = [seq for seq in escape_sequences if seq in s] if not found_sequences: # always return a plain str, also for str subclasses such as String - return str(s) + # NOTE: calling `str(s)` here would dispatch to an overridden + # `__str__` on a str subclass (e.g. `enum.StrEnum` or a custom + # wrapper) and serialize that representation instead of the + # underlying characters. `str.__str__` unboxes to a plain `str` + # without invoking the subclass implementation. + return str.__str__(s) # Replace every sequence in a single left-to-right pass. Longer sequences # come first so that '"""' is matched as a whole.