Skip to content

ENH: Add type annotations to ICA() parameters - #14583

Open
draarivpatel-ui wants to merge 1 commit into
mne-tools:mainfrom
draarivpatel-ui:fix-ica-type-hints-13597
Open

draarivpatel-ui wants to merge 1 commit into
mne-tools:mainfrom
draarivpatel-ui:fix-ica-type-hints-13597

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Reference issue (if any)

Closes #13597.

What does this implement/fix?

Adds type annotations to the ICA.__init__ signature so that calls accepted at runtime and documented, such as ICA(n_components=3, max_iter=1000), no longer produce false positives in static type checkers. Previously the parameters were unannotated, so checkers inferred each one from its default value (for example max_iter was treated as str and rejected integer values).

Each annotation matches the documented parameter types in the class docstring:

  • n_components: int | float | None
  • noise_cov: Covariance | None
  • rng: int | Generator | RandomState | None
  • random_state: int | RandomState | None
  • method: Literal["fastica", "infomax", "jamica", "picard"]
  • fit_params: dict | None
  • max_iter: int | Literal["auto"]
  • allow_ref_meg: bool
  • verbose: bool | str | int | None

The internal-only method="imported_eeglab" used by read_ica_eeglab stays out of the public annotation (it remains undocumented) and the call site carries a ty: ignore comment.

Additional information

Verified locally with a fresh venv on this branch: ruff check and ruff format --check pass, codespell/rstcheck pass on the changelog fragment, pytest mne/tests/test_docstring_parameters.py passes (18 tests including test_type_hints_match_docstrings), pytest mne/preprocessing/tests/test_ica.py::test_ica_max_iter_ and test_ica_rng_transition pass, a mypy check of a user-side script exercising every parameter reports no issues, and ty reports one fewer diagnostic on the file than on main (no new ones introduced).

AI assistance disclosure: this PR was prepared with AI assistance (Devin, SWE-2 Max). The tool inspected the issue and runtime validation, wrote the annotations and changelog fragment, ran the checks listed above, and drafted this description. I reviewed the diff and take responsibility for its contents.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 16:34
@welcome

welcome Bot commented Nov 22, 2026

Copy link
Copy Markdown

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

This comment was marked as low quality.

@drammock

Copy link
Copy Markdown
Member

thanks for the contribution! As we work our way toward completing type-hints across the whole public API, we're taking the approach of doing one entire submodule at a time so that we can enable the type-checker to run on that submodule in our pre-commit hook (see

mne-python/pyproject.toml

Lines 522 to 530 in 387a0dd

[tool.ty.src]
exclude = ["mne/_fiff/tests", "mne/io/**/tests"]
include = [
"mne/_fiff",
"mne/annotations.py",
"mne/epochs.py",
"mne/evoked.py",
"mne/io",
]
), thereby preventing future changes from breaking the type contracts.

Are you willing to tackle type-hinting the whole mne.preprocessing submodule? (warning: it's quite a bit of work to do well, even with LLM help). If so, check out some of the convenience types in mne.utils._typing (e.g. for verbose), and look at files in mne._fiff for inspiration on how we've handled things so far (e.g., we use Any and # type: ignore quite sparingly, try to be explicit about array shapes/dtypes when possible, etc)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type hints for mne.preprocessing.ICA() are narrower than their runtime behavior and documented usage.

3 participants