Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
55 changes: 55 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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"
7 changes: 6 additions & 1 deletion tomlkit/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading