Skip to main content

CodePact

CodePact banner

CodePact finds Python resource bugs before runtime. Declare how an object closes and which functions own or borrow it. CodePact reads source and can flag a connection used after close, cleanup skipped on a supported exit, or a discarded must-use result. Run it alongside your type checker and tests.

Quality Docs PyPI helper PyPI CLI MIT license

Documentation · Install options · Rule catalog · Roadmap

New in 0.7.0. Add typed Result and Option values, opt in to finite outcome-match checks, and install the pure-Python helpers separately from the native CLI. Typed outcomes · Installation options

Quick start

In an empty directory with uv and Python 3.11 or later, start a project.

uv init --bare --name codepact-demo
uv add 'codepact[cli]==0.7.0'
uv run --locked codepact --version
mkdir src

Save the complete connection example as src/connection.py and run the check.

uv run --locked codepact check --root src

Expect exit 1 with a related location pointing to the close.

connection.py:23:1: error TP1001: Borrowed call requires an open resource
  related: connection.py:22:1

Delete the final line and rerun with --mode strict to confirm exit 0. Full walkthrough · Install with pip or run the standalone command

What you can check

Declare CodePact checks
@resource(closed_by="close") Supported use after close and conflicting lifecycle metadata
Owned[T] Cleanup duties across modeled exits and open resources at ownership handoff
Borrowed[T] Closing a resource through a shared view
@must_use or MustUse[T] Bare discarded calls to marked synchronous functions

The helpers use typing.Annotated, so ordinary types remain available to mypy and Pyright. The checker follows supported branches, aliases, calls, exceptions, and cleanup paths. Check reports include primary and related source locations, certainty, and coverage. Contract syntax · Supported syntax

Try experimental ownership

Add the following to pyproject.toml to enable move and lexical borrow checks.

[tool.codepact]
config-version = 1
experimental-ownership = true

Use move(value) to transfer a tracked handle and its cleanup duty. The checker reports later use of the old handle. The TaskGroup model requires a separate default-task-factory assumption. Ownership examples and scope

Run in CI

Start with a narrow source root. Use strict mode when incomplete coverage should fail the job, and retain the JSON report alongside the exit status.

uv run --locked codepact check --root src --mode strict --json > codepact-report.json
Exit Meaning
0 The completed report passed the selected policy
1 The completed report failed policy
2 The invocation or check failed

Advisory mode can pass with incomplete coverage. The coverage field describes which parts of the program the checker could analyze. Teams can configure rule severity, review a baseline, and record line-level exceptions with a reason. Adoption guide · CI workflow

Compatibility

The helper package runs on Python 3.11 and later, with CI on CPython 3.11–3.14. The analyzer supports Python 3.11–3.13 source targets. Native CLI wheels are available for Linux, macOS, and Windows on x86-64 and ARM64. Building from source requires Rust 1.96 or later and a linker. Platform matrix · Standalone CLI and Rust library

Contribute

Run uv sync --locked --extra cli and uv run --locked make check after installing the repository's pinned Cargo audit tools. The contributor guide lists the setup and focused check targets. Contributing · Build the docs · MIT license

Metadata

Release files for codepact 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codepact 0.7.0
File Size Uploaded Provenance Actions
codepact-0.7.0.tar.gz 6.8 kB Without provenance Details for codepact-0.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for codepact 0.7.0
File Interpreter ABI Platform Actions
codepact-0.7.0-py3-none-any.whl Python 3 none any Details for codepact-0.7.0-py3-none-any.whl

Total release size: 15.0 kB

Release files / codepact-0.7.0.tar.gz

Download URL codepact-0.7.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b03ee4c22e048d784c90fb6c006b9428d3267f2d1f02a72468c5b40dfa8fb52e
BLAKE2b-256 checksum
How to use checksums
d303f3ea7ef3703dcce2388b68cb61b88cc1ed0b87f1a3ddca22f23d5c9a432b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / codepact-0.7.0-py3-none-any.whl

Download URL codepact-0.7.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c82f8d26afa8e4d94aa0e512e13e0775632ee8869b905ab1ff8af31cea84448d
BLAKE2b-256 checksum
How to use checksums
e51443c153ec4ad45ed3c5eb9a33d73ca5519c9cb9ae9b98f55b1a01ee6199b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for codepact 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page