Metadata-Version: 2.4
Name: heki
Version: 0.14.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Rust
Classifier: Topic :: Software Development :: Quality Assurance
License-File: LICENSE
Summary: Static checker for Python that tracks side effects and mutations across function calls
Keywords: linter,static-analysis,effect-analysis,alias-analysis,python,rust
Home-Page: https://github.com/koxudaxi/heki
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/koxudaxi/heki/blob/main/CHANGELOG.md
Project-URL: Documentation, https://heki.koxudaxi.dev
Project-URL: Homepage, https://github.com/koxudaxi/heki
Project-URL: Issues, https://github.com/koxudaxi/heki/issues
Project-URL: Repository, https://github.com/koxudaxi/heki

# heki

<p align="center">
  <img src="https://raw.githubusercontent.com/koxudaxi/heki/main/docs/assets/heki-banner.png" alt="The heki banner, a snake in front of a brick wall" width="480">
</p>

heki (壁, Japanese for "wall") is a static checker for Python, written in Rust. It follows calls
across files to find shared state changed in place, database queries in loops, and blocking calls
in `async` code, and it enforces the boundaries your project declares. It runs alongside Ruff and
your type checker (mypy, Pyright, Pyrefly, or ty), and never runs your code.

📚 [Documentation](https://heki.koxudaxi.dev/) · 🚀 [Getting started](https://heki.koxudaxi.dev/getting-started/) · 📏 [Rules](https://heki.koxudaxi.dev/rules/) · 📜 [Contracts](https://heki.koxudaxi.dev/configuration/#contracts) · 🤖 [Coding agents](https://heki.koxudaxi.dev/coding-agents/)

[![PyPI](https://img.shields.io/pypi/v/heki)](https://pypi.org/project/heki/) [![Python versions](https://img.shields.io/pypi/pyversions/heki)](https://pypi.org/project/heki/) [![CI](https://github.com/koxudaxi/heki/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/koxudaxi/heki/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/koxudaxi/heki/blob/main/LICENSE)

## Highlights

- With no configuration, heki reports cached results changed in place, database queries and new clients in loops, blocking calls in `async` code, and locks taken again while held ([Rules](https://heki.koxudaxi.dev/rules/)).
- The default rules report only what they prove. Every report on 378 packages and a holdout of 100 more was checked by hand, and none was false ([Rule precision](https://heki.koxudaxi.dev/precision/#certifications)).
- `heki check --fix` applies a rewrite only when it proves that the program behaves the same under CPython's semantics ([Fixes and safety](https://heki.koxudaxi.dev/fixes/)).
- heki is a single binary with no Python dependencies, and it checks a 100,000-line project in a few seconds.
- It runs in CI, in editors through its LSP server, and for coding agents as JSON. In our agent runs, it found an N+1 query that had passed the tests, Ruff, and strict mypy ([the measurements](https://heki.koxudaxi.dev/coding-agents/#what-agents-did-with-heki-measurements)).
- Optional contracts in `heki.toml` say who may change, read, or call what, where I/O may happen, and which imports are allowed ([Contracts](https://heki.koxudaxi.dev/configuration/#contracts)).

## Quick start

```console
$ uvx heki check     # check the project in its root, with no install and no configuration
$ uvx heki init      # write heki.toml and a Ratchet baseline, and set up coding agents if you ask
```

The baseline holds today's reports, so only new ones fail. [Getting started](https://heki.koxudaxi.dev/getting-started/) reads a diagnostic and applies a fix.

## Example

```python
# app/config.py
from functools import lru_cache


@lru_cache(maxsize=64)
def base_headers(region: str) -> dict[str, str]:
    return {"Accept": "application/json", "X-Region": region}
```

```python
# app/api.py
from app.config import base_headers


def request_headers(region: str, token: str) -> dict[str, str]:
    headers = base_headers(region)
    headers["Authorization"] = f"Bearer {token}"
    return headers
```

Each file looks fine on its own, and the types are right. But `lru_cache` hands the same dict to
every call with the same `region`, and `request_headers` writes a token into it, so a later caller
of `base_headers` for that region sends the token of whoever called `request_headers` last.

Both files pass Ruff and strict type checking (mypy, Pyright, Pyrefly, and ty). `heki check`
reports `MUT114`.

![heki check in a terminal reports MUT114, with no configuration, for a dict that an lru_cache function returned and another file changes in place](https://raw.githubusercontent.com/koxudaxi/heki/main/docs/assets/example-check.svg)

## Contracts (optional)

Contracts are rules only your project knows, and heki enforces them through helpers and aliases.
This one lets only the functions in `app.store` change `Order.items`.

```toml
[[write-owners]]
name = "order-store"
targets = ["app.models.Order.items"]
allowed-writers = ["app.store.*"]
message = "change Order.items through app.store"
fail = true
```

A write through a helper in another file is then reported as `MUT201` ([the whole example](https://heki.koxudaxi.dev/ownership/#only-the-store-changes-the-order)).
[Contracts](https://heki.koxudaxi.dev/configuration/#contracts) describes all eight contract tables.

## Installation and CI

Install heki with `uv tool install heki`, `pipx install heki`, or `pip install heki`. The prebuilt
wheels for Linux, macOS, and Windows need no Rust toolchain ([Installation](https://heki.koxudaxi.dev/installation/)).
heki is pre-release, so pin the version in CI with `uvx heki==0.14.0 check` and in the pre-commit hook.

```yaml
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/koxudaxi/heki-pre-commit
    rev: v0.14.0
    hooks:
      - id: heki
```

The release notes list every change. [Adopting heki](https://heki.koxudaxi.dev/adoption/) covers baselines and CI,
and says [what stays stable](https://heki.koxudaxi.dev/adoption/#stability-on-the-way-to-10) until 1.0.

## Works alongside

| Tool | Checks | heki adds |
| --- | --- | --- |
| Ruff | syntax and style | what a call does across files |
| mypy, Pyright, Pyrefly, ty | declared types | in-place changes to read-only values through helpers |
| tach, import-linter | import layers | who changes, reads, or calls what, and where I/O happens |

## Documentation

The documentation is at [heki.koxudaxi.dev](https://heki.koxudaxi.dev/), with an index for language models at
[llms.txt](https://heki.koxudaxi.dev/llms.txt). [Design philosophy](https://heki.koxudaxi.dev/philosophy/) says why heki works
the way it does and where its name comes from, and the [Changelog](https://github.com/koxudaxi/heki/blob/main/CHANGELOG.md) lists every release.

heki is licensed under the [MIT license](https://github.com/koxudaxi/heki/blob/main/LICENSE).

