Metadata-Version: 2.5
Name: gruff-py
Version: 0.6.0
Summary: Python project quality analyser.
Project-URL: Homepage, https://github.com/blundergoat/gruff-py
Project-URL: Repository, https://github.com/blundergoat/gruff-py
Project-URL: Issues, https://github.com/blundergoat/gruff-py/issues
Project-URL: Changelog, https://github.com/blundergoat/gruff-py/blob/main/CHANGELOG.md
Author: Matthew Hansen
License: MIT
License-File: LICENSE.md
Keywords: code-quality,code-review,dashboard,dead-code,linter,quality-gate,sarif,security-scanner,static-analysis,test-quality
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: click>=8.1
Requires-Dist: docstring-parser<1,>=0.15
Requires-Dist: pathspec>=0.12
Requires-Dist: pyyaml>=6.0
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pip-audit>=2.10.0; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Requires-Dist: types-pyyaml>=6.0; extra == 'dev'
Description-Content-Type: text/markdown

# gruff-py

`gruff-py` is the Python port of **gruff**, a static quality analyser built to keep AI-generated code legible, safe, and reviewable. It walks Python projects, scores findings across quality pillars, and outputs reports for terminals, CI annotations, SARIF code scanning, static HTML, and a local dashboard.

## Mission

A coding agent wrote the change, and a person who did *not* write it has to review and trust it. Every rule, severity, and score serves that reviewer, not abstract style. Wired into a coding-agent hook, gruff-py **guides** the agent with advisory findings and **gates** it with `--fail-on` warning and error findings so changes stay:

- **Legible enough to verify** — a reviewer can follow the control flow and confirm it does what was asked, without holding the whole function in their head.
- **Secure where the eye fails** — it flags the dangerous patterns and leaked secrets human review is worst at catching, because those are the failures the eye skips.
- **Tested for real, not padded** — it rewards tests that exercise behaviour and flags low-signal ceremony (assertion-free tests, tautologies, mock-only theatre) that inflates coverage without earning trust.

Doc comments are mandatory, even on a private one-liner. Agents often write code that looks like it works while misreading the requirement; stating intent, usage, contract, and failure behaviour gives the reviewer something to check the code against, and a mismatch is itself a reason to look closer.

gruff-py is heuristic static analysis. It complements `ruff`, `mypy`, `pytest`, security scanners, and human review; it does not replace them. [docs/mission.md](https://github.com/blundergoat/gruff-py/blob/main/docs/mission.md) has the full statement.

## Status At A Glance

| Field | Value |
| --- | --- |
| Release line | `0.6.0` package line |
| Runtime | Python `3.11+` |
| Package | `gruff-py` |
| Import package | `gruffpy` with `py.typed` |
| Binary | `gruff-py` |
| Rule catalogue | Generated from `RuleRegistry.defaults()`; see [Rules](https://github.com/blundergoat/gruff-py/blob/main/docs/rules.md) |
| Primary config | `.gruff-py.yaml`; `[tool.gruff-py]` in `pyproject.toml` is also supported |
| Analysis schema | `gruff.analysis.v3` |
| Baseline schema | `gruff.baseline.v3`; `gruff-py.baseline.v1` and `gruff.baseline.v1` fail closed with a migration command |
| Severity gate | `--fail-on` with `none`, `advisory`, `warning`, `error`; project default via `failOn:` in `.gruff-py.yaml` / `pyproject.toml` |
| Dashboard | `127.0.0.1:8765` by default |

Finding fingerprints are 16-character SHA-256 prefixes kept byte-compatible with `gruff-php` wherever rule and finding identities match. Analysis JSON uses the shared `gruff.analysis.v3` schema, while hotspot and config schemas stay language-prefixed (`gruff-py.*`).

Each JSON finding also includes `stableIdentity`, a line-insensitive companion to `fingerprint` that lets baselines and external diff tools match the same logical finding across line shifts. See [`docs/output-formats.md`](https://github.com/blundergoat/gruff-py/blob/main/docs/output-formats.md#finding-identity) for the exact fields hashed into each identity.

## Requirements

- Python `3.11+`.
- `uv` for source-checkout development.
- Git only for diff and branch-review modes.
- Optional external mutation tooling only when mutation flags are explicitly used.

## Install

Install as a project dev dependency:

```bash
uv add --dev gruff-py
uv run gruff-py init
uv run gruff-py summary
```

From a source checkout:

```bash
uv sync
uv run gruff-py --help
./bin/gruff-py --help
```

## Quick Start

```bash
# Create the project config.
uv run gruff-py init

# Review the current finding mix.
uv run gruff-py summary

# Explore without failing because of findings.
uv run gruff-py analyse src/ --fail-on none

# Gate on warning and error findings.
uv run gruff-py analyse src/ --fail-on warning

# Emit SARIF for code scanning.
uv run gruff-py analyse src/ --format sarif --fail-on none > gruff.sarif

# Generate a fresh-start baseline.
uv run gruff-py analyse src/ --generate-baseline --fail-on none

# Start the local dashboard.
uv run gruff-py dashboard src/ --report-interactive
```

Open `http://127.0.0.1:8765/` for the dashboard.

## Commands

| Command | Purpose |
| --- | --- |
| `init` | Write a default `.gruff-py.yaml` to the current directory. |
| `analyse [paths...]` | Run the analyser and print findings. |
| `summary [paths...]` | Print compact score, pillar, rule, and file summaries. |
| `report [paths...]` | Render an HTML or JSON report to stdout or `--output`. |
| `list-rules [rule-id]` | Print rule metadata as text or JSON; pass a rule id for explain mode. |
| `check-ignore [paths...]` | Report whether each path is ignored, and why (exit codes mirror `git check-ignore`). |
| `hook [paths...]` | Run analysis for a coding-agent hook; emits `gruff.hook.v2` JSON. |
| `migrate-config` | Rewrite legacy config keys to the current schema; `--dry-run` prints the diff. |
| `dashboard [paths...]` | Serve the local browser dashboard. |
| `completion [shell]` | Print a shell completion script. |
| `list`, `help` | Show command lists and command-specific help. |

Global options mirror the broader gruff CLI surface: `--silent`, `--quiet`, `--version`, `--ansi` / `--no-ansi`, `--no-interaction`, and `--verbose`.

## Output Formats

`analyse --format <fmt>` accepts:

| Format | Use it for |
| --- | --- |
| `text` | Human terminal output. |
| `json` | Full `gruff.analysis.v3` report. |
| `html` | Self-contained inspection report. |
| `markdown` | Pull-request or issue comment summary. |
| `github` | GitHub Actions workflow annotations. |
| `hotspot` | `gruff-py.hotspot.v1` file-offender JSON. |
| `sarif` | SARIF 2.1.0 for code scanning. |

`report --format <fmt>` accepts `html` and `json`.

## Exit Codes

| Code | Meaning |
| --- | --- |
| `0` | Run completed and no finding met `--fail-on`. |
| `1` | At least one finding met `--fail-on`. |
| `2` | Fatal diagnostic such as input, parse, configuration, baseline, or diff failure. |

`--fail-on` gates findings only. A parse error is a fatal diagnostic and exits
`2` even with `--fail-on none`; `none` makes findings report-only, not
diagnostics.

`analyse` defaults to `--fail-on advisory`. Set
`failOn.analyse` in `.gruff-py.yaml` to change the default
per-project (see [docs/configuration.md](https://github.com/blundergoat/gruff-py/blob/main/docs/configuration.md#severity-gate)).

## CI Usage

Generic CI command:

```bash
uv run gruff-py analyse src tests --format github --fail-on warning
```

SARIF jobs can write an artifact for code scanning:

```bash
uv run gruff-py analyse src tests --format sarif --fail-on none > gruff-py.sarif
```

Use `--no-baseline` on security gates, so a finding already recorded in the adoption baseline still fails the run.

## Configuration

Config is optional. Precedence is:

1. `--config <path>`
2. `.gruff-py.yaml` in the project root
3. `[tool.gruff-py]` in `pyproject.toml`
4. Built-in defaults

Example `.gruff-py.yaml`:

```yaml
schemaVersion: gruff-py.config.v0.1

paths:
  ignore:
    - "tests/fixtures/**"

selection:
  excludeRules:
    - docs.missing-module-docstring

rules:
  size.file-length:
    threshold: 900
    severity: error
```

See [Configuration](https://github.com/blundergoat/gruff-py/blob/main/docs/configuration.md) for the full shape.

## Rules And Pillars

[`docs/rules.md`](https://github.com/blundergoat/gruff-py/blob/main/docs/rules.md) is generated from `RuleRegistry.defaults()` and lists every rule ID, pillar, default, and remediation note. Check that the committed copy is current without rewriting it:

```bash
uv run python -m gruffpy.command.rule_docs --check docs/rules.md
```

`coupling`, `architecture`, and `mutation` are reserved schema names with no shipping rules in `0.6.0`. `modernisation` and `maintainability` lost their last rules in `0.6.0` but still count toward the composite, scoring 100 whenever files are evaluated.

## Baselines And Changed-Code Scans

Baselines suppress reviewed findings by a line-free identity and a count, so an unrelated edit
that shifts line numbers does not re-open reviewed debt:

```bash
uv run gruff-py analyse src/ --generate-baseline --fail-on none
uv run gruff-py analyse src/ --baseline gruff-baseline.json --fail-on warning
uv run gruff-py analyse src/ --no-baseline --fail-on none
```

Changed-code scans are changed-region aware: a finding is kept when its location
or enclosing function/class overlaps the changed hunk. JSON output reports the
findings excluded as out of scope at `summary.suppressedFindings` and
`diff.filteredFindings`.

```bash
uv run gruff-py analyse --format json --changed-ranges "3-3,8-10" src/foo.py
uv run gruff-py analyse --format json --since HEAD src/foo.py
git diff | uv run gruff-py analyse --format json --diff - src/foo.py
```

Narrow path scans that leave project-wide rules enabled include a partial-context
caveat in JSON run metadata and text output because conclusions can differ from
a full-project scan.

`--exclude-rule`, `--include-rule`, `--exclude-pillar`, and `--include-pillar` are execution-level:
the excluded rules do not run, so the score and the exit code move with them. Display filters such as
`--min-severity`, `--hide-rule`, and `--show-rule` reduce rendered `analyse` output without changing
which rules execute, the score, or the exit code. Text output discloses when findings are hidden by
these filters.

## Dashboard

```bash
uv run gruff-py dashboard src/ --host 127.0.0.1 --port 8765 --report-interactive
```

The dashboard serves a local browser UI for repeated scans. It has no
authentication and is intended for local development. Non-loopback hosts are
refused unless you pass `--allow-public`; that acknowledgment warns that remote
users can scan any directory readable by the server process. See
[Dashboard](https://github.com/blundergoat/gruff-py/blob/main/docs/dashboard.md) for supported controls and safety notes.

In polyglot repositories, `gruff-go`, `gruff-php`, and `gruff-py` all default to port `8765` (`gruff-rs` uses `8766` and `gruff-ts` `8767`); pass `--port` when running several dashboards at once.

## Trust Boundary

Default scans are local source inspections. `gruff-py` parses Python source and selected project metadata; it does not execute target application code, run tests, query vulnerability feeds, or contact package registries. Git is used only for explicit diff modes. External mutation tooling is used only when explicitly requested by mutation flags. Sensitive-data previews are redacted before they reach terminal, JSON, SARIF, GitHub, Markdown, hotspot, or HTML output.

## Stability Contract

Through the `0.x` line, rule IDs, finding fingerprints, baseline identity, `gruff.analysis.v3`, `gruff.summary.v3`, `gruff.baseline.v3`, `gruff-py.hotspot.v1`, `gruff.hook.v2`, SARIF rendering, and CLI exit semantics are compatibility-sensitive. Before `1.0`, a minor bump (such as `0.5.x` to `0.6.0`) may break them; every break carries a `BREAKING:` marker and a migration path in [`CHANGELOG.md`](https://github.com/blundergoat/gruff-py/blob/main/CHANGELOG.md), and [`UPGRADING.md`](https://github.com/blundergoat/gruff-py/blob/main/UPGRADING.md) explains how to migrate or pin back.

## How It Compares

| Tool | Relationship |
| --- | --- |
| `ruff` | Fast linting and formatting. `gruff-py` adds scoring, baselines, reports, dashboard, and project-quality rules. |
| `mypy` / Pyright | Type checking. `gruff-py` does not prove type correctness. |
| `pytest` | Runtime tests. `gruff-py` can flag test-quality smells but does not prove behavior. |
| Bandit / Semgrep / vulnerability scanners | Security-focused checks. `gruff-py` reports local static signals and does not replace specialized scanners. |
| Vulture / dead-code tools | Focused unused-code detection. `gruff-py` includes broader quality scoring and reporting. |

## Development

```bash
uv sync --extra dev
uv run ruff check src tests
uv run ruff format --check src tests
uv run mypy src
uv run pytest
make check
```

`make check` uses the `lint` target, which runs `ruff check --fix`. Use the explicit commands above when you need non-mutating release verification.

## Documentation

- [Mission](https://github.com/blundergoat/gruff-py/blob/main/docs/mission.md)
- [Documentation index](https://github.com/blundergoat/gruff-py/blob/main/docs/README.md)
- [Upgrading](https://github.com/blundergoat/gruff-py/blob/main/UPGRADING.md)
- [Changelog](https://github.com/blundergoat/gruff-py/blob/main/CHANGELOG.md)
- [Configuration](https://github.com/blundergoat/gruff-py/blob/main/docs/configuration.md)
- [Rules](https://github.com/blundergoat/gruff-py/blob/main/docs/rules.md)
- [Output formats](https://github.com/blundergoat/gruff-py/blob/main/docs/output-formats.md)
- [Coding-agent hook](https://github.com/blundergoat/gruff-py/blob/main/docs/agent-hook.md)
- [CI integration](https://github.com/blundergoat/gruff-py/blob/main/docs/ci-integration.md)
- [Triage a noisy run](https://github.com/blundergoat/gruff-py/blob/main/docs/triage.md)
- [Explain a rule](https://github.com/blundergoat/gruff-py/blob/main/docs/explain.md)
- [Reports](https://github.com/blundergoat/gruff-py/blob/main/docs/reporting.md)
- [Dashboard](https://github.com/blundergoat/gruff-py/blob/main/docs/dashboard.md)
- [Release checklist](https://github.com/blundergoat/gruff-py/blob/main/docs/releasing.md)
- [Contributing](https://github.com/blundergoat/gruff-py/blob/main/CONTRIBUTING.md)
- [Code of Conduct](https://github.com/blundergoat/gruff-py/blob/main/CODE_OF_CONDUCT.md)
- [Security](https://github.com/blundergoat/gruff-py/blob/main/SECURITY.md)
- [Support](https://github.com/blundergoat/gruff-py/blob/main/SUPPORT.md)

## Author

Built by [Matthew Hansen](https://www.blundergoat.com/about).

## License

[MIT](https://github.com/blundergoat/gruff-py/blob/main/LICENSE.md)
