Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix a bug that escape chars are lost after concat with another string #235

Merged
merged 5 commits into from
Sep 14, 2022
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
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