Metadata-Version: 2.4
Name: unnotebook
Version: 0.1.0
Classifier: Development Status :: 1 - Planning
Classifier: Programming Language :: Rust
License-File: LICENSE
Summary: Deterministic static analysis and refactoring for data-science code.
Author: William Martin
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/w-martin/unnotebook
Project-URL: Issue Tracker, https://github.com/w-martin/unnotebook/issues
Project-URL: Repository, https://github.com/w-martin/unnotebook

# unnotebook

[![CI](https://github.com/w-martin/unnotebook/actions/workflows/ci.yml/badge.svg)](https://github.com/w-martin/unnotebook/actions/workflows/ci.yml)
[![PyPI version](https://img.shields.io/pypi/v/unnotebook.svg)](https://pypi.org/project/unnotebook/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> ⚠️ **Project status: early proof of concept (v0.1.0).** The recognisers cover a narrow set of notebook shapes, and
> anything outside them is refused rather than guessed at.

**Deterministic static analysis and refactoring for data-science code. `unnotebook` finds bugs in notebooks and
scripts, fixes the ones it can prove, and rewrites a training notebook as a tested, hexagonal Python project.**

- **No LLM, no execution.** Everything is derived from the syntax tree (via the [ruff](https://github.com/astral-sh/ruff)
  parser crates). Your code is never run.
- **Proven fixes only.** A finding is auto-fixed only when the fix is known to be correct; everything else is reported.
- **Refuses instead of guessing.** If a stage of the pipeline is not recognised, `rewrite` stops and lists exactly what
  is missing.
- **A single native binary.** Written in Rust; Python is only the language it analyses.

## Installation

```shell
uv tool install unnotebook
# or
pip install unnotebook
```

The wheel carries only the native executable, so it works with any Python from 3.11. Static Linux executables
(x86_64 and aarch64, musl) with build-provenance attestations are attached to each
[GitHub release](https://github.com/w-martin/unnotebook/releases):

```shell
gh attestation verify unnotebook-x86_64-unknown-linux-musl.tar.gz --repo w-martin/unnotebook
```

## Commands

### `check`

Reports data-science smells in `.py` files and `.ipynb` notebooks, with notebook cell coordinates.

```shell
unnotebook check analysis.ipynb
# analysis.ipynb:cell 3:2:1: API001 `.fillna(inplace=True)` on `df['Age']` updates a copy, so the original is never changed (a no-op under pandas >= 3 copy-on-write)
# analysis.ipynb:cell 3:3:1: API001 `.replace(inplace=True)` on `df['Sex']` updates a copy, so the original is never changed (a no-op under pandas >= 3 copy-on-write)
# Found 2 findings.
```

| Code       | Finding                                                                  |
|------------|--------------------------------------------------------------------------|
| `API001`   | Chained `inplace=True` call that silently updates a copy (auto-fixable)  |
| `LEAK001`  | Test data imputed or standardised with its own statistics, not train's   |
| `LEAK002`  | Encoder or dummies fitted separately on train and test                   |
| `EVAL001`  | Model evaluated on its own training data                                 |
| `CFG001`   | Hard-coded absolute data path                                            |
| `REPRO001` | No random seed set                                                       |

### `fix`

Rewrites files to remove the findings that have a proven fix, in place or into `--output-dir`.

```shell
unnotebook fix analysis.ipynb
# Fixed 2, 0 remaining.
```

### `rewrite`

Turns a notebook, a script or a repository into a modular project:

```shell
unnotebook rewrite --output-dir titanic_pipeline titanic.ipynb
```

The generated project has:

- hexagonal layers (`domain`, `application`, `infrastructure`) with dependencies pointing inward, and thin
  `train`, `evaluate`, `predict` and `serve` entrypoints exposed as console scripts;
- a lazy composition root (`container.py`), so heavy optional libraries (Keras, MLflow, OpenTelemetry) are imported
  only when used;
- preprocessing refitted on the training data only, with the leaks it corrected recorded in `plan.toml`;
- [typedframes](https://github.com/w-martin/typedframes) schemas for the raw inputs, configuration in TOML, structured
  logging, a run manifest and a FastAPI inference service;
- a stage-wise golden-master test that keeps the notebook's own preprocessing verbatim and checks the new pipeline
  against it on synthetic data.

Supported today: Keras `Sequential` models with `Dense` layers and scikit-learn classifiers built from literal
arguments, with median/mode imputation, `get_dummies` one-hot encoding, row-wise feature engineering, and column
selection. Given a repository, the one script that matches becomes the pipeline and the remaining scripts are carried
over verbatim.

## Exit codes

| Code | Meaning                                                                       |
|------|-------------------------------------------------------------------------------|
| `0`  | Clean, or everything was fixed or rewritten                                   |
| `1`  | Findings remain, or `rewrite` could not place some statements                 |
| `2`  | Error: unreadable input, syntax error, or `rewrite` refused the input         |

## Building from source

Requires a Rust toolchain and [uv](https://docs.astral.sh/uv/).

```shell
git clone https://github.com/w-martin/unnotebook
cd unnotebook
uv sync
uv run unnotebook --help
```

The development gate (format, lint, tests) is `uv run inv all`.

## License

[MIT](LICENSE)

