Skip to content

Improve type checks - #14036

Merged
larsoner merged 27 commits into
mne-tools:mainfrom
larsoner:types
Jul 14, 2026
Merged

Improve type checks#14036
larsoner merged 27 commits into
mne-tools:mainfrom
larsoner:types

Conversation

@larsoner

@larsoner larsoner commented Jul 8, 2026

Copy link
Copy Markdown
Member
  1. Move from mypy to ty, including as pre-commit as ty is fast enough 🚀
  2. Add a consistency check for documented types (NumpyDoc) and our types (this check could eventually move to docstub but let's use it here for a bit)
  3. Stricter enforcement (mypy missed some stuff) and checking in epochs.py, evoked.py, and mne/io
  4. Correctly type the io.read_raw_*, but keep their docs/ / website aliases pointing to publicly documented objects
  5. Most typing changes were of the form 1) dict[str, Any], 2) assert x is not None for when x is self._data etc. (places where we know it is by code structure), plus using __getattr__ and __setattr__ to get *Bunch classes to play nicely
  6. Some simplification of ctf/res4.py because type checkers have trouble with NumPy structured arrays
  7. Various minor fixes caught by the tests + checker, like instance of RawEDF where we should have had instance of RawBDF
  8. Switch to prek in CIs instead of pre-commit (pre-commit fails because our pre-commit env is too large now)

Potential other things:

  • Could switch contrib guide from pre-commit to prek if we wanted
  • Could bumpy Python version to 3.11 (3.10 EOL in Oct; 3.11 will be SPEC0-dropped soon actually!), it would simplify a little bit of stuff (no need for typing_extensions)

A follow-up PR can actually fully type hint the public functions + classes, but this is a (I'd argue necessary) prerequisite!

Used Claude Opus 4.8 to help me with the drudgery of assert x is not None etc., but I also made a lot of changes myself, and understand + vouch for all code.

Makes progress toward #12243

larsoner added 3 commits July 8, 2026 17:16
* upstream/main:
  Fix transition bandwidth reported in 'filter too short' error (mne-tools#11406) (mne-tools#14005)
@larsoner

larsoner commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

Okay too many packages needed for type checking made pre-commit.ci unhappy, so here I move to prek + prek-action in autofix.yml instead. pre-commit.ci will be red here, but once we're happy with this PR we can merge it and disable pre-commit.ci and use prek. (prek uses the preexisting .pre-commit-config.yml and is a nice, faster version of it.) We could also consider other toolsk, but prek is the least invasive to existing dev workflows (people can continue to equvialently continue using pre-commit locally if they want!)

@larsoner

larsoner commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

A tiny bit of stuff here would get simpler if we dropped 3.10 support. 3.11 is actually almost out of SPEC0 so I don't mind dropping 3.10 now. But not too painful to keep it. It's EOL in October.

@Aniketsy

Copy link
Copy Markdown
Contributor

nice to see these changes on type check improvement, @larsoner thanks for the work ❤️ , I tried to go through the pr to help with review but it includes many changes so i quit 😁 (leaving for members).

A follow-up PR can actually fully type hint the public functions + classes, but this is a (I'd argue necessary) prerequisite!

happy to help if needed, please feel free to ping 😊

* upstream/main:
  Make scrollbar handlers draggable (mne-tools#14040)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#14052)
  Warn when Epochs events fall outside the raw data range (mne-tools#12989) (mne-tools#14004)
  Ensure epochs being concatenated have compatible event ids (mne-tools#14051)
  Widen main content area (mne-tools#14015)
  MAINT: remove dead gain/bits/value_range fields from _read_header in … (mne-tools#14047)
  ENH: replace `_get_blocks` binary reader with mffpy Reader API (mne-tools#14043)
  MAINT: Update dependency specifiers (mne-tools#14048)
  [dependabot]: Bump the actions group with 2 updates (mne-tools#14049)
  Simplify doc building with more refleak (mne-tools#14045)
  ENH: add overlay Brain GUI (mne-tools#14031)
  ENH: support multiple simultaneous overlays in Brain.add_data (mne-tools#13995)
  Add option to show a zero line in browser (mne-tools#14018)
  FIX: pass cmap name string not tuple to interactive topomap slider kwargs (mne-tools#14039)
  Doc/add ai policy pointer (mne-tools#14037)
  Allow subclasses of FigureClass to be passed to plot_raw/plot_epochs (mne-tools#13979)
  MAINT: Replace manual PNS binary block reader in `_read_segment_file` with `mffpy` (mne-tools#14030)
@larsoner

Copy link
Copy Markdown
Member Author

Okay this approach is working in several projects (mne-kit-gui, sphinx-gallery, etc.) and seems to be working here, so I'll go ahead and merge so that we don't get more and more conflicts!

@larsoner
larsoner enabled auto-merge (squash) July 14, 2026 15:32
@larsoner
larsoner merged commit 6bd165d into mne-tools:main Jul 14, 2026
29 checks passed
@larsoner
larsoner deleted the types branch July 14, 2026 16:34
larsoner added a commit to larsoner/mne-python that referenced this pull request Jul 14, 2026
Resolve conflicts from the landed ty type-checker PR (mne-tools#14036) and
intervening feature PRs:

- CI (autofix.yml/tests.yml/azure-pipelines.yml): take upstream's canonical
  restructure (vulture moved into the autofix job, mypy->ty, style job removed);
  our preliminary CI edits were superseded.
- Type-checker infra (_typing.py, test_docstring_parameters.py): keep our richer
  doc/api-scoped consistency checker (superset comparison, Color/FileLike
  pseudo-type aliases, Literal handling) which subsumes upstream's simpler one;
  add a `class`->`type` pseudo-type normalization for the new `figure_class`.
- New upstream features merged with our annotations: `on_outside` on
  BaseEpochs/Epochs/EpochsArray, `show_zero_line`/`figure_class` on
  Raw.plot/Epochs.plot (annotated to match their numpydoc types).
- egi: drop the now-dead `_block_r`/`_get_blocks` helpers and take upstream's
  `_get_mff_reader`-based PNS reader; keep our `_get_signalfname` assert fix.

ty pre-commit hook, ruff, and test_type_hints_match_docstrings all pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
larsoner added a commit to sharifhsn/mne-python that referenced this pull request Jul 14, 2026
* upstream/main: (206 commits)
  Improve type checks (mne-tools#14036)
  Make scrollbar handlers draggable (mne-tools#14040)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#14052)
  Warn when Epochs events fall outside the raw data range (mne-tools#12989) (mne-tools#14004)
  Ensure epochs being concatenated have compatible event ids (mne-tools#14051)
  Widen main content area (mne-tools#14015)
  MAINT: remove dead gain/bits/value_range fields from _read_header in … (mne-tools#14047)
  ENH: replace `_get_blocks` binary reader with mffpy Reader API (mne-tools#14043)
  MAINT: Update dependency specifiers (mne-tools#14048)
  [dependabot]: Bump the actions group with 2 updates (mne-tools#14049)
  Simplify doc building with more refleak (mne-tools#14045)
  ENH: add overlay Brain GUI (mne-tools#14031)
  ENH: support multiple simultaneous overlays in Brain.add_data (mne-tools#13995)
  Add option to show a zero line in browser (mne-tools#14018)
  FIX: pass cmap name string not tuple to interactive topomap slider kwargs (mne-tools#14039)
  Doc/add ai policy pointer (mne-tools#14037)
  Allow subclasses of FigureClass to be passed to plot_raw/plot_epochs (mne-tools#13979)
  MAINT: Replace manual PNS binary block reader in `_read_segment_file` with `mffpy` (mne-tools#14030)
  Fix transition bandwidth reported in 'filter too short' error (mne-tools#11406) (mne-tools#14005)
  ENH: Show the current time as a vertical line in plot_evoked_topo (mne-tools#14032)
  ...
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.

2 participants