Skip to content

Commit 6186ed0

Browse files
authored
Merge pull request #82 from jg-rp/fix-lint
Fix lint checks and patch targets that look like ints
2 parents 2223a85 + 1fe8aa8 commit 6186ed0

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Python JSONPath Change Log
22

3+
## Version 1.3.1 (unreleased)
4+
5+
**Fixes**
6+
7+
- 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).
8+
- 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).
9+
310
## Version 1.3.0
411

512
**Fixes**

jsonpath/filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .path import JSONPath
3131
from .selectors import FilterContext
3232

33-
# ruff: noqa: D102
33+
# ruff: noqa: D102, PLW1641
3434

3535

3636
class FilterExpression(ABC):

jsonpath/function_extensions/arguments.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Class-based function extension base."""
2+
23
import inspect
34
from typing import TYPE_CHECKING
45
from typing import Any
@@ -26,7 +27,7 @@ def validate(
2627
params = list(inspect.signature(func).parameters.values())
2728

2829
# Keyword only params are not supported
29-
if len([p for p in params if p.kind in (p.KEYWORD_ONLY, p.VAR_KEYWORD)]):
30+
if [p for p in params if p.kind in (p.KEYWORD_ONLY, p.VAR_KEYWORD)]:
3031
raise JSONPathTypeError(
3132
f"function {token.value!r} requires keyword arguments",
3233
token=token,

jsonpath/patch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def apply(
7474
else:
7575
parent.insert(int(target), self.value)
7676
elif isinstance(parent, MutableMapping):
77-
parent[target] = self.value
77+
parent[str(target)] = self.value
7878
else:
7979
raise JSONPatchError(
8080
f"unexpected operation on {parent.__class__.__name__!r}"
@@ -183,7 +183,7 @@ def apply(
183183
elif isinstance(parent, MutableMapping):
184184
if obj is UNDEFINED:
185185
raise JSONPatchError("can't remove nonexistent property")
186-
del parent[self.path.parts[-1]]
186+
del parent[str(self.path.parts[-1])]
187187
else:
188188
raise JSONPatchError(
189189
f"unexpected operation on {parent.__class__.__name__!r}"
@@ -221,7 +221,7 @@ def apply(
221221
elif isinstance(parent, MutableMapping):
222222
if obj is UNDEFINED:
223223
raise JSONPatchError("can't replace nonexistent property")
224-
parent[self.path.parts[-1]] = self.value
224+
parent[str(self.path.parts[-1])] = self.value
225225
else:
226226
raise JSONPatchError(
227227
f"unexpected operation on {parent.__class__.__name__!r}"
@@ -259,7 +259,7 @@ def apply(
259259
if isinstance(source_parent, MutableSequence):
260260
del source_parent[int(self.source.parts[-1])]
261261
if isinstance(source_parent, MutableMapping):
262-
del source_parent[self.source.parts[-1]]
262+
del source_parent[str(self.source.parts[-1])]
263263

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

@@ -270,7 +270,7 @@ def apply(
270270
if isinstance(dest_parent, MutableSequence):
271271
dest_parent.insert(int(self.dest.parts[-1]), source_obj)
272272
elif isinstance(dest_parent, MutableMapping):
273-
dest_parent[self.dest.parts[-1]] = source_obj
273+
dest_parent[str(self.dest.parts[-1])] = source_obj
274274
else:
275275
raise JSONPatchError(
276276
f"unexpected operation on {dest_parent.__class__.__name__!r}"
@@ -312,7 +312,7 @@ def apply(
312312
if isinstance(dest_parent, MutableSequence):
313313
dest_parent.insert(int(self.dest.parts[-1]), copy.deepcopy(source_obj))
314314
elif isinstance(dest_parent, MutableMapping):
315-
dest_parent[self.dest.parts[-1]] = copy.deepcopy(source_obj)
315+
dest_parent[str(self.dest.parts[-1])] = copy.deepcopy(source_obj)
316316
else:
317317
raise JSONPatchError(
318318
f"unexpected operation on {dest_parent.__class__.__name__!r}"

jsonpath/pointer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ def __str__(self) -> str:
485485
def __eq__(self, __value: object) -> bool:
486486
return isinstance(__value, RelativeJSONPointer) and str(self) == str(__value)
487487

488+
def __hash__(self) -> int:
489+
return hash((self.origin, self.index, self.pointer))
490+
488491
def _parse(
489492
self,
490493
rel: str,

tests/test_json_patch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,8 @@ def test_non_standard_addap_op() -> None:
268268
# Index 7 is out of range and would raises a JSONPatchError with the `add` op.
269269
patch = JSONPatch().addap(path="/foo/7", value=99)
270270
assert patch.apply({"foo": [1, 2, 3]}) == {"foo": [1, 2, 3, 99]}
271+
272+
273+
def test_add_to_mapping_with_int_key() -> None:
274+
patch = JSONPatch().add(path="/1", value=99)
275+
assert patch.apply({"foo": 1}) == {"foo": 1, "1": 99}

0 commit comments

Comments
 (0)