Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Python JSONPath Change Log

## Version 1.3.1 (unreleased)

**Fixes**

- Fixed the non-standard JSON Patch operation, `addap`. Previously it was behaving like `addne`. See [#81](https://github.com/jg-rp/python-jsonpath/pull/81).
- Fixed JSON Patch ops that operate on mappings and have a target that looks like an int. We now ensure the target is a string. See [#82](https://github.com/jg-rp/python-jsonpath/pull/82).

## Version 1.3.0

**Fixes**
Expand Down
2 changes: 1 addition & 1 deletion jsonpath/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .path import JSONPath
from .selectors import FilterContext

# ruff: noqa: D102
# ruff: noqa: D102, PLW1641


class FilterExpression(ABC):
Expand Down
3 changes: 2 additions & 1 deletion jsonpath/function_extensions/arguments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Class-based function extension base."""

import inspect
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -26,7 +27,7 @@ def validate(
params = list(inspect.signature(func).parameters.values())

# Keyword only params are not supported
if len([p for p in params if p.kind in (p.KEYWORD_ONLY, p.VAR_KEYWORD)]):
if [p for p in params if p.kind in (p.KEYWORD_ONLY, p.VAR_KEYWORD)]:
raise JSONPathTypeError(
f"function {token.value!r} requires keyword arguments",
token=token,
Expand Down
12 changes: 6 additions & 6 deletions jsonpath/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def apply(
else:
parent.insert(int(target), self.value)
elif isinstance(parent, MutableMapping):
parent[target] = self.value
parent[str(target)] = self.value
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
Expand Down Expand Up @@ -183,7 +183,7 @@ def apply(
elif isinstance(parent, MutableMapping):
if obj is UNDEFINED:
raise JSONPatchError("can't remove nonexistent property")
del parent[self.path.parts[-1]]
del parent[str(self.path.parts[-1])]
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
Expand Down Expand Up @@ -221,7 +221,7 @@ def apply(
elif isinstance(parent, MutableMapping):
if obj is UNDEFINED:
raise JSONPatchError("can't replace nonexistent property")
parent[self.path.parts[-1]] = self.value
parent[str(self.path.parts[-1])] = self.value
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
Expand Down Expand Up @@ -259,7 +259,7 @@ def apply(
if isinstance(source_parent, MutableSequence):
del source_parent[int(self.source.parts[-1])]
if isinstance(source_parent, MutableMapping):
del source_parent[self.source.parts[-1]]
del source_parent[str(self.source.parts[-1])]

dest_parent, _ = self.dest.resolve_parent(data)

Expand All @@ -270,7 +270,7 @@ def apply(
if isinstance(dest_parent, MutableSequence):
dest_parent.insert(int(self.dest.parts[-1]), source_obj)
elif isinstance(dest_parent, MutableMapping):
dest_parent[self.dest.parts[-1]] = source_obj
dest_parent[str(self.dest.parts[-1])] = source_obj
else:
raise JSONPatchError(
f"unexpected operation on {dest_parent.__class__.__name__!r}"
Expand Down Expand Up @@ -312,7 +312,7 @@ def apply(
if isinstance(dest_parent, MutableSequence):
dest_parent.insert(int(self.dest.parts[-1]), copy.deepcopy(source_obj))
elif isinstance(dest_parent, MutableMapping):
dest_parent[self.dest.parts[-1]] = copy.deepcopy(source_obj)
dest_parent[str(self.dest.parts[-1])] = copy.deepcopy(source_obj)
else:
raise JSONPatchError(
f"unexpected operation on {dest_parent.__class__.__name__!r}"
Expand Down
3 changes: 3 additions & 0 deletions jsonpath/pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ def __str__(self) -> str:
def __eq__(self, __value: object) -> bool:
return isinstance(__value, RelativeJSONPointer) and str(self) == str(__value)

def __hash__(self) -> int:
return hash((self.origin, self.index, self.pointer))

def _parse(
self,
rel: str,
Expand Down
5 changes: 5 additions & 0 deletions tests/test_json_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,8 @@ def test_non_standard_addap_op() -> None:
# Index 7 is out of range and would raises a JSONPatchError with the `add` op.
patch = JSONPatch().addap(path="/foo/7", value=99)
assert patch.apply({"foo": [1, 2, 3]}) == {"foo": [1, 2, 3, 99]}


def test_add_to_mapping_with_int_key() -> None:
patch = JSONPatch().add(path="/1", value=99)
assert patch.apply({"foo": 1}) == {"foo": 1, "1": 99}