Metadata-Version: 2.5
Name: syntopica-codeality-py
Version: 0.3.0
Summary: Structural linter and config scaffolder for Python: atomic files, placement, size caps, no inline SQL
Project-URL: Homepage, https://github.com/syntopica/codeality
Project-URL: Issues, https://github.com/syntopica/codeality/issues
Author: Cristian Deluxe
License: MIT License
        
        Copyright (c) Syntopica
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: architecture,ast,code-quality,linter
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.11
Requires-Dist: click<9,>=8.1
Requires-Dist: tomlkit<1,>=0.13
Description-Content-Type: text/markdown

# codeality-py

Structural linter and config scaffolder for Python: atomic files, placement,
size caps, no inline SQL. Brings the same discipline as
`@syntopica/eslint-config` and `cargo-baseline` to Python projects.

- **Design spec:**
  [docs/superpowers/specs/2026-08-31-python-baseline-design.md](https://github.com/syntopica/codeality/blob/main/docs/superpowers/specs/2026-08-31-python-baseline-design.md).

## Install

```bash
uv add --dev syntopica-codeality-py
```

## Usage

```bash
codeality-py init [--check|--apply|--force]  # scaffold ruff, mypy, deptry,
                                            # pytest, coverage and CI config
codeality-py check [PATH...]                 # run the structural rules
codeality-py gate                            # run the whole quality chain
codeality-py baseline create|update|check    # track migration debt
codeality-py affected [--base REF] [-- ...]  # run only the tests a change reaches
codeality-py test-audit [--json]             # find what makes the suite costly
```

`init` never overwrites: it merges into existing configuration, or reports a
conflict. `check` never writes. `gate` fails when a required tool is absent
rather than skipping it.

## Inner loop: `affected`

`codeality-py affected` runs pytest on the test files that import, directly or
transitively, a Python file changed since `--base` (default `HEAD`: committed
since, staged, unstaged and untracked). It builds a static import graph with
`ast`, so there is no state to keep, no first full run and no new dependency.
`pytest-testmon` was rejected for the opposite reasons: it keeps a
`.testmondata` database, traces coverage on every run and needs a full run
first. `--plan` prints the pytest command without running it, and everything
after `--` goes to pytest. When `addopts` collects coverage, `--no-cov` is
added, because a subset would fail the coverage threshold.

The whole suite runs when a change can reach every test without an import: a
`conftest.py`, `pytest.ini`, `tox.ini`, `setup.cfg`, `pyproject.toml`, a
lockfile, an `.env*` file, or a deleted module. When no test reaches the changed
files, the command says they are untested and never reports a pass. Every run
ends by saying the full gate has not run. Git runs without `GIT_DIR`,
`GIT_WORK_TREE` and `GIT_INDEX_FILE`, so it works inside a hook of a linked
worktree.

Dynamic imports are followed only for a literal `importlib.import_module("x")`
or `__import__("x")`. Declare the rest in `codeality-py.toml`:

```toml
[affected]
widen = ["fixtures/**"]          # a change here runs the whole suite

[[affected.map]]
when = "migrations/*.sql"        # a non-Python file...
to = ["src/app/db"]              # ...counts as a change to these paths
```

## Suite audit: `test-audit`

Findings exit 1; observations never change the exit code. Both print their
evidence, and `--json` emits `schemaVersion`, `findings` and `observations`.

| Name                  | Kind        | Reported when                                                               |
| --------------------- | ----------- | --------------------------------------------------------------------------- |
| `coverage-in-addopts` | finding     | `addopts` has `--cov*`, so every run pays for tracing.                      |
| `missing-testpaths`   | finding     | No `testpaths` and a bare run walks 100 or more directories with no test.   |
| `real-sleep-in-tests` | finding     | A literal `time.sleep(n)` or `asyncio.sleep(n)` of 0.5s or more in a test.  |
| `xdist-candidate`     | observation | 40 or more test files and no `-n` in `addopts`.                             |
| `hook-runs-full-gate` | observation | A lefthook, husky or `.githooks` hook runs `pytest` or `codeality-py gate`. |

## Rules

| Code     | Rule                | Enforces                                                       |
| -------- | ------------------- | -------------------------------------------------------------- |
| `BPY000` | `parse-error`       | A file that fails to parse is a finding, never a silent skip.  |
| `BPY001` | `one-primary-unit`  | Exactly one primary declaration per ordinary module.           |
| `BPY002` | `file-matches-unit` | File name is the snake_case of its primary declaration.        |
| `BPY003` | `no-grab-bag-names` | No `utils`, `helpers`, `misc` or `common` in any path segment. |
| `BPY004` | `max-file-lines`    | Code-line cap per file; tests get their own, looser cap.       |
| `BPY005` | `barrel-only-init`  | `__init__.py` holds imports, `__all__` and nothing else.       |
| `BPY006` | `no-inline-sql`     | SQL lives in resource files, not in string literals.           |

## Suite time budget

```toml
# codeality-py.toml
test-budget-seconds = 300
```

Past the budget a passing suite is a finding: the pytest stage reports
`over-budget`, exits 1, and its detail carries the slowest tests. The
procedure - profile, make the unit under test cheaper, parallelise, serialise
what must stay shared - is in
[docs/standards/testing.md](https://github.com/syntopica/codeality/blob/main/docs/standards/testing.md#suite-time-budget).

The budget is tuned to the machine the suite is developed on. A slower
environment, such as a CI runner, sets its own for the run instead of raising
the committed number for everyone:

```yaml
# .github/workflows/quality.yml
env:
  CODEALITY_PY_TEST_BUDGET_SECONDS: '600'
```

When `CODEALITY_PY_TEST_BUDGET_SECONDS` is set it replaces
`test-budget-seconds`; unset or empty, it changes nothing. A value that is not a
positive integer is a configuration error (exit 2), never silently ignored. The
pytest stage names the budget it was held to and where it came from -
`budget 600s from CODEALITY_PY_TEST_BUDGET_SECONDS` - in its text line, its
detail, and the `budget_seconds` and `budget_source` fields of `gate --json`.

## Exit codes

| Code | Meaning                                                         |
| ---- | --------------------------------------------------------------- |
| 0    | All blocking checks passed.                                     |
| 1    | Policy findings.                                                |
| 2    | Invalid usage or invalid configuration.                         |
| 3    | Infrastructure failure: a required tool is missing or unusable. |

## License

MIT
