Repository navigation
Conversation
…fixes jmespath#318) Parser caches the parsed AST per expression, so the literal node's already-decoded Python value is shared across every call. Returning it directly meant that two searches of `[]` or `{}` would hand back the same list/dict object, and any in-place mutation by the caller would leak into every subsequent call -- sometimes into completely unrelated search calls on a different input. visit_literal now copies the decoded value with copy.deepcopy on every visit. Immutable literals (str/int/float/bool/None) are unaffected; mutable JSON literals become independent per call. Performance is unchanged for the common case where the result is read-only. Four regression tests in tests/test_search.py cover list, object, and nested list literals, plus a control that string literals still come back as plain strings.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Avoid unnecessary immutable-scalar copying and clarify the misleading comment before approval.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Fixes shared mutable literal results by returning independent values for each search.
Changes:
- Deep-copy cached literal values during visitation.
- Add regression tests for list, object, nested, and string literals.
| File | Summary | Review notes |
|---|---|---|
jmespath/visitor.py |
Returns fresh literal values per visit. | Moderate (1 vote): avoid deep-copying immutable scalars. Nit (2 votes): clarify the implementation comment. |
tests/test_search.py |
Adds literal freshness regression coverage. | No findings. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+233
to
+234
| # into every other call. Re-decode JSON literals on every visit so | ||
| # callers get a fresh, independent value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #318.
What
Parsercaches the parsed AST per expression, so theliteralnode's already-decoded Python value is shared across every call tosearch(). Returning that object directly meant that two searches of the same literal expression ([],{},[1, 2], ...) would hand back the same list/dict object -- any in-place mutation by the caller would leak into every subsequent call, including unrelated searches on completely different inputs.visit_literalnow wraps the cached value withcopy.deepcopyon every visit. Immutable literals (str / int / float / bool / None) are unaffected; mutable JSON literals become independent per call.Repro (before)
After:
aandbare independent lists.Diff
Tests
Four regression tests in
tests/test_search.py(newTestLiteralFreshnessclass):test_list_literal_is_independent_per_searchtest_object_literal_is_independent_per_searchtest_nested_list_literal_is_independent(covers[[]]etc.)test_string_literal_remains_a_string(control)Full suite:
995 passed, 1 skipped(excludingtests/legacy/which is data-only). No existing tests changed.Performance
copy.deepcopyis only invoked for literal nodes in expressions that use them, and only mutable JSON literals allocate new containers. The common case -- read-only results, immutable literals, expressions that don't use literals at all -- has the same cost as before.AI disclosure
Prepared with AI assistance (kilo assistant); the diagnosis, fix, and tests were developed and verified locally before submission.