query-cascade 0.3.24__tar.gz → 0.3.26__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.
- {query_cascade-0.3.24 → query_cascade-0.3.26}/PKG-INFO +2 -4
- {query_cascade-0.3.24 → query_cascade-0.3.26}/README.md +1 -1
- {query_cascade-0.3.24 → query_cascade-0.3.26}/pyproject.toml +1 -3
- query_cascade-0.3.26/src/cascade/_canonical.py +327 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_disk_cache.py +25 -268
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_evaluator.py +297 -84
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_persistence.py +23 -46
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_serde.py +20 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_state.py +18 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_store.py +170 -49
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/engine.py +13 -5
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/query_cascade.egg-info/PKG-INFO +2 -4
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/query_cascade.egg-info/SOURCES.txt +4 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/query_cascade.egg-info/requires.txt +0 -2
- query_cascade-0.3.26/tests/test_dirty_propagation.py +555 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_disk_cache.py +23 -7
- query_cascade-0.3.26/tests/test_disk_format2.py +325 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_internal_invariants.py +6 -5
- query_cascade-0.3.26/tests/test_iterative_verification.py +175 -0
- query_cascade-0.3.26/tests/test_overhead_optimizations.py +302 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_persistence_permutations.py +15 -30
- query_cascade-0.3.24/src/cascade/_canonical.py +0 -258
- {query_cascade-0.3.24 → query_cascade-0.3.26}/setup.cfg +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/__init__.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_ast_rewrite.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_collections.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_errors.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_graph_export.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_incremental.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_runtime.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_scheduler.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/cascade/_synthetic_graph.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/query_cascade.egg-info/dependency_links.txt +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/src/query_cascade.egg-info/top_level.txt +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_ast_rewrite.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_async.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_async_coverage.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_code_versioning.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_datetime.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_dependency_parallelism.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine_compute_many.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine_concurrency.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine_persistence_and_tracing.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine_private.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine_private2.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_engine_sweep.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_error_caching.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_error_caching_persistence.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_evaluator_and_synthetic_coverage.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_examples.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_fixed_point.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_incremental_collections.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_incremental_graph.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_incremental_persistence.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_incremental_pipeline.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_memoize_false.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_performance.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_repro_touch_memo_keyerror.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_scale_behavior.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_serde.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_stateful_engine_invariants.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_store_coverage.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_subgraph_stats_export.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_sweep_unaccessed.py +0 -0
- {query_cascade-0.3.24 → query_cascade-0.3.26}/tests/test_ttl.py +0 -0
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: query-cascade
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.26
|
|
4
4
|
Summary: Minimal demand-driven query framework for incremental computation.
|
|
5
5
|
Requires-Python: >=3.12
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
7
7
|
Provides-Extra: disk
|
|
8
8
|
Requires-Dist: libmdbx; extra == "disk"
|
|
9
|
-
Requires-Dist: lmdb; extra == "disk"
|
|
10
9
|
Requires-Dist: msgpack; extra == "disk"
|
|
11
10
|
Provides-Extra: dev
|
|
12
11
|
Requires-Dist: hypothesis; extra == "dev"
|
|
13
12
|
Requires-Dist: libmdbx; extra == "dev"
|
|
14
|
-
Requires-Dist: lmdb; extra == "dev"
|
|
15
13
|
Requires-Dist: msgpack; extra == "dev"
|
|
16
14
|
Requires-Dist: mutmut; extra == "dev"
|
|
17
15
|
Requires-Dist: pytest; extra == "dev"
|
|
@@ -91,7 +89,7 @@ The `Engine` manages caching, execution, and state tracking.
|
|
|
91
89
|
* **`Engine(*, max_entries=10000, trace_limit=50000, stats=False, cache_dir=None, cache_map_size=2**30, cache_backend="mdbx", incremental=True)`**: Initializes the engine.
|
|
92
90
|
* `max_entries` sets the limit for the Least Recently Used (LRU) in-memory cache.
|
|
93
91
|
* Passing `cache_dir` enables persistent disk caching.
|
|
94
|
-
* `cache_backend` allows you to select the storage engine for persistence (`"mdbx"
|
|
92
|
+
* `cache_backend` allows you to select the storage engine for persistence (`"mdbx"` or `"sqlite"`).
|
|
95
93
|
|
|
96
94
|
### Defining Nodes: `@engine.input` and `@engine.query`
|
|
97
95
|
|
|
@@ -70,7 +70,7 @@ The `Engine` manages caching, execution, and state tracking.
|
|
|
70
70
|
* **`Engine(*, max_entries=10000, trace_limit=50000, stats=False, cache_dir=None, cache_map_size=2**30, cache_backend="mdbx", incremental=True)`**: Initializes the engine.
|
|
71
71
|
* `max_entries` sets the limit for the Least Recently Used (LRU) in-memory cache.
|
|
72
72
|
* Passing `cache_dir` enables persistent disk caching.
|
|
73
|
-
* `cache_backend` allows you to select the storage engine for persistence (`"mdbx"
|
|
73
|
+
* `cache_backend` allows you to select the storage engine for persistence (`"mdbx"` or `"sqlite"`).
|
|
74
74
|
|
|
75
75
|
### Defining Nodes: `@engine.input` and `@engine.query`
|
|
76
76
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "query-cascade"
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.26"
|
|
8
8
|
description = "Minimal demand-driven query framework for incremental computation."
|
|
9
9
|
requires-python = ">=3.12"
|
|
10
10
|
readme = "README.md"
|
|
@@ -12,13 +12,11 @@ readme = "README.md"
|
|
|
12
12
|
[project.optional-dependencies]
|
|
13
13
|
disk = [
|
|
14
14
|
"libmdbx",
|
|
15
|
-
"lmdb",
|
|
16
15
|
"msgpack",
|
|
17
16
|
]
|
|
18
17
|
dev = [
|
|
19
18
|
"hypothesis",
|
|
20
19
|
"libmdbx",
|
|
21
|
-
"lmdb",
|
|
22
20
|
"msgpack",
|
|
23
21
|
"mutmut",
|
|
24
22
|
"pytest",
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"""Deterministic msgpack encoding and hashing for the persistent disk cache.
|
|
2
|
+
|
|
3
|
+
Equal values must always produce identical byte streams, so dict keys and set
|
|
4
|
+
elements are emitted in a canonical order. Fingerprints are blake2b digests of
|
|
5
|
+
those byte streams. msgpack is imported lazily so the in-memory engine keeps
|
|
6
|
+
working without it; enabling ``cache_dir`` requires it.
|
|
7
|
+
|
|
8
|
+
Wire format (disk format 2)
|
|
9
|
+
---------------------------
|
|
10
|
+
Values msgpack cannot represent natively (tuples, sets, dataclasses, ...) are
|
|
11
|
+
written as an ordinary msgpack array whose first element is a zero-length
|
|
12
|
+
extension marker carrying the type code::
|
|
13
|
+
|
|
14
|
+
(1, "a") -> [ext(TUPLE, b""), 1, "a"]
|
|
15
|
+
{3, 1} -> [ext(SET, b""), 1, 3]
|
|
16
|
+
|
|
17
|
+
A plain ``list`` can never start with such a marker, so the form is
|
|
18
|
+
unambiguous. Because tagged values are ordinary arrays, a whole value is
|
|
19
|
+
packed with a single ``packb`` call and unpacked with a single ``unpackb``
|
|
20
|
+
(the unpacker invokes ``_list_hook`` as each array closes).
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import dataclasses
|
|
26
|
+
import datetime
|
|
27
|
+
import hashlib
|
|
28
|
+
import importlib
|
|
29
|
+
from typing import Any
|
|
30
|
+
|
|
31
|
+
from ._errors import PersistentCacheError
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
import msgpack
|
|
35
|
+
except ImportError: # pragma: no cover - exercised via monkeypatch in tests
|
|
36
|
+
msgpack = None # type: ignore[assignment]
|
|
37
|
+
|
|
38
|
+
MSGPACK_INSTALL_HINT = (
|
|
39
|
+
"cascade persistent caching requires the 'msgpack' package for deterministic "
|
|
40
|
+
"serialization; there is no fallback. Install it with: pip install msgpack"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
_EXT_TUPLE = 1
|
|
44
|
+
_EXT_SET = 2
|
|
45
|
+
_EXT_FROZENSET = 3
|
|
46
|
+
_EXT_MAP = 4 # dict with at least one non-str key
|
|
47
|
+
_EXT_BIGINT = 5 # int outside the native msgpack 64-bit range
|
|
48
|
+
_EXT_DATACLASS = 6
|
|
49
|
+
_EXT_NAMEDTUPLE = 7
|
|
50
|
+
_EXT_EXCEPTION = 8
|
|
51
|
+
_EXT_DATETIME = 9
|
|
52
|
+
_EXT_DATE = 10
|
|
53
|
+
_EXT_TIME = 11
|
|
54
|
+
_EXT_TIMEDELTA = 12
|
|
55
|
+
_EXT_TIMEZONE = 13
|
|
56
|
+
|
|
57
|
+
_INT64_MIN = -(2**63)
|
|
58
|
+
_UINT64_MAX = 2**64 - 1
|
|
59
|
+
|
|
60
|
+
_PASSTHROUGH_SCALARS = (str, bytes, float, bool, type(None))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class _Tag:
|
|
64
|
+
"""Decoded form of a zero-length extension marker."""
|
|
65
|
+
|
|
66
|
+
__slots__ = ("code",)
|
|
67
|
+
|
|
68
|
+
def __init__(self, code: int) -> None:
|
|
69
|
+
self.code = code
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
_TAGS: dict[int, _Tag] = {code: _Tag(code) for code in range(1, 14)}
|
|
73
|
+
# Encode-side markers, built on first use because msgpack is optional.
|
|
74
|
+
_MARKERS: dict[int, Any] = {}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def require_msgpack() -> Any:
|
|
78
|
+
if msgpack is None:
|
|
79
|
+
raise PersistentCacheError(MSGPACK_INSTALL_HINT)
|
|
80
|
+
return msgpack
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _marker(code: int) -> Any:
|
|
84
|
+
marker = _MARKERS.get(code)
|
|
85
|
+
if marker is None:
|
|
86
|
+
marker = _MARKERS[code] = msgpack.ExtType(code, b"")
|
|
87
|
+
return marker
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def encode(value: Any) -> bytes:
|
|
91
|
+
"""Canonical msgpack bytes for ``value``. Equal values yield equal bytes."""
|
|
92
|
+
mp = require_msgpack()
|
|
93
|
+
return mp.packb(_encode_node(value), use_bin_type=True)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def decode(blob: bytes) -> Any:
|
|
97
|
+
"""Inverse of :func:`encode`."""
|
|
98
|
+
mp = require_msgpack()
|
|
99
|
+
return mp.unpackb(
|
|
100
|
+
blob,
|
|
101
|
+
raw=False,
|
|
102
|
+
use_list=True,
|
|
103
|
+
strict_map_key=False,
|
|
104
|
+
ext_hook=_ext_hook,
|
|
105
|
+
list_hook=_list_hook,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def digest_bytes(blob: bytes) -> str:
|
|
110
|
+
return hashlib.blake2b(blob, digest_size=20).hexdigest()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def value_digest(value: Any) -> str:
|
|
114
|
+
"""Blake2b hex fingerprint of the canonical byte stream of ``value``."""
|
|
115
|
+
t = type(value)
|
|
116
|
+
if t in _PASSTHROUGH_SCALARS or (t is int and _INT64_MIN <= value <= _UINT64_MAX):
|
|
117
|
+
# Same bytes encode() would produce: _encode_node returns these as-is.
|
|
118
|
+
return digest_bytes(require_msgpack().packb(value, use_bin_type=True))
|
|
119
|
+
return digest_bytes(encode(value))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _packed(node: Any) -> bytes:
|
|
123
|
+
return msgpack.packb(node, use_bin_type=True)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _encode_node(obj: Any) -> Any:
|
|
127
|
+
t = type(obj)
|
|
128
|
+
# Exact-type fast paths first: these cover nearly every node in practice.
|
|
129
|
+
if t in _PASSTHROUGH_SCALARS:
|
|
130
|
+
return obj
|
|
131
|
+
if t is int:
|
|
132
|
+
if _INT64_MIN <= obj <= _UINT64_MAX:
|
|
133
|
+
return obj
|
|
134
|
+
magnitude = abs(obj)
|
|
135
|
+
return [
|
|
136
|
+
_marker(_EXT_BIGINT),
|
|
137
|
+
obj < 0,
|
|
138
|
+
magnitude.to_bytes((magnitude.bit_length() + 7) // 8 or 1, "big"),
|
|
139
|
+
]
|
|
140
|
+
if t is tuple:
|
|
141
|
+
return [_marker(_EXT_TUPLE), *[_encode_node(x) for x in obj]]
|
|
142
|
+
if t is list:
|
|
143
|
+
return [_encode_node(x) for x in obj]
|
|
144
|
+
if t is dict:
|
|
145
|
+
return _encode_dict(obj)
|
|
146
|
+
|
|
147
|
+
if t is bytearray:
|
|
148
|
+
return bytes(obj)
|
|
149
|
+
if isinstance(obj, datetime.datetime):
|
|
150
|
+
return [_marker(_EXT_DATETIME), obj.isoformat()]
|
|
151
|
+
if isinstance(obj, datetime.date):
|
|
152
|
+
return [_marker(_EXT_DATE), obj.isoformat()]
|
|
153
|
+
if isinstance(obj, datetime.time):
|
|
154
|
+
return [_marker(_EXT_TIME), obj.isoformat()]
|
|
155
|
+
if isinstance(obj, datetime.timedelta):
|
|
156
|
+
return [_marker(_EXT_TIMEDELTA), obj.days, obj.seconds, obj.microseconds]
|
|
157
|
+
if isinstance(obj, datetime.timezone):
|
|
158
|
+
td = obj.utcoffset(None)
|
|
159
|
+
return [
|
|
160
|
+
_marker(_EXT_TIMEZONE),
|
|
161
|
+
td.days,
|
|
162
|
+
td.seconds,
|
|
163
|
+
td.microseconds,
|
|
164
|
+
obj.tzname(None),
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
if isinstance(obj, tuple) and hasattr(obj, "_fields"):
|
|
168
|
+
return [
|
|
169
|
+
_marker(_EXT_NAMEDTUPLE),
|
|
170
|
+
t.__module__,
|
|
171
|
+
t.__qualname__,
|
|
172
|
+
*[_encode_node(getattr(obj, f)) for f in obj._fields],
|
|
173
|
+
]
|
|
174
|
+
if dataclasses.is_dataclass(obj) and not isinstance(obj, type):
|
|
175
|
+
node: list[Any] = [_marker(_EXT_DATACLASS), t.__module__, t.__qualname__]
|
|
176
|
+
for name in sorted(obj.__dataclass_fields__.keys()):
|
|
177
|
+
node.append(name)
|
|
178
|
+
node.append(_encode_node(getattr(obj, name)))
|
|
179
|
+
return node
|
|
180
|
+
|
|
181
|
+
if isinstance(obj, tuple):
|
|
182
|
+
return [_marker(_EXT_TUPLE), *[_encode_node(x) for x in obj]]
|
|
183
|
+
if isinstance(obj, list):
|
|
184
|
+
return [_encode_node(x) for x in obj]
|
|
185
|
+
if isinstance(obj, frozenset):
|
|
186
|
+
return [_marker(_EXT_FROZENSET), *_sorted_nodes(obj)]
|
|
187
|
+
if isinstance(obj, set):
|
|
188
|
+
return [_marker(_EXT_SET), *_sorted_nodes(obj)]
|
|
189
|
+
if isinstance(obj, dict):
|
|
190
|
+
return _encode_dict(obj)
|
|
191
|
+
|
|
192
|
+
if isinstance(obj, BaseException):
|
|
193
|
+
return [
|
|
194
|
+
_marker(_EXT_EXCEPTION),
|
|
195
|
+
t.__module__,
|
|
196
|
+
t.__qualname__,
|
|
197
|
+
*[_encode_node(x) for x in obj.args],
|
|
198
|
+
]
|
|
199
|
+
|
|
200
|
+
raise TypeError(
|
|
201
|
+
f"cascade persistent cache: cannot serialize {t.__module__}.{t.__qualname__!r}; "
|
|
202
|
+
"supported types are primitives, bytes, list/tuple/set/frozenset/dict, datetime, "
|
|
203
|
+
"@dataclass instances, typing.NamedTuple instances, and BaseException."
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _sorted_nodes(items: Any) -> list[Any]:
|
|
208
|
+
encoded = [_encode_node(x) for x in items]
|
|
209
|
+
# Homogeneous str or int sets (the common case) have a natural total
|
|
210
|
+
# order; anything else is ordered by its own packed bytes.
|
|
211
|
+
kinds = {type(x) for x in encoded}
|
|
212
|
+
if kinds == {str} or kinds == {int}:
|
|
213
|
+
encoded.sort()
|
|
214
|
+
return encoded
|
|
215
|
+
return sorted(encoded, key=_packed)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _encode_dict(obj: dict[Any, Any]) -> Any:
|
|
219
|
+
if all(type(k) is str for k in obj):
|
|
220
|
+
return {k: _encode_node(obj[k]) for k in sorted(obj)}
|
|
221
|
+
pairs = [(_encode_node(k), _encode_node(v)) for k, v in obj.items()]
|
|
222
|
+
pairs.sort(key=lambda kv: _packed(kv[0]))
|
|
223
|
+
node: list[Any] = [_marker(_EXT_MAP)]
|
|
224
|
+
for k, v in pairs:
|
|
225
|
+
node.append(k)
|
|
226
|
+
node.append(v)
|
|
227
|
+
return node
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _resolve_type(module_name: str, qualname: str) -> type[Any]:
|
|
231
|
+
mod = importlib.import_module(module_name)
|
|
232
|
+
cur: Any = mod
|
|
233
|
+
for part in qualname.split("."):
|
|
234
|
+
cur = getattr(cur, part)
|
|
235
|
+
if not isinstance(cur, type):
|
|
236
|
+
raise TypeError(f"{module_name}.{qualname!r} is not a type") # pragma: no cover
|
|
237
|
+
return cur
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _ext_hook(code: int, data: bytes) -> Any:
|
|
241
|
+
tag = _TAGS.get(code)
|
|
242
|
+
if tag is None or data:
|
|
243
|
+
raise ValueError(
|
|
244
|
+
f"cascade persistent cache: unexpected extension (code {code}, "
|
|
245
|
+
f"{len(data)} payload bytes); the blob is not disk format 2"
|
|
246
|
+
)
|
|
247
|
+
return tag
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _list_hook(items: list[Any]) -> Any:
|
|
251
|
+
# Children are already decoded: the unpacker calls this bottom-up.
|
|
252
|
+
if items and type(items[0]) is _Tag:
|
|
253
|
+
return _untag(items[0].code, items)
|
|
254
|
+
return items
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _make_dataclass(module_name: str, qualname: str, fields: dict[str, Any]) -> Any:
|
|
258
|
+
cls = _resolve_type(module_name, qualname)
|
|
259
|
+
if not (dataclasses.is_dataclass(cls) and isinstance(cls, type)):
|
|
260
|
+
raise TypeError(
|
|
261
|
+
f"{module_name}.{qualname!r} is not a dataclass"
|
|
262
|
+
) # pragma: no cover
|
|
263
|
+
return cls(**fields)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _make_namedtuple(module_name: str, qualname: str, values: list[Any]) -> Any:
|
|
267
|
+
cls = _resolve_type(module_name, qualname)
|
|
268
|
+
if not (isinstance(cls, type) and issubclass(cls, tuple) and hasattr(cls, "_make")):
|
|
269
|
+
raise TypeError(
|
|
270
|
+
f"{module_name}.{qualname!r} is not a NamedTuple"
|
|
271
|
+
) # pragma: no cover
|
|
272
|
+
return cls(*values)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _make_exception(module_name: str, qualname: str, args: list[Any]) -> Any:
|
|
276
|
+
cls = _resolve_type(module_name, qualname)
|
|
277
|
+
if not (isinstance(cls, type) and issubclass(cls, BaseException)):
|
|
278
|
+
raise TypeError(
|
|
279
|
+
f"{module_name}.{qualname!r} is not an Exception"
|
|
280
|
+
) # pragma: no cover
|
|
281
|
+
return cls(*args)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _make_bigint(negative: bool, magnitude: bytes) -> int:
|
|
285
|
+
value = int.from_bytes(magnitude, "big")
|
|
286
|
+
return -value if negative else value
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _make_timezone(
|
|
290
|
+
days: int, seconds: int, microseconds: int, name: str
|
|
291
|
+
) -> datetime.timezone:
|
|
292
|
+
return datetime.timezone(
|
|
293
|
+
datetime.timedelta(days=days, seconds=seconds, microseconds=microseconds),
|
|
294
|
+
name,
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _untag(code: int, items: list[Any]) -> Any:
|
|
299
|
+
"""Rebuild a tagged value from ``[marker, *already-decoded parts]``."""
|
|
300
|
+
if code == _EXT_TUPLE:
|
|
301
|
+
return tuple(items[1:])
|
|
302
|
+
if code == _EXT_SET:
|
|
303
|
+
return set(items[1:])
|
|
304
|
+
if code == _EXT_FROZENSET:
|
|
305
|
+
return frozenset(items[1:])
|
|
306
|
+
if code == _EXT_MAP:
|
|
307
|
+
return {items[i]: items[i + 1] for i in range(1, len(items), 2)}
|
|
308
|
+
if code == _EXT_BIGINT:
|
|
309
|
+
return _make_bigint(items[1], items[2])
|
|
310
|
+
if code == _EXT_DATACLASS:
|
|
311
|
+
fields = {items[i]: items[i + 1] for i in range(3, len(items), 2)}
|
|
312
|
+
return _make_dataclass(items[1], items[2], fields)
|
|
313
|
+
if code == _EXT_NAMEDTUPLE:
|
|
314
|
+
return _make_namedtuple(items[1], items[2], items[3:])
|
|
315
|
+
if code == _EXT_EXCEPTION:
|
|
316
|
+
return _make_exception(items[1], items[2], items[3:])
|
|
317
|
+
if code == _EXT_DATETIME:
|
|
318
|
+
return datetime.datetime.fromisoformat(items[1])
|
|
319
|
+
if code == _EXT_DATE:
|
|
320
|
+
return datetime.date.fromisoformat(items[1])
|
|
321
|
+
if code == _EXT_TIME:
|
|
322
|
+
return datetime.time.fromisoformat(items[1])
|
|
323
|
+
if code == _EXT_TIMEDELTA:
|
|
324
|
+
return datetime.timedelta(
|
|
325
|
+
days=items[1], seconds=items[2], microseconds=items[3]
|
|
326
|
+
)
|
|
327
|
+
return _make_timezone(items[1], items[2], items[3], items[4])
|