Skip to content

Child.find(source) method updates contents of source #214

@eric-spitler

Description

@eric-spitler
  • Python 3.13.12
  • jsonpath_ng 1.8.0

My tests were failing when they worked on version 1.7.0 and I traced it back to the contents of a dict being changed when calling jsonpath_ng.ext.parse(path).find(source)

To cause it to fail, I change the source to be unwritable using MappingProxyType

Here are examples that will cause the failure:

from copy import deepcopy
from json import dumps
from types import MappingProxyType

from jsonpath_ng.ext import parse

path = '$.theChildToBreak[?(@.someFloatValue >= 85)]'
original = {
    "theChildToBreak": {
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [0.0, 0.0],
                    [0.0, 1.0],
                    [1.0, 1.0],
                    [1.0, 0.0],
                    [0.0, 0.0]
                ]
            ]
        },
        "aBoolean": True
    }
}
source = deepcopy(original)
parse(path).find(source)
assert original != source
print(type(source['theChildToBreak']), type(original['theChildToBreak']))
print(dumps(source, indent=4))
# ^ prints <class 'list'> <class 'dict'>
parse(path).find(MappingProxyType(original))  # make the "original" unmodifiable
<class 'list'> <class 'dict'>
{
    "theChildToBreak": [
        {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        0.0,
                        0.0
                    ],
                    [
                        0.0,
                        1.0
                    ],
                    [
                        1.0,
                        1.0
                    ],
                    [
                        1.0,
                        0.0
                    ],
                    [
                        0.0,
                        0.0
                    ]
                ]
            ]
        },
        true
    ]
}
Traceback (most recent call last):
  File "/home/break_jsonpath.py", line 31, in <module>
    parse(path).find(MappingProxyType(original))  # make the "original" unmodifiable
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/python3.13/site-packages/jsonpath_ng/jsonpath.py", line 282, in find
    for submatch in self.right.find(subdata)]
                    ~~~~~~~~~~~~~~~^^^^^^^^^
  File "/home/python3.13/site-packages/jsonpath_ng/ext/filter.py", line 45, in find
    datum.value = list(datum.value.values())
    ^^^^^^^^^^^
  File "/home/python3.13/site-packages/jsonpath_ng/jsonpath.py", line 116, in value
    self.path.update(self.context.value, value)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/python3.13/site-packages/jsonpath_ng/jsonpath.py", line 655, in update
    return self._update_base(data, val, create=False)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/python3.13/site-packages/jsonpath_ng/jsonpath.py", line 669, in _update_base
    data[field] = val
    ~~~~^^^^^^^
TypeError: 'mappingproxy' object does not support item assignment

Process finished with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions