Skip to content

Commit a06dd45

Browse files
Merge pull request #5 from LitePy/docs/restructure
Restructure the documentation
2 parents 3741516 + 5da4840 commit a06dd45

59 files changed

Lines changed: 3750 additions & 2122 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Retry (with `Retry-After`), circuit breaker, timeout, cache, rate limit, bulkhea
1919

2020
[**Documentation**](https://python-wrap.readthedocs.io) ·
2121
[Cookbook](https://python-wrap.readthedocs.io/en/latest/cookbook/) ·
22-
[Performance](https://python-wrap.readthedocs.io/en/latest/performance/) ·
23-
[Migration guide](https://python-wrap.readthedocs.io/en/latest/migration/) ·
22+
[Performance](https://python-wrap.readthedocs.io/en/latest/project/performance/) ·
23+
[Migration guide](https://python-wrap.readthedocs.io/en/latest/project/migration/) ·
2424
[Changelog](https://github.com/LitePy/PythonWrap/blob/main/CHANGELOG.md)
2525

2626
</div>
@@ -33,7 +33,7 @@ Retry (with `Retry-After`), circuit breaker, timeout, cache, rate limit, bulkhea
3333
- **Zero dependencies.** Pure Python, nothing to audit or pin.
3434
- **Typed.** Decorators preserve the wrapped signature (`ParamSpec`), and stacking keeps the exact type (`@retry` over `@memoize` still has `.cache_info()`). Ships `py.typed`, checked with `mypy --strict`.
3535
- **Sync and async, one API.** Works on `def`, `async def` and methods. Async wrappers stay coroutine functions and never block the event loop.
36-
- **Fast.** Cache hits and validation skip signature binding on the hot path ([numbers](https://python-wrap.readthedocs.io/en/latest/performance/)).
36+
- **Fast.** Cache hits and validation skip signature binding on the hot path ([numbers](https://python-wrap.readthedocs.io/en/latest/project/performance/)).
3737
- **Observable and testable.** One event stream for retries, cache, circuit, limiters and timeouts; `python_wrap.testing` makes tests instant and deterministic.
3838
- **Never configures your logging**, is thread-safe, and is tested on Python 3.10 to 3.14 on Linux, macOS and Windows.
3939

@@ -113,7 +113,7 @@ print(f"{time.monotonic() - start:.2f}s")
113113

114114
## What is in the box
115115

116-
Everything below is importable from `python_wrap` unless a module is shown. [Choose a decorator](https://python-wrap.readthedocs.io/en/latest/choosing/) is the quick way in.
116+
Everything below is importable from `python_wrap` unless a module is shown. [Choose a decorator](https://python-wrap.readthedocs.io/en/latest/get-started/choosing/) is the quick way in.
117117

118118
| Area | Name | What it does |
119119
|---|---|---|
@@ -232,7 +232,7 @@ except TimeoutExpired:
232232
print("out of time")
233233
```
234234

235-
After `recovery_timeout` the breaker goes half-open and lets a trial call through. Share one `CircuitBreaker` across functions for one dependency, or use `@circuit_breaker(...)` for a private one (`func.breaker`). `timeout` interrupts sync code with `SIGALRM` on the Unix main thread, cancels `async def` with `asyncio.wait_for`, and elsewhere falls back to a thread that cannot stop the work (see [Limitations](https://python-wrap.readthedocs.io/en/latest/limitations/)).
235+
After `recovery_timeout` the breaker goes half-open and lets a trial call through. Share one `CircuitBreaker` across functions for one dependency, or use `@circuit_breaker(...)` for a private one (`func.breaker`). `timeout` interrupts sync code with `SIGALRM` on the Unix main thread, cancels `async def` with `asyncio.wait_for`, and elsewhere falls back to a thread that cannot stop the work (see [Limitations](https://python-wrap.readthedocs.io/en/latest/project/limitations/)).
236236

237237
### Caching
238238

@@ -359,7 +359,7 @@ print(names) # retry.attempt_failed, ..., retry.succeed
359359
print(work.stats.calls, work.stats.p95)
360360
```
361361

362-
Subscribe once and forward events to Prometheus, OpenTelemetry or statsd (recipes in the [guide](https://python-wrap.readthedocs.io/en/latest/guides/metrics/)). Listeners cannot break the decorated function.
362+
Subscribe once and forward events to Prometheus, OpenTelemetry or statsd (recipes in the [guide](https://python-wrap.readthedocs.io/en/latest/features/observability/metrics/)). Listeners cannot break the decorated function.
363363

364364
### Validation and logging with redaction
365365

@@ -411,7 +411,7 @@ Decorators are applied bottom-up and run top-down, so order matters:
411411
| `log_args`, `timing`, `audit` | everything | They observe what callers actually experience |
412412
| `enforce_types`, `validate_args` | the rest | Reject bad input before doing any work |
413413

414-
You do not have to memorise this. `policy(observe=, validate=, fallback=, cache=, limiter=, bulkhead=, breaker=, retry=, timeout=, guard=)` takes each decorator by role and applies them in the recommended order, whatever order you wrote them in. Policies are immutable (`.with_(timeout=timeout(30))`), work on methods and `async def`, and `wrap_methods(policy)` protects a whole class. `compose(a, b, c)(f)` is `a(b(c(f)))` when you just want a named stack. Details: [Stacking](https://python-wrap.readthedocs.io/en/latest/stacking/) and [Policies](https://python-wrap.readthedocs.io/en/latest/guides/policies/).
414+
You do not have to memorise this. `policy(observe=, validate=, fallback=, cache=, limiter=, bulkhead=, breaker=, retry=, timeout=, guard=)` takes each decorator by role and applies them in the recommended order, whatever order you wrote them in. Policies are immutable (`.with_(timeout=timeout(30))`), work on methods and `async def`, and `wrap_methods(policy)` protects a whole class. `compose(a, b, c)(f)` is `a(b(c(f)))` when you just want a named stack. Details: [Stacking](https://python-wrap.readthedocs.io/en/latest/get-started/stacking/) and [Policies](https://python-wrap.readthedocs.io/en/latest/features/resilience/policies/).
415415

416416
## How it compares
417417

@@ -433,14 +433,14 @@ None of them cover bounded `gather`, bulkheads, batching, hedging, policies or t
433433
## Learn more
434434

435435
- [Cookbook](https://python-wrap.readthedocs.io/en/latest/cookbook/): resilient HTTP client, async crawler, FastAPI, Django, CLI tools, DataLoader batching, testing, observability.
436-
- [Performance](https://python-wrap.readthedocs.io/en/latest/performance/): per-call overhead and how to benchmark it yourself.
436+
- [Performance](https://python-wrap.readthedocs.io/en/latest/project/performance/): per-call overhead and how to benchmark it yourself.
437437
- [Testing](https://python-wrap.readthedocs.io/en/latest/testing/): `fast_mode`, `FakeClock`, `spy` and pytest fixtures.
438-
- [Migration guide](https://python-wrap.readthedocs.io/en/latest/migration/) and [CHANGELOG](https://github.com/LitePy/PythonWrap/blob/main/CHANGELOG.md).
438+
- [Migration guide](https://python-wrap.readthedocs.io/en/latest/project/migration/) and [CHANGELOG](https://github.com/LitePy/PythonWrap/blob/main/CHANGELOG.md).
439439
- Runnable [examples](https://github.com/LitePy/PythonWrap/tree/main/examples).
440440

441441
## Upgrading from 0.1
442442

443-
0.2.0 fixes decorators that did not do what their names promised (`transactional`, `revert_on_failure`, async `timing`) and makes a few breaking changes: `run_in_thread` returns a `Future`, `benchmark` is a function, `retry_on_exception` is deprecated and bounded, `rate_limit` raises `RateLimitExceeded`, and the logging decorators default to `DEBUG`. Python 3.8 and 3.9 are no longer supported. `from python_wrap.decorators import ...` still works. See the [migration guide](https://python-wrap.readthedocs.io/en/latest/migration/).
443+
0.2.0 fixes decorators that did not do what their names promised (`transactional`, `revert_on_failure`, async `timing`) and makes a few breaking changes: `run_in_thread` returns a `Future`, `benchmark` is a function, `retry_on_exception` is deprecated and bounded, `rate_limit` raises `RateLimitExceeded`, and the logging decorators default to `DEBUG`. Python 3.8 and 3.9 are no longer supported. `from python_wrap.decorators import ...` still works. See the [migration guide](https://python-wrap.readthedocs.io/en/latest/project/migration/).
444444

445445
## Contributing
446446

‎docs/api/index.md‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
11
# API reference
22

3-
Everything is importable from the top-level `python_wrap` package. These pages are generated from the docstrings, so they always match the code.
3+
These pages are generated from the docstrings, so they always match the code. Almost
4+
everything is importable from the top-level `python_wrap` package; the exceptions are the
5+
helpers that live in a submodule (`python_wrap.testing`, `python_wrap.events`, and
6+
`remaining`/`check` in `python_wrap.deadline`).
7+
8+
For explanations and examples, start from the matching [feature page](../features/index.md).
9+
10+
## Resilience
411

512
| Module | Contents |
613
| --- | --- |
7-
| [errors](errors.md) | `PythonWrapError`, `TimeoutExpired`, `RateLimitExceeded`, `CircuitOpenError`, `ValidationError`, `ArgumentTypeError`, `BulkheadFull` |
8-
| [retry](retry.md) | `retry`, `fallback`, `suppress`, `retry_on_exception` |
9-
| [circuit](circuit.md) | `CircuitBreaker`, `circuit_breaker`, `CircuitState`, `CircuitStats` |
14+
| [retry](retry.md) | `retry`, `retrying`, `retry_stats`, `retry_after_header`, `fallback`, `suppress`, `retry_on_exception`, `Attempt`, `RetryState`, `RetryStats`, `Retrying` |
15+
| [circuit](circuit.md) | `CircuitBreaker`, `circuit_breaker`, `CircuitState`, `CircuitStats`, `CircuitProtected` |
1016
| [deadline](deadline.md) | `deadline`, `remaining`, `check`, `Deadline` |
11-
| [caching](caching.md) | `memoize`, `cache`, `once` |
12-
| [concurrency](concurrency.md) | `timeout`, `Timeout`, `rate_limit`, `RateLimiter`, `throttle`, `debounce`, `singleflight`, `run_in_thread` |
17+
| [hedging](hedging.md) | `hedge` |
18+
| [policy](policy.md) | `policy`, `Policy`, `ROLES` |
19+
20+
## Caching and batching
21+
22+
| Module | Contents |
23+
| --- | --- |
24+
| [caching](caching.md) | `memoize`, `cache`, `once`, `cached_property`, `invalidate`, `CacheInfo`, `Memoized`, `Once`, `CachedProperty` |
25+
| [batching](batching.md) | `batch_calls`, `Batched`, `AsyncBatched` |
26+
27+
## Concurrency and limits
28+
29+
| Module | Contents |
30+
| --- | --- |
31+
| [concurrency](concurrency.md) | `timeout`, `Timeout`, `rate_limit`, `RateLimiter`, `Limit`, `limiter_of`, `throttle`, `debounce`, `singleflight`, `run_in_thread` |
1332
| [aio](aio.md) | `gather_limit`, `map_async`, `amap_iter`, `race`, `RaceFailed`, `run_sync` |
1433
| [bulkhead](bulkhead.md) | `Bulkhead`, `limit_concurrency`, `Limited` |
15-
| [timing](timing.md) | `timing`, `benchmark`, `benchmark_async`, `measure_memory`, `profile` |
16-
| [logs](logs.md) | `log_args`, `log_return`, `audit`, `trace`, `DEFAULT_REDACT` |
34+
35+
## Observability
36+
37+
| Module | Contents |
38+
| --- | --- |
39+
| [logs](logs.md) | `log_args`, `log_return`, `trace`, `audit`, `AuditRecord`, `log_context`, `current_log_context`, `DEFAULT_REDACT` |
40+
| [timing](timing.md) | `timing`, `benchmark`, `benchmark_async`, `measure_memory`, `profile`, `TimingRecord`, `BenchmarkResult`, `MemoryRecord` |
41+
| [metrics](metrics.md) | `count_calls`, `CallStats`, `Counted` |
42+
| [events](events.md) | `subscribe`, `listening`, `filter_events`, `Event`, `EVENT_NAMES` |
43+
44+
## Correctness
45+
46+
| Module | Contents |
47+
| --- | --- |
1748
| [validation](validation.md) | `validate_args`, `check_type`, `enforce_types` |
18-
| [state](state.md) | `revert_on_failure`, `Transaction`, `transactional` |
19-
| [devtools](devtools.md) | `deprecate`, `mock_data`, `compose`, `no_debug` |
49+
| [state](state.md) | `revert_on_failure`, `Transaction`, `transactional`, `INJECTED`, `current_transaction`, `require_transaction` |
50+
51+
## Everything else
52+
53+
| Module | Contents |
54+
| --- | --- |
55+
| [config](config.md) | `configure`, `configured`, `get_config`, `reset_config` |
56+
| [devtools](devtools.md) | `deprecate`, `mock_data`, `compose`, `wrap_methods`, `no_debug` |
57+
| [testing](testing.md) | `fast_mode`, `fake_clock`, `FakeClock`, `disable_retries`, `disable_rate_limits`, `disable_caching`, `overrides`, `spy`, `spy_on`, `eventually`, `eventually_sync` |
58+
| [errors](errors.md) | `PythonWrapError`, `TimeoutExpired`, `RateLimitExceeded`, `CircuitOpenError`, `BulkheadFull`, `BatchError`, `ValidationError`, `ArgumentTypeError` |

‎docs/cookbook/dataloader-batching.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ for post in posts:
1111

1212
This is the "N+1" query problem, and it appears everywhere: ORMs, GraphQL resolvers, templates, serializers. Fixing it by hand means restructuring the code so someone collects all ids first, loads them in one query and passes the results back down, and that couples every layer to the loading strategy.
1313

14-
[`batch_calls`](../guides/batching.md) is the DataLoader pattern: write **one bulk function** that takes a list of ids, and call it **one item at a time** from anywhere. Calls made close together are collected into a single bulk call, and each caller gets its own result.
14+
[`batch_calls`](../features/caching/batching.md) is the DataLoader pattern: write **one bulk function** that takes a list of ids, and call it **one item at a time** from anywhere. Calls made close together are collected into a single bulk call, and each caller gets its own result.
1515

1616
## The code
1717

‎docs/cookbook/index.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The guides explain one decorator at a time. The cookbook shows how they combine
66
2. **The code**, complete and runnable. Every code block on these pages is executed by the docs test suite, so what you read works.
77
3. **How it works**, one decorator at a time.
88
4. **Pitfalls**, the mistakes that cost a day.
9-
5. **Test it**, a runnable test file that uses [`python_wrap.testing`](../testing.md), so the decorators never make your tests slow or flaky.
9+
5. **Test it**, a runnable test file that uses [`python_wrap.testing`](../testing/index.md), so the decorators never make your tests slow or flaky.
1010

1111
Every recipe that matters has a standalone script in the [`examples/`](https://github.com/LitePy/PythonWrap/tree/main/examples) directory that you can run with `python examples/<name>.py`. No third-party packages are needed; framework code (FastAPI, Django, httpx) is shown but marked as such.
1212

@@ -29,7 +29,7 @@ New to python-wrap? Start with the [resilient HTTP client](resilient-http-client
2929

3030
## Ground rules the recipes follow
3131

32-
* **Stack deliberately.** The decorator closest to the function runs first. `@retry` above `@rate_limit` means every retry counts against the limit; the other way round, the whole retry loop counts once. See [Stacking decorators](../stacking.md).
32+
* **Stack deliberately.** The decorator closest to the function runs first. `@retry` above `@rate_limit` means every retry counts against the limit; the other way round, the whole retry loop counts once. See [Stacking decorators](../get-started/stacking.md).
3333
* **State belongs to the function.** Caches, breakers and rate-limit windows are created once per decorated function and shared by every caller in the process. That is the point, and also the first thing to reset in tests.
3434
* **Libraries do not configure logging.** Recipes that print or collect logs set up their own handlers, as your application should.
3535
* **Be explicit about what is safe to repeat.** `retry`, `hedge` and `race` may run an operation more than once. Use them for reads and idempotent writes.

‎docs/cookbook/resilient-http-client.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ For `httpx.AsyncClient` make `get` and `_send` `async def` and use `await breake
219219

220220
## Test it
221221

222-
The decorators wait and remember, so tests need the helpers from [`python_wrap.testing`](../testing.md): `fake_clock` makes sleeps instant and records them, `spy_on` counts transport calls and `disable_rate_limits` lets calls through limiters. (`fast_mode` skips waits without recording them; see [Testing code that uses python-wrap](testing-your-code.md).)
222+
The decorators wait and remember, so tests need the helpers from [`python_wrap.testing`](../testing/index.md): `fake_clock` makes sleeps instant and records them, `spy_on` counts transport calls and `disable_rate_limits` lets calls through limiters. (`fast_mode` skips waits without recording them; see [Testing code that uses python-wrap](testing-your-code.md).)
223223

224224
```python
225225
import threading

‎docs/cookbook/testing-your-code.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Decorators that wait and remember make tests slow and order-dependent:
99
- A `rate_limit` or circuit breaker keeps counting across tests, so a test passes alone and fails in the suite.
1010
- A TTL cache or throttle window needs real time to pass.
1111

12-
[`python_wrap.testing`](../testing.md) is built for this. It is standard library only (pytest is optional), and the helpers change python-wrap's behaviour from the outside, so production code stays unchanged.
12+
[`python_wrap.testing`](../testing/index.md) is built for this. It is standard library only (pytest is optional), and the helpers change python-wrap's behaviour from the outside, so production code stays unchanged.
1313

1414
| Helper | Use it to |
1515
| --- | --- |

0 commit comments

Comments
 (0)