python-wrap 0.2.1__py3-none-any.whl → 0.4.0__py3-none-any.whl
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.
- python_wrap/__init__.py +348 -1
- python_wrap/__main__.py +57 -0
- python_wrap/_backend.py +133 -0
- python_wrap/_buckets.py +162 -0
- python_wrap/_core.py +12 -3
- python_wrap/_data/agent_guide.md +103 -0
- python_wrap/_data/recipes.json +703 -0
- python_wrap/_fork.py +113 -0
- python_wrap/_integration.py +29 -0
- python_wrap/_runtime.py +157 -0
- python_wrap/adapters/__init__.py +101 -0
- python_wrap/adapters/_frameworks.py +251 -0
- python_wrap/adapters/_sdk.py +1978 -0
- python_wrap/adapters/anthropic.py +139 -0
- python_wrap/adapters/langchain.py +288 -0
- python_wrap/adapters/openai.py +169 -0
- python_wrap/adapters/openai_agents.py +468 -0
- python_wrap/adapters/pydantic_ai.py +459 -0
- python_wrap/adaptive.py +605 -0
- python_wrap/agents.py +2503 -0
- python_wrap/alerts.py +293 -0
- python_wrap/async_cache.py +745 -0
- python_wrap/batching.py +17 -4
- python_wrap/buffering.py +952 -0
- python_wrap/bulkhead.py +58 -6
- python_wrap/caching.py +425 -56
- python_wrap/celery.py +528 -0
- python_wrap/chaos.py +519 -0
- python_wrap/circuit.py +454 -23
- python_wrap/compat.py +267 -0
- python_wrap/concurrency.py +82 -1746
- python_wrap/config.py +25 -4
- python_wrap/coordination.py +638 -0
- python_wrap/credentials.py +126 -0
- python_wrap/dashboard.py +358 -0
- python_wrap/debounce.py +538 -0
- python_wrap/django.py +100 -0
- python_wrap/doctor.py +599 -0
- python_wrap/events.py +145 -4
- python_wrap/exporters.py +663 -0
- python_wrap/fastapi.py +81 -0
- python_wrap/flask.py +61 -0
- python_wrap/genai.py +1101 -0
- python_wrap/health.py +187 -0
- python_wrap/http.py +940 -0
- python_wrap/idempotency.py +446 -0
- python_wrap/introspect.py +738 -0
- python_wrap/lifecycle.py +620 -0
- python_wrap/llm.py +2574 -0
- python_wrap/llm_cassette.py +948 -0
- python_wrap/llm_http_chaos.py +326 -0
- python_wrap/llm_shared.py +772 -0
- python_wrap/llm_testing.py +990 -0
- python_wrap/llm_validate.py +383 -0
- python_wrap/local_state.py +395 -0
- python_wrap/logs.py +82 -26
- python_wrap/mcp.py +490 -0
- python_wrap/mcp_guard.py +357 -0
- python_wrap/metrics.py +6 -3
- python_wrap/migrate.py +1452 -0
- python_wrap/observability.py +484 -0
- python_wrap/periodic.py +24 -2
- python_wrap/policies.py +396 -0
- python_wrap/policy.py +14 -0
- python_wrap/predicates.py +276 -0
- python_wrap/presets.py +378 -0
- python_wrap/processing.py +227 -0
- python_wrap/propagation.py +62 -0
- python_wrap/pytest_plugin.py +85 -0
- python_wrap/quotas.py +319 -0
- python_wrap/ratelimit.py +560 -0
- python_wrap/redaction.py +228 -0
- python_wrap/retry.py +448 -70
- python_wrap/shared.py +653 -0
- python_wrap/singleflight.py +255 -0
- python_wrap/slow.py +72 -0
- python_wrap/stores.py +168 -0
- python_wrap/switches.py +164 -0
- python_wrap/testing.py +34 -8
- python_wrap/timeouts.py +874 -0
- python_wrap/timing.py +239 -17
- python_wrap/validation.py +187 -12
- python_wrap/web.py +723 -0
- {python_wrap-0.2.1.dist-info → python_wrap-0.4.0.dist-info}/METADATA +117 -7
- python_wrap-0.4.0.dist-info/RECORD +95 -0
- python_wrap-0.2.1.dist-info/RECORD +0 -29
- {python_wrap-0.2.1.dist-info → python_wrap-0.4.0.dist-info}/WHEEL +0 -0
- {python_wrap-0.2.1.dist-info → python_wrap-0.4.0.dist-info}/licenses/LICENSE +0 -0
python_wrap/testing.py
CHANGED
|
@@ -24,7 +24,6 @@ import asyncio
|
|
|
24
24
|
import functools
|
|
25
25
|
import inspect
|
|
26
26
|
import math
|
|
27
|
-
import threading
|
|
28
27
|
import time
|
|
29
28
|
import types
|
|
30
29
|
from collections.abc import Awaitable, Callable, Iterator
|
|
@@ -32,7 +31,7 @@ from contextlib import contextmanager
|
|
|
32
31
|
from dataclasses import dataclass, field
|
|
33
32
|
from typing import Any, Generic, NoReturn, ParamSpec, TypeVar, overload
|
|
34
33
|
|
|
35
|
-
from python_wrap import _core
|
|
34
|
+
from python_wrap import _core, _fork
|
|
36
35
|
|
|
37
36
|
try:
|
|
38
37
|
import pytest as _pytest
|
|
@@ -43,9 +42,12 @@ __all__ = [
|
|
|
43
42
|
"Call",
|
|
44
43
|
"FakeClock",
|
|
45
44
|
"Spy",
|
|
45
|
+
"assert_cache_hit",
|
|
46
|
+
"assert_retried",
|
|
46
47
|
"disable_caching",
|
|
47
48
|
"disable_rate_limits",
|
|
48
49
|
"disable_retries",
|
|
50
|
+
"disabled",
|
|
49
51
|
"eventually",
|
|
50
52
|
"eventually_sync",
|
|
51
53
|
"fake_clock",
|
|
@@ -71,7 +73,7 @@ _UNSET = _Unset()
|
|
|
71
73
|
# --------------------------------------------------------------------------- overrides
|
|
72
74
|
|
|
73
75
|
|
|
74
|
-
_scope_lock =
|
|
76
|
+
_scope_lock = _fork.ForkSafeLock()
|
|
75
77
|
# Per overridden field: the value before the first active scope, and the stack of
|
|
76
78
|
# (scope token, value) for every scope currently active on it.
|
|
77
79
|
_baseline: dict[str, Any] = {}
|
|
@@ -260,8 +262,8 @@ class FakeClock:
|
|
|
260
262
|
start: float = field(default_factory=lambda: float(math.ceil(time.monotonic())))
|
|
261
263
|
now: float = field(init=False)
|
|
262
264
|
sleeps: list[float] = field(default_factory=list)
|
|
263
|
-
_lock:
|
|
264
|
-
default_factory=
|
|
265
|
+
_lock: _fork.ForkSafeLock = field(
|
|
266
|
+
default_factory=_fork.ForkSafeLock, init=False, repr=False, compare=False
|
|
265
267
|
)
|
|
266
268
|
|
|
267
269
|
def __reduce__(self) -> NoReturn:
|
|
@@ -304,7 +306,7 @@ class FakeClock:
|
|
|
304
306
|
|
|
305
307
|
@contextmanager
|
|
306
308
|
def fake_clock(clock: FakeClock | None = None) -> Iterator[FakeClock]:
|
|
307
|
-
"""Patch
|
|
309
|
+
"""Patch core monotonic, performance, wall-clock and sleep sources with a FakeClock.
|
|
308
310
|
|
|
309
311
|
The previous time sources are restored on exit, so it nests.
|
|
310
312
|
|
|
@@ -334,11 +336,12 @@ def fake_clock(clock: FakeClock | None = None) -> Iterator[FakeClock]:
|
|
|
334
336
|
```
|
|
335
337
|
"""
|
|
336
338
|
clock = clock if clock is not None else FakeClock()
|
|
337
|
-
names = ("monotonic", "perf_counter", "sleep", "async_sleep")
|
|
339
|
+
names = ("monotonic", "perf_counter", "wall_time", "sleep", "async_sleep")
|
|
338
340
|
with _core.overrides_lock:
|
|
339
341
|
saved = {name: getattr(_core, name) for name in names}
|
|
340
342
|
_core.monotonic = clock.time
|
|
341
343
|
_core.perf_counter = clock.time
|
|
344
|
+
_core.wall_time = clock.time
|
|
342
345
|
_core.sleep = clock.sleep
|
|
343
346
|
_core.async_sleep = clock.async_sleep
|
|
344
347
|
try:
|
|
@@ -376,7 +379,7 @@ class Spy(Generic[P, R]):
|
|
|
376
379
|
def __init__(self, func: Callable[P, R]) -> None:
|
|
377
380
|
self._func = func
|
|
378
381
|
self.calls: list[Call] = []
|
|
379
|
-
self._lock =
|
|
382
|
+
self._lock = _fork.ForkSafeLock()
|
|
380
383
|
functools.update_wrapper(self, func)
|
|
381
384
|
|
|
382
385
|
@property
|
|
@@ -660,3 +663,26 @@ if _pytest is not None:
|
|
|
660
663
|
"""Run the test under `fast_mode()`."""
|
|
661
664
|
with fast_mode():
|
|
662
665
|
yield
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
def disabled() -> _Scope:
|
|
669
|
+
"""Temporarily bypass retries, caches and rate limits; deadlines remain real."""
|
|
670
|
+
return overrides(max_attempts=1, disable_caching=True, disable_rate_limits=True)
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
def assert_retried(func: Any, *, times: int) -> None:
|
|
674
|
+
"""Assert the retry counter since the last reset (times means extra attempts)."""
|
|
675
|
+
from python_wrap.retry import retry_stats
|
|
676
|
+
|
|
677
|
+
stats = retry_stats(func)
|
|
678
|
+
if stats is None:
|
|
679
|
+
raise AssertionError("function is not decorated with retry")
|
|
680
|
+
if stats.retries != times:
|
|
681
|
+
raise AssertionError(f"expected {times} retries, got {stats.retries}")
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
def assert_cache_hit(func: Any, *, times: int = 1) -> None:
|
|
685
|
+
"""Assert the total cache hits since cache_clear/reset."""
|
|
686
|
+
hits = func.cache_info().hits
|
|
687
|
+
if hits != times:
|
|
688
|
+
raise AssertionError(f"expected {times} cache hits, got {hits}")
|