Skip to content

Add a type stub for the compiled IM._core extension - #254

Merged
lispandfound merged 2 commits into
masterfrom
ai/issue-242
Oct 6, 2026
Merged

lispandfound merged 2 commits into
masterfrom
ai/issue-242

Conversation

@ucgmsim

@ucgmsim ucgmsim Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Closes #242

What was wrong

The Rust extension IM._core had no .pyi stub. Both IM/ims.py and IM/konno_ohmachi.py imported it with # ty: ignore[unresolved-import], so ty treated _core as unknown and never checked any call into the core.

What changed

  • IM/_core.pyi (new): signatures for all seven #[pyfunction]s in src-rust/lib.rs (_arias_intensity, _cav, _konno_ohmachi_matrix_rows, _konno_ohmachi_smooth, _psa, _rotd, _significant_duration). Parameter names match the runtime ones, and return types give the dtype and number of dimensions (float32 for the Konno-Ohmachi matrix rows, float64 everywhere else).
  • IM/ims.py, IM/konno_ohmachi.py: removed the ty: ignore[unresolved-import] comments.
  • IM/ims.py (_psa call): once the stub was in place, ty flagged _core._psa(*chunk, coefficients), because it can't tell that chunk unpacks into exactly three components. I changed it to unpack explicitly first (comp_0, comp_90, comp_ver = chunk). Nothing it computes changes.

Tests

tests/test_core_stub.py checks that the stub file exists, that it declares exactly the functions the compiled _core exports, and that each function's parameter names in the stub match inspect.signature on the real extension. Before the change all 9 tests failed because there was no stub. They now pass, and will fail if someone adds or renames a #[pyfunction] without updating the stub. .ai/ci-checks passes in full (ruff, ty, deptry, npdlint, pytest, cargo).

For reviewers

  • Input types are loose on purpose. Array arguments are typed as plain np.ndarray, not shape- or dtype-specific arrays. At runtime PyO3 enforces C-contiguous 2-D float64, and the stub's docstring says so. Tighter input types would mean adding casts at every call site, which is a bigger change than this issue asks for.
  • Packaging: the stub sits next to the compiled extension inside the IM package. With setuptools-scm and the default include-package-data, it should go into wheels once committed. No py.typed marker was added, since the issue didn't ask for one.

Cost $0.45, 23 turns.

🤖 Generated with Claude Code

Closes #242

## What was wrong

The Rust extension `IM._core` had no `.pyi` stub. Both `IM/ims.py` and `IM/konno_ohmachi.py` imported it with `# ty: ignore[unresolved-import]`, so `ty` treated `_core` as unknown and never checked any call into the core.

## What changed

- **`IM/_core.pyi`** (new): signatures for all seven `#[pyfunction]`s in `src-rust/lib.rs` (`_arias_intensity`, `_cav`, `_konno_ohmachi_matrix_rows`, `_konno_ohmachi_smooth`, `_psa`, `_rotd`, `_significant_duration`). Parameter names match the runtime ones, and return types give the dtype and number of dimensions (`float32` for the Konno-Ohmachi matrix rows, `float64` everywhere else).
- **`IM/ims.py`, `IM/konno_ohmachi.py`**: removed the `ty: ignore[unresolved-import]` comments.
- **`IM/ims.py` (`_psa` call)**: once the stub was in place, `ty` flagged `_core._psa(*chunk, coefficients)`, because it can't tell that `chunk` unpacks into exactly three components. I changed it to unpack explicitly first (`comp_0, comp_90, comp_ver = chunk`). Nothing it computes changes.

## Tests

`tests/test_core_stub.py` checks that the stub file exists, that it declares exactly the functions the compiled `_core` exports, and that each function's parameter names in the stub match `inspect.signature` on the real extension. Before the change all 9 tests failed because there was no stub. They now pass, and will fail if someone adds or renames a `#[pyfunction]` without updating the stub. `.ai/ci-checks` passes in full (ruff, ty, deptry, npdlint, pytest, cargo).

## For reviewers

- **Input types are loose on purpose.** Array arguments are typed as plain `np.ndarray`, not shape- or dtype-specific arrays. At runtime PyO3 enforces C-contiguous 2-D `float64`, and the stub's docstring says so. Tighter input types would mean adding casts at every call site, which is a bigger change than this issue asks for.
- **Packaging:** the stub sits next to the compiled extension inside the `IM` package. With setuptools-scm and the default `include-package-data`, it should go into wheels once committed. No `py.typed` marker was added, since the issue didn't ask for one.

_Cost $0.45, 23 turns._

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ucgmsim ucgmsim Bot added ai-generated Opened by the scheduled Claude jobs ai-fix Bot-written fix that needs review labels Oct 5, 2026
@ucgmsim
ucgmsim Bot requested a review from lispandfound October 5, 2026 13:26
@lispandfound
lispandfound merged commit f91ed7d into master Oct 6, 2026
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-fix Bot-written fix that needs review ai-generated Opened by the scheduled Claude jobs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add .pyi stub

1 participant