This release is a pre-release and may not be stable for production use.
wrapture
Wrap anything, capture everything, change nothing.
wrapture (wrapt + capture) is a Python library for attaching bindings to
arbitrary call sites, without modifying the code being observed, and doing
something useful with what flows through them.
It is a sibling project to wrapt and autowrapt, building on the safe monkey-patching machinery wrapt provides.
Status: early development. The monkey patching, unit testing and ad-hoc tracing layers are implemented, including WSGI and ASGI request tracing. Development previews are published to PyPI; the API may still shift before 1.0.0.
Installation
wrapture is on PyPI:
$ pip install wrapture
or with uv:
$ uv add wrapture
Documentation
Full documentation is at wrapture.readthedocs.io.
Start with the getting started
page: everything on it can be pasted into a Python interpreter. Coming
from unittest.mock? There is a
comparison page
mapping each mock idiom to its wrapture counterpart. After that, the
worked examples, starting with
testing code that calls external services,
each take one question you might arrive with and answer it end to end.
Thirty seconds of it
None of the classes below import wrapture or know they are observed:
place = wrapture.binding(OrderService, "place")
charge = wrapture.binding(Gateway, "charge")
record = wrapture.binding(Ledger, "record")
with wrapture.timeline(place, charge, record) as tape:
OrderService().place(500)
print(tape.tree())
OrderService.place(amount=500) -> {'id': 'ch_500', 'amount': 500}
Gateway.charge(amount=500, currency='USD') -> {'id': 'ch_500', 'amount': 500}
Ledger.record(entry={'id': 'ch_500', 'amount': 500}) -> 'led_ch_500'
The same bindings intervene as well as observe: stub a result, inject a failure, or transform one argument while the real code keeps running.
What it does
One mechanism, three uses, in increasing order of machinery:
-
Monkey patching. A clean lifecycle and behaviour vocabulary over wrapt's
wrap_object(). Point at a method by name and stub it, fail it, transform its arguments or result, or wrap it with a decorator, then remove it again, with honest reporting if something else displaced the patch in the meantime. Useful entirely on its own, with nothing else switched on. -
Unit testing. Observe and assert on how calls actually flowed through a real call graph (nesting, ordering, arguments and return values) and optionally intervene (stub, transform, fail-inject). Unlike a
Mock, which fabricates values and cannot see calls an object makes to itself, wrapture watches the real code run. This makes it possible to test code with no injectable seams at all, and to assert on what didn't happen on an error path: inject a gateway timeout, then verify the ledger was not written, the receipt was not sent, and the compensating refund was issued. -
Ad-hoc tracing. Attach bindings to a running application, including one you cannot modify or redeploy, and emit a structured, nested trace to process or chart elsewhere. Name a handful of methods and a call tree appears; no code changes required: with a
wrapture.tomlnaming the methods and a sink,python -m wrapture manage.py runservertraces the application untouched. With autowrapt installed, not even the launcher is needed:AUTOWRAPT_BOOTSTRAP=wrapturein the environment applies the same config at interpreter startup, so the program starts with plainpython.
The distinction that matters: most tracing tools either need the code to have been written with them in mind, or can only be switched on for the whole program at once. wrapture needs neither: you point at a method by name and a trace appears.
Why
No single existing tool covers "point at arbitrary methods, get a structured nested trace, assert on it or export it, in tests or in production":
unittest.mockrecords a flat call list, with no nesting and no return values, and a patched call returns a fabricatedMagicMockrather than running the real code.- Span-assertion tools (
logfire.testing, OpenTelemetry'sInMemorySpanExporter) require the code to already be instrumented. sys.settracetools (hunter,snoop) give a firehose with no assertion API.- APM agents are all-or-nothing products rather than a toolkit.
wrapture fills that gap: a targeted call tree with normalized arguments and return values, produced by naming the methods you care about, usable as a testing assertion library, a tracing tool, or both at once.
What it is not
- Not a replacement for
unittest.mock. It complements mocking where code has seams; it exists for the code that doesn't. - Not a production APM. It is a toolkit that APM-like things could be built on.
- Not an OpenTelemetry competitor. It should emit to OTel, not replace it.
Requirements
- Python 3.12+
- wrapt 2.4.0+
Issues
Bug reports and feature requests go to the issue tracker. Include the wrapture and Python versions and, where you can, a small binding that reproduces the problem.
License
BSD 2-Clause. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wrapture-1.0.0.dev7.tar.gz.
File metadata
- Download URL: wrapture-1.0.0.dev7.tar.gz
- Upload date:
- Size: 194.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c106a7fa425a3e1c90c33a9c0291d8889aa2d1d8361ccee8c4e62a33bac1335
|
|
| MD5 |
c5efd1899a45258c34985239a64314cf
|
|
| BLAKE2b-256 |
fcdd5f60b1524ac3a7b835ff668210a447b98e2283604e019d57cf4ef5c08c15
|
Provenance
The following attestation bundles were made for wrapture-1.0.0.dev7.tar.gz:
Publisher:
build-test-release.yml on GrahamDumpleton/wrapture
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wrapture-1.0.0.dev7.tar.gz -
Subject digest:
3c106a7fa425a3e1c90c33a9c0291d8889aa2d1d8361ccee8c4e62a33bac1335 - Sigstore transparency entry: 2502315962
- Sigstore integration time:
-
Permalink:
GrahamDumpleton/wrapture@10e775a05f4aca3b18bf6e82f2157227efbe417a -
Branch / Tag:
refs/tags/1.0.0.dev7 - Owner: https://github.com/GrahamDumpleton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-test-release.yml@10e775a05f4aca3b18bf6e82f2157227efbe417a -
Trigger Event:
push
-
Statement type:
File details
Details for the file wrapture-1.0.0.dev7-py3-none-any.whl.
File metadata
- Download URL: wrapture-1.0.0.dev7-py3-none-any.whl
- Upload date:
- Size: 125.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a878e49b3d36ec95f3700dc5aa2e7c977bc30a60cbcfaea156155d0bbabcebbb
|
|
| MD5 |
adf1c958ea0f05a97d586befe8869066
|
|
| BLAKE2b-256 |
4c17bd60b33262c96fcd84f18c77c19d3e67e04ffdf7c40df258036758203c0c
|
Provenance
The following attestation bundles were made for wrapture-1.0.0.dev7-py3-none-any.whl:
Publisher:
build-test-release.yml on GrahamDumpleton/wrapture
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wrapture-1.0.0.dev7-py3-none-any.whl -
Subject digest:
a878e49b3d36ec95f3700dc5aa2e7c977bc30a60cbcfaea156155d0bbabcebbb - Sigstore transparency entry: 2502316062
- Sigstore integration time:
-
Permalink:
GrahamDumpleton/wrapture@10e775a05f4aca3b18bf6e82f2157227efbe417a -
Branch / Tag:
refs/tags/1.0.0.dev7 - Owner: https://github.com/GrahamDumpleton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-test-release.yml@10e775a05f4aca3b18bf6e82f2157227efbe417a -
Trigger Event:
push
-
Statement type: