Skip to content

Commit

Permalink
fix: fix a bug that escape chars are lost after concat with another s…
Browse files Browse the repository at this point in the history
…tring (#235)
  • Loading branch information
frostming authored Sep 14, 2022
1 parent 80f958c commit a0f2309
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ jobs:
python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
submodules: "recursive"

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -45,7 +45,7 @@ jobs:
- name: Install Poetry
shell: bash
run: |
curl -fsSL https://install.python-poetry.org | python - -y
curl -fsSL https://install.python-poetry.org | python - -y --version 1.1.15
- name: Update PATH
if: ${{ matrix.os != 'Windows' }}
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
Expand All @@ -59,7 +59,7 @@ jobs:
run: |
poetry config virtualenvs.in-project true
- name: Set up cache
uses: actions/cache@v2
uses: actions/cache@v3
id: cache
with:
path: .venv
Expand Down
8 changes: 8 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,14 @@ def test_strings_behave_like_strs():
assert doc.as_string() == 'str = "foo bar" # Comment'


def test_string_add_preserve_escapes():
i = api.value('"foo\\"bar"')
i += " baz"

assert i == 'foo"bar baz'
assert i.as_string() == '"foo\\"bar baz"'


def test_tables_behave_like_dicts():
t = item({"foo": "bar"})

Expand Down
16 changes: 7 additions & 9 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,18 +1779,16 @@ def value(self) -> str:
def as_string(self) -> str:
return f"{self._t.value}{decode(self._original)}{self._t.value}"

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

return self._new(result)
return self._new(result, original)

def __sub__(self, other):
result = super().__sub__(other)

return self._new(result)

def _new(self, result):
return String(self._t, result, result, self._trivia)
def _new(self, result: str, original: str) -> "String":
return String(self._t, result, original, self._trivia)

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

0 comments on commit a0f2309

Please sign in to comment.