Skip to main content

CodePact

Find Python resource bugs before runtime. Declare which method closes a resource and which APIs own or borrow it. CodePact reads Python source and reports supported use-after-close, missed cleanup, and discarded must-use results. It checks your code without importing or running it.

Quality Docs PyPI helper PyPI CLI MIT license

Documentation · Install options · Rule catalog · Roadmap

New in 0.5.0. Opt in to explicit moves, lexical shared borrows, and a bounded asyncio.TaskGroup model. The default scope retains lifecycle and must-use checks. Ownership guide · Release availability

Quick start

In an existing uv project, install the helper package and its matching CLI.

uv add codepact==0.5.0
mkdir -p demo

Save this as demo/connection.py.

"""Small lifecycle example for `codepact check --root examples`."""

from codepact import resource
from codepact.rust import Borrowed


@resource(closed_by="close")
class Connection:
    def status(self) -> str:
        return "ready"

    def close(self) -> None:
        pass


def inspect_connection(conn: Borrowed[Connection]) -> str:
    return conn.status()


conn = Connection()
inspect_connection(conn)
conn.close()
inspect_connection(conn)  # TP1001 at this call; close is the related location.
uv run --locked codepact check --root demo
connection.py:23:1: error TP1001: Borrowed call requires an open resource
  certainty: definite
  trust: declared-unverified
  repair: Keep the use before close, or guard it with a lifecycle check.
  related: connection.py:22:1
1 diagnostics; 1 actionable; 0 suppressed; 0 rule-disabled; 1 returned

The check exits 1. Remove the call after conn.close() and rerun the same command to get exit 0. The example uses a stub close method. CodePact checks the declared state transition, while your implementation supplies the actual cleanup. Quick start and report walkthrough

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 examples

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 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.5.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.5.0
File Size Uploaded Provenance Actions
codepact-0.5.0.tar.gz 5.5 kB Without provenance Details for codepact-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 11.8 kB

Release files / codepact-0.5.0.tar.gz

Download URL codepact-0.5.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
07bd6d166456e22925a72e54beae4596f1755d65bcbc0fa37af022bc457ce678
BLAKE2b-256 checksum
How to use checksums
2006266ac94f19fe900069a0d535f6700b346839dd511dc0620062f7751ac1bb
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.5.0-py3-none-any.whl

Download URL codepact-0.5.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6e363e46779d29a6947de9a00293b2425e28748122c96eeb7df892fab8b46d2
BLAKE2b-256 checksum
How to use checksums
a31989065c952951b690706b73008279269967819522580b5193c00610caa4c2
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.5.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