Skip to content

Fix quoted string keys breaking extract() and Delta - #673

Open
tony1233200 wants to merge 1 commit into
qlustered:devfrom
tony1233200:fix/648-quoted-string-keys
Open

tony1233200 wants to merge 1 commit into
qlustered:devfrom
tony1233200:fix/648-quoted-string-keys

Conversation

@tony1233200

Copy link
Copy Markdown

Fix quoted string keys breaking extract() and Delta

Fixes #648.

Problem

A dictionary key containing both single and double quotes (e.g. Bob's "plan") produced a path that parse_path(), extract() and Delta could not consume:

from deepdiff import DeepDiff, Delta, extract

key = '''Bob's "plan"'''
before, after = {key: 1}, {key: 2}
diff = DeepDiff(before, after)
path = next(iter(diff['values_changed']))
# root["Bob's "plan""]
extract(after, path)  # KeyError: "Bob's "
before + Delta(diff, bidirectional=True, raise_errors=True)  # DeltaError

Root cause

stringify_element() replaced both quote types in such a key with the 𝆺𝅥𝅯 marker and wrapped the result in double quotes, producing a path the parser cannot split back into the original key. The same failure occurred for added/removed quoted keys.

Change

deepdiff/path.py only:

  • Keys containing both quote types are now emitted as explicit u-prefixed Python string literals (str.__repr__), so the parser can recover them exactly with literal_eval. This keeps legacy raw-backslash path spellings intact, including keys ending in a backslash.
  • _path_to_elements() recognizes the u'...' / u"..." literal at the start of a bracket segment and parses it as one element.
  • String subclasses are handled via str.__repr__ (not the subclass's custom __repr__).

Compatibility note (for the maintainer)

As noted in #648, this changes the canonical spelling of previously malformed paths: old paths that only ever worked as opaque exclude_paths strings will no longer match under the new spelling, while exclusions regenerated from the new output work. Ordinary keys, single-quote-type keys and old raw-backslash paths keep their existing spelling and parsing. Happy to adjust the approach if a different compatibility direction is preferred.

Tests

  • tests/test_path.py: new round-trip tests for both-quote keys (changed/added/removed), existing-spelling keys, nested keys, keys followed by attributes, and str subclass keys — 21 new parametrized cases.
  • tests/test_diff_text.py: updated test_diff_quote_and_double_quote_in_string expectation to the new spelling.
  • Repro from the issue: before the fix extract raised KeyError and Delta raised DeltaError; after the fix extract returns 2 and Delta round-trips (old + delta == new, new - delta == old).
  • Full suite: pytest tests/ — 1380 passed, 45 skipped, 0 failed.
  • Lint: flake8 on the touched files reports the same 64 pre-existing findings as the clean tree — no new violations.

Notes

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 qlustered#648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant