jsonpatch 1.40__tar.gz → 1.41__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. {jsonpatch-1.40 → jsonpatch-1.41}/MANIFEST.in +1 -0
  2. {jsonpatch-1.40/jsonpatch.egg-info → jsonpatch-1.41}/PKG-INFO +17 -2
  3. {jsonpatch-1.40 → jsonpatch-1.41}/README.md +14 -0
  4. jsonpatch-1.41/jsonpatch-stubs/__init__.pyi +146 -0
  5. {jsonpatch-1.40 → jsonpatch-1.41/jsonpatch.egg-info}/PKG-INFO +17 -2
  6. {jsonpatch-1.40 → jsonpatch-1.41}/jsonpatch.egg-info/SOURCES.txt +3 -1
  7. jsonpatch-1.41/jsonpatch.egg-info/requires.txt +1 -0
  8. {jsonpatch-1.40 → jsonpatch-1.41}/jsonpatch.egg-info/top_level.txt +1 -0
  9. {jsonpatch-1.40 → jsonpatch-1.41}/jsonpatch.py +1 -1
  10. {jsonpatch-1.40 → jsonpatch-1.41}/pyproject.toml +3 -0
  11. jsonpatch-1.41/requirements.txt +1 -0
  12. {jsonpatch-1.40 → jsonpatch-1.41}/setup.py +5 -1
  13. jsonpatch-1.41/typetests/usage.py +103 -0
  14. jsonpatch-1.40/jsonpatch.egg-info/requires.txt +0 -1
  15. jsonpatch-1.40/requirements.txt +0 -1
  16. {jsonpatch-1.40 → jsonpatch-1.41}/AUTHORS +0 -0
  17. {jsonpatch-1.40 → jsonpatch-1.41}/LICENSE +0 -0
  18. {jsonpatch-1.40 → jsonpatch-1.41}/doc/commandline.rst +0 -0
  19. {jsonpatch-1.40 → jsonpatch-1.41}/doc/index.rst +0 -0
  20. {jsonpatch-1.40 → jsonpatch-1.41}/doc/mod-jsonpatch.rst +0 -0
  21. {jsonpatch-1.40 → jsonpatch-1.41}/doc/tutorial.rst +0 -0
  22. {jsonpatch-1.40 → jsonpatch-1.41}/ext_tests.py +0 -0
  23. {jsonpatch-1.40 → jsonpatch-1.41}/jsonpatch.egg-info/dependency_links.txt +0 -0
  24. {jsonpatch-1.40 → jsonpatch-1.41}/jsonpatch.egg-info/entry_points.txt +0 -0
  25. {jsonpatch-1.40 → jsonpatch-1.41}/jsonpatch_cli.py +0 -0
  26. {jsonpatch-1.40 → jsonpatch-1.41}/property_tests.py +0 -0
  27. {jsonpatch-1.40 → jsonpatch-1.41}/pytest.ini +0 -0
  28. {jsonpatch-1.40 → jsonpatch-1.41}/setup.cfg +0 -0
  29. {jsonpatch-1.40 → jsonpatch-1.41}/tests.js +0 -0
  30. {jsonpatch-1.40 → jsonpatch-1.41}/tests.py +0 -0
@@ -6,5 +6,6 @@ include doc/*.rst
6
6
  include property_tests.py
7
7
  include pytest.ini
8
8
  include ext_tests.py
9
+ include typetests/usage.py
9
10
  include AUTHORS
10
11
  include tests.js
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpatch
3
- Version: 1.40
3
+ Version: 1.41
4
4
  Summary: Apply JSON-Patches (RFC 6902)
5
5
  Home-page: https://github.com/stefankoegl/python-json-patch
6
6
  Author: Stefan Kögl
@@ -28,11 +28,12 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
28
28
  Classifier: Programming Language :: Python :: Implementation :: PyPy
29
29
  Classifier: Topic :: Software Development :: Libraries
30
30
  Classifier: Topic :: Utilities
31
+ Classifier: Typing :: Typed
31
32
  Requires-Python: >=3.10
32
33
  Description-Content-Type: text/markdown
33
34
  License-File: LICENSE
34
35
  License-File: AUTHORS
35
- Requires-Dist: jsonpointer>=3.2
36
+ Requires-Dist: jsonpointer>=3.8
36
37
  Dynamic: author
37
38
  Dynamic: author-email
38
39
  Dynamic: classifier
@@ -69,6 +70,20 @@ See source code for examples
69
70
  * CI: https://github.com/stefankoegl/python-json-patch/actions
70
71
  * Coveralls: https://coveralls.io/github/stefankoegl/python-json-patch
71
72
 
73
+ Type hints
74
+ ----------
75
+ jsonpatch ships type stubs (in the `jsonpatch-stubs` directory, as
76
+ jsonpatch.py is a single-file module), so type checkers such as mypy and
77
+ pyright work with it without further setup. JSON documents are typed as `Any`.
78
+
79
+ When changing the public API of jsonpatch.py, update
80
+ `jsonpatch-stubs/__init__.pyi` as well. These check that both agree, and that
81
+ typical usage in `typetests/usage.py` type-checks:
82
+
83
+ pip install mypy .
84
+ python -m mypy.stubtest --mypy-config-file pyproject.toml jsonpatch
85
+ cd typetests && mypy --config-file ../pyproject.toml usage.py
86
+
72
87
  Running external tests
73
88
  ----------------------
74
89
  To run external tests (such as those from https://github.com/json-patch/json-patch-tests) use ext_tests.py
@@ -21,6 +21,20 @@ See source code for examples
21
21
  * CI: https://github.com/stefankoegl/python-json-patch/actions
22
22
  * Coveralls: https://coveralls.io/github/stefankoegl/python-json-patch
23
23
 
24
+ Type hints
25
+ ----------
26
+ jsonpatch ships type stubs (in the `jsonpatch-stubs` directory, as
27
+ jsonpatch.py is a single-file module), so type checkers such as mypy and
28
+ pyright work with it without further setup. JSON documents are typed as `Any`.
29
+
30
+ When changing the public API of jsonpatch.py, update
31
+ `jsonpatch-stubs/__init__.pyi` as well. These check that both agree, and that
32
+ typical usage in `typetests/usage.py` type-checks:
33
+
34
+ pip install mypy .
35
+ python -m mypy.stubtest --mypy-config-file pyproject.toml jsonpatch
36
+ cd typetests && mypy --config-file ../pyproject.toml usage.py
37
+
24
38
  Running external tests
25
39
  ----------------------
26
40
  To run external tests (such as those from https://github.com/json-patch/json-patch-tests) use ext_tests.py
@@ -0,0 +1,146 @@
1
+ # Type stubs for jsonpatch.
2
+ #
3
+ # jsonpatch is a single-file module, and PEP 561 has no py.typed marker for
4
+ # those, so its types are shipped as this stub-only package in the same
5
+ # distribution. Keep it in sync with jsonpatch.py; CI runs mypy's stubtest to
6
+ # check that both agree.
7
+ #
8
+ # JSON documents are typed as Any: they can be any JSON value, and the module
9
+ # also accepts arbitrary Mapping / Sequence types, which a recursive JSON type
10
+ # would reject.
11
+
12
+ from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
13
+ from typing import Any, ClassVar
14
+
15
+ # Re-exported, as jsonpatch.py imports them and users rely on that, e.g. to
16
+ # catch jsonpatch.JsonPointerException. stubtest does not check these, as
17
+ # they are defined in another module.
18
+ from jsonpointer import JsonPointer as JsonPointer
19
+ from jsonpointer import JsonPointerException as JsonPointerException
20
+ from typing_extensions import Self
21
+
22
+ __author__: str
23
+ __version__: str
24
+ __website__: str
25
+ __license__: str
26
+
27
+ # A single patch operation, e.g. {'op': 'add', 'path': '/a', 'value': 1}
28
+ _Operation = Mapping[str, Any]
29
+
30
+ class JsonPatchException(Exception):
31
+ # The failing operation: a PatchOperation, or the raw operation object if
32
+ # none could be constructed from it, or None.
33
+ op: Any
34
+ def __init__(self, *args: object, op: Any = None) -> None: ...
35
+
36
+ class InvalidJsonPatch(JsonPatchException): ...
37
+ class JsonPatchConflict(JsonPatchException): ...
38
+ class JsonPatchTestFailed(JsonPatchException, AssertionError): ...
39
+
40
+ def multidict(ordered_pairs: Iterable[tuple[str, Any]]) -> dict[str, Any]: ...
41
+ def apply_patch(
42
+ doc: Any,
43
+ patch: str | bytes | Iterable[_Operation],
44
+ in_place: bool = False,
45
+ pointer_cls: type[JsonPointer] = ...,
46
+ ) -> Any: ...
47
+ def make_patch(
48
+ src: Any,
49
+ dst: Any,
50
+ pointer_cls: type[JsonPointer] = ...,
51
+ generate_test_ops: bool = False,
52
+ ) -> JsonPatch: ...
53
+
54
+ class PatchOperation:
55
+ pointer_cls: type[JsonPointer]
56
+ location: str
57
+ pointer: JsonPointer
58
+ operation: _Operation
59
+ def __init__(self, operation: _Operation, pointer_cls: type[JsonPointer] = ...) -> None: ...
60
+ def apply(self, obj: Any) -> Any: ...
61
+ def __hash__(self) -> int: ...
62
+ def __eq__(self, other: object) -> bool: ...
63
+ def __ne__(self, other: object) -> bool: ...
64
+ @property
65
+ def path(self) -> str: ...
66
+ @property
67
+ def key(self) -> int | str: ...
68
+ @key.setter
69
+ def key(self, value: int | str) -> None: ...
70
+ def get_part(self, index: int) -> int | str: ...
71
+ def set_part(self, index: int, value: int | str) -> None: ...
72
+
73
+ class RemoveOperation(PatchOperation): ...
74
+ class AddOperation(PatchOperation): ...
75
+ class ReplaceOperation(PatchOperation): ...
76
+
77
+ class MoveOperation(PatchOperation):
78
+ @property
79
+ def from_path(self) -> str: ...
80
+ @property
81
+ def from_key(self) -> int | str: ...
82
+ @from_key.setter
83
+ def from_key(self, value: int | str) -> None: ...
84
+ def get_from_part(self, index: int) -> int | str: ...
85
+ def set_from_part(self, index: int, value: int | str) -> None: ...
86
+
87
+ class TestOperation(PatchOperation): ...
88
+ class CopyOperation(PatchOperation): ...
89
+
90
+ class JsonPatch:
91
+ json_dumper: ClassVar[Callable[..., str]]
92
+ json_loader: ClassVar[Callable[..., Any]]
93
+ operations: ClassVar[Mapping[str, type[PatchOperation]]]
94
+ pointer_cls: type[JsonPointer]
95
+ patch: Sequence[_Operation]
96
+ def __init__(self, patch: Iterable[_Operation], pointer_cls: type[JsonPointer] = ...) -> None: ...
97
+ def __bool__(self) -> bool: ...
98
+ def __iter__(self) -> Iterator[_Operation]: ...
99
+ def __hash__(self) -> int: ...
100
+ def __eq__(self, other: object) -> bool: ...
101
+ def __ne__(self, other: object) -> bool: ...
102
+ __nonzero__ = __bool__
103
+ @classmethod
104
+ def from_string(
105
+ cls,
106
+ patch_str: str | bytes | bytearray,
107
+ loads: Callable[[Any], Any] | None = None,
108
+ pointer_cls: type[JsonPointer] = ...,
109
+ ) -> Self: ...
110
+ @classmethod
111
+ def from_diff(
112
+ cls,
113
+ src: Any,
114
+ dst: Any,
115
+ optimization: bool = True,
116
+ dumps: Callable[[Any], str] | None = None,
117
+ pointer_cls: type[JsonPointer] = ...,
118
+ generate_test_ops: bool = False,
119
+ ) -> Self: ...
120
+ def to_string(self, dumps: Callable[[Any], str] | None = None) -> str: ...
121
+ def apply(self, obj: Any, in_place: bool = False) -> Any: ...
122
+
123
+ # Computes the operations for JsonPatch.from_diff; mostly an implementation
124
+ # detail, so its internal methods are typed loosely.
125
+ class DiffBuilder:
126
+ dumps: Callable[[Any], str]
127
+ pointer_cls: type[JsonPointer]
128
+ optimization: bool
129
+ generate_test_ops: bool
130
+ src_doc: Any
131
+ dst_doc: Any
132
+ def __init__(
133
+ self,
134
+ src_doc: Any,
135
+ dst_doc: Any,
136
+ dumps: Callable[[Any], str] = ...,
137
+ pointer_cls: type[JsonPointer] = ...,
138
+ optimization: bool = True,
139
+ generate_test_ops: bool = False,
140
+ ) -> None: ...
141
+ def store_index(self, value: Any, index: Any, st: int) -> None: ...
142
+ def take_index(self, value: Any, st: int) -> Any: ...
143
+ def insert(self, op: Any) -> Any: ...
144
+ def remove(self, index: Any) -> None: ...
145
+ def __iter__(self) -> Iterator[Any]: ...
146
+ def execute(self) -> Iterator[dict[str, Any]]: ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpatch
3
- Version: 1.40
3
+ Version: 1.41
4
4
  Summary: Apply JSON-Patches (RFC 6902)
5
5
  Home-page: https://github.com/stefankoegl/python-json-patch
6
6
  Author: Stefan Kögl
@@ -28,11 +28,12 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
28
28
  Classifier: Programming Language :: Python :: Implementation :: PyPy
29
29
  Classifier: Topic :: Software Development :: Libraries
30
30
  Classifier: Topic :: Utilities
31
+ Classifier: Typing :: Typed
31
32
  Requires-Python: >=3.10
32
33
  Description-Content-Type: text/markdown
33
34
  License-File: LICENSE
34
35
  License-File: AUTHORS
35
- Requires-Dist: jsonpointer>=3.2
36
+ Requires-Dist: jsonpointer>=3.8
36
37
  Dynamic: author
37
38
  Dynamic: author-email
38
39
  Dynamic: classifier
@@ -69,6 +70,20 @@ See source code for examples
69
70
  * CI: https://github.com/stefankoegl/python-json-patch/actions
70
71
  * Coveralls: https://coveralls.io/github/stefankoegl/python-json-patch
71
72
 
73
+ Type hints
74
+ ----------
75
+ jsonpatch ships type stubs (in the `jsonpatch-stubs` directory, as
76
+ jsonpatch.py is a single-file module), so type checkers such as mypy and
77
+ pyright work with it without further setup. JSON documents are typed as `Any`.
78
+
79
+ When changing the public API of jsonpatch.py, update
80
+ `jsonpatch-stubs/__init__.pyi` as well. These check that both agree, and that
81
+ typical usage in `typetests/usage.py` type-checks:
82
+
83
+ pip install mypy .
84
+ python -m mypy.stubtest --mypy-config-file pyproject.toml jsonpatch
85
+ cd typetests && mypy --config-file ../pyproject.toml usage.py
86
+
72
87
  Running external tests
73
88
  ----------------------
74
89
  To run external tests (such as those from https://github.com/json-patch/json-patch-tests) use ext_tests.py
@@ -16,9 +16,11 @@ doc/commandline.rst
16
16
  doc/index.rst
17
17
  doc/mod-jsonpatch.rst
18
18
  doc/tutorial.rst
19
+ jsonpatch-stubs/__init__.pyi
19
20
  jsonpatch.egg-info/PKG-INFO
20
21
  jsonpatch.egg-info/SOURCES.txt
21
22
  jsonpatch.egg-info/dependency_links.txt
22
23
  jsonpatch.egg-info/entry_points.txt
23
24
  jsonpatch.egg-info/requires.txt
24
- jsonpatch.egg-info/top_level.txt
25
+ jsonpatch.egg-info/top_level.txt
26
+ typetests/usage.py
@@ -0,0 +1 @@
1
+ jsonpointer>=3.8
@@ -1,2 +1,3 @@
1
1
  jsonpatch
2
+ jsonpatch-stubs
2
3
  jsonpatch_cli
@@ -55,7 +55,7 @@ _EXACT_MATCH_LIMIT = 500 ** 2
55
55
 
56
56
  # Will be parsed by setup.py to determine package metadata
57
57
  __author__ = 'Stefan Kögl <stefan@skoegl.net>'
58
- __version__ = '1.40'
58
+ __version__ = '1.41'
59
59
  __website__ = 'https://github.com/stefankoegl/python-json-patch'
60
60
  __license__ = 'BSD-3-Clause'
61
61
 
@@ -2,3 +2,6 @@
2
2
  # license_expression in setup.py requires setuptools >= 77
3
3
  requires = ["setuptools>=77"]
4
4
  build-backend = "setuptools.build_meta"
5
+
6
+ [tool.mypy]
7
+ strict = true
@@ -0,0 +1 @@
1
+ jsonpointer>=3.8
@@ -43,6 +43,7 @@ CLASSIFIERS = [
43
43
  'Programming Language :: Python :: Implementation :: PyPy',
44
44
  'Topic :: Software Development :: Libraries',
45
45
  'Topic :: Utilities',
46
+ 'Typing :: Typed',
46
47
  ]
47
48
 
48
49
 
@@ -56,7 +57,10 @@ setup(name=PACKAGE,
56
57
  license_expression=LICENSE,
57
58
  url=WEBSITE,
58
59
  py_modules=MODULES,
59
- package_data={'': ['requirements.txt']},
60
+ # jsonpatch is a single-file module, which can't be marked as typed
61
+ # with py.typed (PEP 561), so its type stubs are a stub-only package
62
+ packages=['jsonpatch-stubs'],
63
+ package_data={'': ['requirements.txt'], 'jsonpatch-stubs': ['*.pyi']},
60
64
  entry_points={
61
65
  'console_scripts': [
62
66
  'jsondiff = jsonpatch_cli:jsondiff_main',
@@ -0,0 +1,103 @@
1
+ """ Type checks of typical jsonpatch usage, as seen by a user's type checker.
2
+
3
+ This file is not run; CI type-checks it with ``mypy --strict`` against the
4
+ installed distribution, run from this directory so that the stubs in
5
+ jsonpatch-stubs are used rather than jsonpatch.py itself. Lines marked with
6
+ ``# type: ignore[...]`` must produce exactly that error (strict mode reports
7
+ unused ignores), so they test that wrong usage is rejected.
8
+ """
9
+
10
+ import json
11
+ from collections.abc import Mapping
12
+ from typing import Any
13
+
14
+ import jsonpointer
15
+ from jsonpointer import JsonPointer
16
+ from typing_extensions import assert_type
17
+
18
+ import jsonpatch
19
+ from jsonpatch import (InvalidJsonPatch, JsonPatch, JsonPatchConflict,
20
+ JsonPatchException, JsonPatchTestFailed,
21
+ PatchOperation)
22
+
23
+ doc = {'foo': 'bar'}
24
+ ops = [{'op': 'add', 'path': '/baz', 'value': 'qux'}]
25
+
26
+ # module-level functions
27
+ assert_type(jsonpatch.apply_patch(doc, ops), Any)
28
+ assert_type(jsonpatch.apply_patch(doc, ops, in_place=True), Any)
29
+ assert_type(jsonpatch.apply_patch(doc, json.dumps(ops)), Any)
30
+ assert_type(jsonpatch.apply_patch(doc, json.dumps(ops).encode()), Any)
31
+ assert_type(jsonpatch.make_patch(doc, {}), JsonPatch)
32
+ assert_type(jsonpatch.make_patch(doc, {}, generate_test_ops=True), JsonPatch)
33
+
34
+ # values of dicts with narrow types are accepted (Mapping is covariant)
35
+ str_ops: list[dict[str, str]] = [{'op': 'remove', 'path': '/foo'}]
36
+ jsonpatch.apply_patch(doc, str_ops)
37
+
38
+ # JsonPatch
39
+ patch = JsonPatch(ops)
40
+ JsonPatch(op for op in ops)
41
+ assert_type(patch.apply(doc), Any)
42
+ assert_type(patch.to_string(), str)
43
+ assert_type(patch.to_string(dumps=json.dumps), str)
44
+ assert_type(bool(patch), bool)
45
+ assert_type(list(patch), list[Mapping[str, Any]])
46
+ assert_type(JsonPatch.from_string('[]'), JsonPatch)
47
+ assert_type(JsonPatch.from_string(b'[]', loads=json.loads), JsonPatch)
48
+ assert_type(JsonPatch.from_string(bytearray(b'[]')), JsonPatch)
49
+ assert_type(JsonPatch.from_diff(doc, {}, optimization=False), JsonPatch)
50
+ assert_type(JsonPatch.operations['add'], type[PatchOperation])
51
+
52
+
53
+ class MyPatch(JsonPatch):
54
+ pass
55
+
56
+
57
+ # the alternative constructors return instances of subclasses
58
+ assert_type(MyPatch.from_string('[]'), MyPatch)
59
+ assert_type(MyPatch.from_diff(doc, {}), MyPatch)
60
+
61
+ # operations
62
+ operation = jsonpatch.AddOperation({'op': 'add', 'path': '/a/0', 'value': 1})
63
+ assert_type(operation.apply({'a': []}), Any)
64
+ assert_type(operation.location, str)
65
+ assert_type(operation.path, str)
66
+ assert_type(operation.key, int | str)
67
+ operation.key = 1
68
+ assert_type(operation.pointer, JsonPointer)
69
+ move = jsonpatch.MoveOperation({'op': 'move', 'from': '/a', 'path': '/b'})
70
+ assert_type(move.from_key, int | str)
71
+
72
+
73
+ # a custom pointer class
74
+ class MyPointer(JsonPointer):
75
+ pass
76
+
77
+
78
+ jsonpatch.apply_patch(doc, ops, pointer_cls=MyPointer)
79
+ JsonPatch(ops, pointer_cls=MyPointer)
80
+
81
+ # exceptions
82
+ try:
83
+ patch.apply(doc)
84
+ except (InvalidJsonPatch, JsonPatchConflict, JsonPatchTestFailed) as ex:
85
+ assert_type(ex, InvalidJsonPatch | JsonPatchConflict | JsonPatchTestFailed)
86
+ failed = ex.op
87
+ JsonPatchException('message', op=None)
88
+
89
+ # names that jsonpatch re-exports from jsonpointer
90
+ try:
91
+ patch.apply(doc)
92
+ except jsonpatch.JsonPointerException as ex:
93
+ assert_type(ex, jsonpointer.JsonPointerException)
94
+ assert_type(jsonpatch.JsonPointer('/a'), jsonpointer.JsonPointer)
95
+
96
+ # wrong usage is rejected
97
+ jsonpatch.apply_patch(doc, ops, in_place='yes') # type: ignore[arg-type]
98
+ jsonpatch.make_patch(doc) # type: ignore[call-arg]
99
+ JsonPatch(ops).to_string(dumps=len) # type: ignore[arg-type]
100
+ JsonPatch([1, 2]) # type: ignore[list-item]
101
+ JsonPatch(ops, pointer_cls=str) # type: ignore[arg-type]
102
+ # apply_patch treats a bytearray as a sequence of operations, and fails
103
+ jsonpatch.apply_patch(doc, bytearray(b'[]')) # type: ignore[arg-type]
@@ -1 +0,0 @@
1
- jsonpointer>=3.2
@@ -1 +0,0 @@
1
- jsonpointer>=3.2
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes