Skip to content

Commit a0f2309

Browse files
authored
fix: fix a bug that escape chars are lost after concat with another string (#235)
1 parent 80f958c commit a0f2309

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

.github/workflows/tests.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ jobs:
2727
python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]
2828

2929
steps:
30-
- uses: actions/checkout@v2
30+
- uses: actions/checkout@v3
3131
with:
3232
submodules: "recursive"
3333

3434
- name: Set up Python ${{ matrix.python-version }}
35-
uses: actions/setup-python@v2
35+
uses: actions/setup-python@v4
3636
with:
3737
python-version: ${{ matrix.python-version }}
3838

@@ -45,7 +45,7 @@ jobs:
4545
- name: Install Poetry
4646
shell: bash
4747
run: |
48-
curl -fsSL https://install.python-poetry.org | python - -y
48+
curl -fsSL https://install.python-poetry.org | python - -y --version 1.1.15
4949
- name: Update PATH
5050
if: ${{ matrix.os != 'Windows' }}
5151
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
@@ -59,7 +59,7 @@ jobs:
5959
run: |
6060
poetry config virtualenvs.in-project true
6161
- name: Set up cache
62-
uses: actions/cache@v2
62+
uses: actions/cache@v3
6363
id: cache
6464
with:
6565
path: .venv

tests/test_items.py

+8
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,14 @@ def test_strings_behave_like_strs():
708708
assert doc.as_string() == 'str = "foo bar" # Comment'
709709

710710

711+
def test_string_add_preserve_escapes():
712+
i = api.value('"foo\\"bar"')
713+
i += " baz"
714+
715+
assert i == 'foo"bar baz'
716+
assert i.as_string() == '"foo\\"bar baz"'
717+
718+
711719
def test_tables_behave_like_dicts():
712720
t = item({"foo": "bar"})
713721

tomlkit/items.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1779,18 +1779,16 @@ def value(self) -> str:
17791779
def as_string(self) -> str:
17801780
return f"{self._t.value}{decode(self._original)}{self._t.value}"
17811781

1782-
def __add__(self, other):
1782+
def __add__(self: ItemT, other: str) -> ItemT:
1783+
if not isinstance(other, str):
1784+
return NotImplemented
17831785
result = super().__add__(other)
1786+
original = self._original + getattr(other, "_original", other)
17841787

1785-
return self._new(result)
1788+
return self._new(result, original)
17861789

1787-
def __sub__(self, other):
1788-
result = super().__sub__(other)
1789-
1790-
return self._new(result)
1791-
1792-
def _new(self, result):
1793-
return String(self._t, result, result, self._trivia)
1790+
def _new(self, result: str, original: str) -> "String":
1791+
return String(self._t, result, original, self._trivia)
17941792

17951793
def _getstate(self, protocol=3):
17961794
return self._t, str(self), self._original, self._trivia

0 commit comments

Comments
 (0)