pydantic-json-patch 2.0.1__tar.gz → 2.1.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pydantic_json_patch
3
- Version: 2.0.1
3
+ Version: 2.1.0
4
4
  Summary: Pydantic models for implementing JSON Patch.
5
5
  Author: Jonathan Sharpe
6
6
  Author-email: Jonathan Sharpe <mail@jonrshar.pe>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pydantic_json_patch"
3
- version = "2.0.1"
3
+ version = "2.1.0"
4
4
  description = "Pydantic models for implementing JSON Patch."
5
5
  readme = "README.md"
6
6
  authors = [
@@ -16,6 +16,7 @@ from pydantic import (
16
16
  model_validator,
17
17
  )
18
18
 
19
+ _CACHE_SIZE = 1_024 # pragma: no mutate
19
20
  _JSON_POINTER = re.compile(r"^(?:/(?:[^/~]|~[01])*)*$")
20
21
 
21
22
  T = tx.TypeVar("T", default=tp.Any)
@@ -32,6 +33,16 @@ def _generate_title(model: type[tp.Any]) -> str:
32
33
  return "".join(("JsonPatch", name, "eration", *rest))
33
34
 
34
35
 
36
+ @lru_cache(maxsize=_CACHE_SIZE)
37
+ def _decode_token(token: str) -> str:
38
+ return token.replace("~1", "/").replace("~0", "~")
39
+
40
+
41
+ @lru_cache(maxsize=_CACHE_SIZE)
42
+ def _encode_token(token: str) -> str:
43
+ return token.replace("~", "~0").replace("/", "~1")
44
+
45
+
35
46
  class _BaseOp(BaseModel):
36
47
  model_config = ConfigDict(
37
48
  frozen=True,
@@ -60,22 +71,12 @@ class _BaseOp(BaseModel):
60
71
  def _dump_pointer(cls, pointer: Sequence[str]) -> str:
61
72
  if isinstance(pointer, str):
62
73
  return pointer
63
- return "/".join(["", *(cls._encode_token(token) for token in pointer)])
74
+ return "/".join(["", *(_encode_token(token) for token in pointer)])
64
75
 
65
76
  @staticmethod
66
- @lru_cache
67
- def _decode_token(token: str) -> str:
68
- return token.replace("~1", "/").replace("~0", "~")
69
-
70
- @staticmethod
71
- @lru_cache
72
- def _encode_token(token: str) -> str:
73
- return token.replace("~", "~0").replace("/", "~1")
74
-
75
- @classmethod
76
- @lru_cache
77
- def _load_pointer(cls, pointer: str) -> tuple[str, ...]:
78
- return tuple(cls._decode_token(token) for token in pointer.split("/")[1:])
77
+ @lru_cache(maxsize=_CACHE_SIZE)
78
+ def _load_pointer(pointer: str) -> tuple[str, ...]:
79
+ return tuple(_decode_token(token) for token in pointer.split("/")[1:])
79
80
 
80
81
 
81
82
  class _FromOp(_BaseOp):
@@ -223,7 +224,9 @@ class JsonPatch(RootModel[Sequence[Operation]], Sequence[Operation]):
223
224
  @model_validator(mode="before")
224
225
  @classmethod
225
226
  def _coerce_seq_to_tuple(cls, value: tp.Any) -> tp.Any: # noqa: ANN401
226
- if isinstance(value, Sequence) and not isinstance(value, tuple):
227
+ if isinstance(value, tuple):
228
+ return value
229
+ if isinstance(value, Sequence):
227
230
  return tuple(value)
228
231
  return value
229
232