From 3270eae1656e7e6ddbd2a0e37513515f6a8e954d Mon Sep 17 00:00:00 2001 From: tony1233200 <127579663+tony1233200@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:28:52 +0000 Subject: [PATCH] Fix quoted string keys breaking extract() and Delta Keys containing both single and double quotes produced paths that parse_path(), extract() and Delta could not consume. Emit such keys as explicit u-prefixed Python string literals and teach the path parser to recognize them, keeping legacy raw-backslash paths intact. Fixes #648 --- deepdiff/path.py | 29 +++++++++++---- tests/test_diff_text.py | 2 +- tests/test_path.py | 82 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 9 deletions(-) diff --git a/deepdiff/path.py b/deepdiff/path.py index 3854b6bf..966147a0 100644 --- a/deepdiff/path.py +++ b/deepdiff/path.py @@ -72,6 +72,7 @@ def _add_to_elements(elements, elem, inside): DEFAULT_FIRST_ELEMENT = ('root', GETATTR) +_ESCAPED_STR_RE = re.compile(r"""u(?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*")(?=\])""") @lru_cache(maxsize=1024 * 128) @@ -96,7 +97,22 @@ def _path_to_elements(path, root_element=DEFAULT_FIRST_ELEMENT): brackets = [] inside_quotes = False quote_used = '' - for char in path: + literal_end = 0 + for index, char in enumerate(path): + if index < literal_end: + continue + if char == 'u' and not inside_quotes and inside == '[' and not elem: + match = _ESCAPED_STR_RE.match(path, index) + if match: + try: + value = literal_eval(match.group()) + except (ValueError, SyntaxError): + pass + else: + elements.append((value, GET)) + literal_end = match.end() + prev_char = None + continue if prev_char == '𝆺𝅥𝅯': elem += char elif char in {'"', "'"}: @@ -313,13 +329,10 @@ def parse_path(path, root_element=DEFAULT_FIRST_ELEMENT, include_actions=False): def stringify_element(param, quote_str=None): has_quote = "'" in param has_double_quote = '"' in param - if has_quote and has_double_quote and not quote_str: - new_param = [] - for char in param: - if char in {'"', "'"}: - new_param.append('𝆺𝅥𝅯') - new_param.append(char) - result = '"' + ''.join(new_param) + '"' + if has_quote and has_double_quote: + # Mark escaped literals explicitly so legacy raw-backslash paths retain + # their meaning, including keys ending in a backslash before a new segment. + result = 'u' + str.__repr__(param) elif has_quote: result = f'"{param}"' elif has_double_quote: diff --git a/tests/test_diff_text.py b/tests/test_diff_text.py index cfeda768..31174cca 100755 --- a/tests/test_diff_text.py +++ b/tests/test_diff_text.py @@ -395,7 +395,7 @@ def test_diff_quote_and_double_quote_in_string(self): t1 = {'''a'"a''': 1} t2 = {'''a'"a''': 2} diff = DeepDiff(t1, t2) - expected = {'values_changed': {'root["a\'"a"]': {'new_value': 2, 'old_value': 1}}} + expected = {'values_changed': {"root[u'a\\\'\"a']": {'new_value': 2, 'old_value': 1}}} assert expected == diff def test_bytes(self): diff --git a/tests/test_path.py b/tests/test_path.py index edb2784f..5323d567 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -1,4 +1,5 @@ import pytest +from deepdiff import DeepDiff, Delta from deepdiff.path import _path_to_elements, GET, GETATTR, extract, parse_path, stringify_path, _add_to_elements @@ -56,6 +57,87 @@ def test_parse_path(): assert [{'element': 'joe', 'action': 'GET'}, {'element': 'age', 'action': 'GETATTR'}] == result4 +@pytest.mark.parametrize('key', [ + "a'\"a", + "a\"'a", + "a'\"\\", + "a'\"\\r\\n", + "a'\"\r\n", + "a'\"\U0001d1c0", + "a'][\"b.c", +]) +@pytest.mark.parametrize('change', ['added', 'removed', 'changed']) +def test_string_key_with_both_quotes_round_trip(key, change): + old = {'container': {'unchanged': 0}} + new = {'container': {'unchanged': 0}} + if change != 'added': + old['container'][key] = 1 + if change != 'removed': + new['container'][key] = 2 + diff = DeepDiff(old, new, threshold_to_diff_deeper=0) + category = 'values_changed' if change == 'changed' else f'dictionary_item_{change}' + path = next(iter(diff[category])) + + assert parse_path(path) == ['container', key] + assert stringify_path([('container', GET), (key, GET)]) == path + source = old if change == 'removed' else new + assert extract(source, path) == source['container'][key] + delta = Delta(diff, bidirectional=True, raise_errors=True) + assert old + delta == new + assert new - delta == old + assert old == {'container': {'unchanged': 0, **({key: 1} if change != 'added' else {})}} + assert new == {'container': {'unchanged': 0, **({key: 2} if change != 'removed' else {})}} + + +@pytest.mark.parametrize('key', [ + 'plain', "only'quote", 'only"quote', r'back\slash', 'ending\\', + "marker\U0001d1c0'quote", 'marker\U0001d1c0\U0001d1c0text', '*', '**', +]) +def test_existing_string_key_path_round_trip(key): + path = stringify_path([(key, GET)]) + assert parse_path(path) == [key] + assert extract({key: 1}, path) == 1 + + +@pytest.mark.parametrize('keys', [ + ['ending\\', "']"], + ['"\\', ']'], + ["'\\", '"]'], + ["marker\U0001d1c0'quote", 'child'], +]) +def test_existing_nested_string_key_path_round_trip(keys): + path = stringify_path([(key, GET) for key in keys]) + assert parse_path(path) == keys + assert extract({keys[0]: {keys[1]: 1}}, path) == 1 + + +def test_escaped_string_key_followed_by_attribute(): + key = "a'\"a" + path = stringify_path([(key, GET), ('age', GETATTR)]) + assert parse_path(path, include_actions=True) == [ + {'element': key, 'action': GET}, {'element': 'age', 'action': GETATTR}, + ] + + +def test_string_subclass_key_with_both_quotes_round_trip(): + class NamedString(str): + def __repr__(self): + return f'NamedString({str.__repr__(self)})' + + def __str__(self): + return 'custom string representation' + + key = NamedString("a'\"a") + old, new = {key: 1}, {key: 2} + diff = DeepDiff(old, new) + path = next(iter(diff['values_changed'])) + assert parse_path(path) == [key] + assert extract(new, path) == 2 + delta = Delta(diff, bidirectional=True, raise_errors=True) + assert old + delta == new + assert new - delta == old + + @pytest.mark.parametrize('test_num, elem, inside, expected', [ ( 1,