Metadata-Version: 2.4
Name: heki
Version: 0.14.1
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

- [Bugs found with no configuration](https://heki.koxudaxi.dev/rules/)
  - 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.
- [Default rules report only what they prove](https://heki.koxudaxi.dev/precision/#certifications)
  - Every report of the default rules on 378 packages and a holdout of 100 more was checked by hand, and none was false.
- [Fixes that keep behavior](https://heki.koxudaxi.dev/fixes/)
  - `heki check --fix` rewrites code only when it proves that the results, the calls into other code, and the changes to shared objects stay the same.
- [A single fast binary](https://heki.koxudaxi.dev/installation/)
  - It has no Python dependencies and checks a 100,000-line project in a few seconds.
- [CI, editors, and coding agents](https://heki.koxudaxi.dev/coding-agents/)
  - It runs in CI, in editors through its LSP server, and for coding agents as JSON. In [our agent runs](https://heki.koxudaxi.dev/coding-agents/#what-agents-did-with-heki-measurements), it found an N+1 query that had passed the tests, Ruff, and strict mypy.
- [Optional contracts](https://heki.koxudaxi.dev/configuration/#contracts)
  - Contracts in `heki.toml` say who may change, read, or call what, where I/O may happen, and which imports are allowed.

## 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.1 check` and in the pre-commit hook.

```yaml
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/koxudaxi/heki-pre-commit
    rev: v0.14.1
    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.

## Where heki fits

### With no configuration

| If you know | It gives you | heki adds, with no configuration |
| --- | --- | --- |
| Ruff | style and syntax, one file at a time | what a call does across files |
| mypy, Pyright, Pyrefly, ty | declared types | parameters typed read-only changed through helpers |
| Tests, profilers, monitoring | problems on the paths that ran | `async def` reaching blocking calls, queries and new clients in loops, and locks taken again while held, before the code runs |
| Borrowing (Rust) | a shared borrow cannot change the value | a shared cached result changed in place by its caller |

### With heki.toml

Each of these takes a contract. `heki init` writes the contracts it can propose from the code, commented out.

| If you know | It gives you | heki adds, with `heki.toml` |
| --- | --- | --- |
| tach, import-linter | which module may import which | writes, calls, and I/O that cross a layer even through allowed imports (a contract) |
| Code review, `AGENTS.md` | rules people and agents have to remember | the same rules, checked on every run (contracts) |
| Ownership (Rust) | one owner decides who may change a value | who may change, read, or call what (a contract) |
| Haskell IO, Effect-TS | the type marks the functions that do I/O | any scope you mark pure, checked for I/O through helpers without rewriting your code (a contract) |

## 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).

