Repository navigation
Add a type stub for the compiled IM._core extension - #254
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #242
What was wrong
The Rust extension
IM._corehad no.pyistub. BothIM/ims.pyandIM/konno_ohmachi.pyimported it with# ty: ignore[unresolved-import], sotytreated_coreas unknown and never checked any call into the core.What changed
IM/_core.pyi(new): signatures for all seven#[pyfunction]s insrc-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 (float32for the Konno-Ohmachi matrix rows,float64everywhere else).IM/ims.py,IM/konno_ohmachi.py: removed thety: ignore[unresolved-import]comments.IM/ims.py(_psacall): once the stub was in place,tyflagged_core._psa(*chunk, coefficients), because it can't tell thatchunkunpacks 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.pychecks that the stub file exists, that it declares exactly the functions the compiled_coreexports, and that each function's parameter names in the stub matchinspect.signatureon 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-checkspasses in full (ruff, ty, deptry, npdlint, pytest, cargo).For reviewers
np.ndarray, not shape- or dtype-specific arrays. At runtime PyO3 enforces C-contiguous 2-Dfloat64, 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.IMpackage. With setuptools-scm and the defaultinclude-package-data, it should go into wheels once committed. Nopy.typedmarker was added, since the issue didn't ask for one.Cost $0.45, 23 turns.
🤖 Generated with Claude Code