Python Engineering Guidelines
When modifying Python code in crates/locus-py or scripts/, the focus must be on maximizing throughput at the FFI boundary, ensuring type safety, and maintaining reproducible environments.
1. FFI & Zero-Copy Rules
- No Hot-Loop Allocations: Do not slice, copy, or instantiate large arrays inside the tight detection loop.
- Contiguous Memory: Ensure image data passed to Rust is contiguous. Rely on Rust's
PyReadonlyArray2<u8>to interpret the buffer securely.
2. Typing & API Surface
- Strict Typing: All Python code must be fully type-hinted. We rely on
basedpyright(via thetypesdependency group) to enforce static typing. - Stub Synchronization:
locus/locus.pyiis generated from the annotated pyo3 surface — do not hand-edit it. After changing the PyO3 interface, regenerate and commit it withcargo run --bin stub_gen --no-default-features --features profiles,stub-gen(underuv run); CI'sstub_gen --checkfails on drift. New#[pyclass]/#[pymethods]/#[pyfunction]items need the matching#[gen_stub_*]annotation to appear in the stub.
3. Environment & Orchestration
uvEverywhere: Never use globalpip. All dependency management, locking, and tool execution must be routed throughuv.- PEP 735 Dependency Groups: Utilize specific groups (
dev,lint,types,bench,docs,etl) when running tasks. For example:uv run --group bench tools/cli.py bench.
4. Quality Gates
- Linting:
uv run ruff check . --fix - Formatting:
uv run ruff format . - Type Checking:
uv run --group types --group bench --group etl basedpyright - Testing:
uv run pytest