Skip to content

Switch from mypy to basedpyright#698

Open
veegalinova wants to merge 4 commits intomainfrom
vg/feat/based-pyright-switch
Open

Switch from mypy to basedpyright#698
veegalinova wants to merge 4 commits intomainfrom
vg/feat/based-pyright-switch

Conversation

@veegalinova
Copy link
Copy Markdown
Collaborator

@veegalinova veegalinova commented Jan 23, 2026

Description

Note

tldr: Switch from mypy to basedpyright for better type checking

Background - why do we need this PR?

When working on a new feature, I got some super annoying mypy false positive errors that were not informative and did not actually protect the code from any real issues.
Currently, many people have migrated to pyright instead. In general, it has more coverage and strictness than mypy, should be faster, and (in my opinion) captures more important stuff than mypy does - more on differences here.

Overview - what changed?

I replaced mypy with basedpyright. Why basedpyright and not the regural one?
basedpyright is a community fork of pyright that implements some neat features useful to us.
The main one is baseline that helps with gradual migration. Instead of getting hundreds of new type errors, we fix the current state of code as "acceptable". Only new code must pass strict type checks. All unresolved stuff is stored in .basedpyright/baseline.json, which we can resolve gradually.
Other improvements in basedpyright vs standard pyright here.

Implementation - how did you implement the changes?

  1. Added basedpyright to dev dependencies
  2. Created pyrightconfig.json with strictness settings
  3. Generated baseline file with 432 current errors.
  4. Updated pre-commit hooks to use basedpyright instead of mypy

Changes Made

New features or files

  • .basedpyright/baseline.json - snapshot of current unresolved errors
  • pyrightconfig.json - type checking configuration
  • .pre-commit-config.yaml - replaced mypy hook with basedpyright hook

Modified features or files

  • pyproject.toml - added basedpyright to dev dependencies

Removed features or files

  • mypy configuration file and pyproject.toml section

Breaking changes

None for users.

For developers:

  • run pre-commit install to update local hooks
  • for vscode/cursor highlighting - install the basedpyright extension and disable the default pylance/pyright extension
  • all new code must pass basedpyright checks (errors not in baseline will fail pre-commit)

Additional Notes and Examples


Please ensure your PR meets the following requirements:

  • Code builds and passes tests locally, including doctests
  • New tests have been added (for bug fixes/features)
  • Pre-commit passes
  • PR to the documentation exists (for bug fixes / features)

@veegalinova veegalinova force-pushed the vg/feat/based-pyright-switch branch from 6facd76 to 92bfdde Compare January 23, 2026 13:28
@jdeschamps jdeschamps requested review from a team, jdeschamps and melisande-c and removed request for melisande-c January 26, 2026 12:19
@melisande-c
Copy link
Copy Markdown
Member

I like it! I have been getting annoyed with mypy recently on some stuff to do with TypeVar narrowing that pyright can do correctly but mypy can't.

It seems though, when run as a pre-commit on github the .env no longer exists, there might be a way to create the env first or we could skip the hook on the CI and instead have a different CI job for basepyright as documented here: https://docs.basedpyright.com/latest/benefits-over-pyright/improved-ci-integration/

"src/careamics/losses/lvae/losses.py",
"**/*.ipynb"
],
"pythonVersion": "3.12",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we currently support from 3.11 so we should probably use that python version

@jdeschamps jdeschamps added the chore General repository or code care label Mar 2, 2026
jdeschamps added a commit that referenced this pull request Mar 27, 2026
## Description

Following #827, this PR
enables pre-commit hooks for `CAREamistV2` and attempts to solve the
errors.

Note that there is something fishy with the use of `TypeVar` that `mypy`
disagrees with:

```python
src/careamics/careamist_v2.py:128: error: Argument 1 to "_load_model" of "CAREamistV2" has incompatible type "NGConfiguration[CAREAlgorithm] | Path | None"; expected "NGConfiguration[AlgorithmConfig] | Path | None"  [arg-type]
src/careamics/careamist_v2.py:128: error: Argument 1 to "_load_model" of "CAREamistV2" has incompatible type "NGConfiguration[N2NAlgorithm] | Path | None"; expected "NGConfiguration[AlgorithmConfig] | Path | None"  [arg-type]
src/careamics/careamist_v2.py:128: error: Argument 1 to "_load_model" of "CAREamistV2" has incompatible type "NGConfiguration[N2VAlgorithm] | Path | None"; expected "NGConfiguration[AlgorithmConfig] | Path | None"  [arg-type]
Found 3 errors in 1 file (checked 1 source file)
```

One way to solve it to redefine `AlgorithmConfig` in the same module,
rather than importing it. It seems that allows "bounding" the type to
the functions it is used in.

But it would be annoying to have to redefine the generics in every
module. I decided to just ignore it for now for two reasons:
- #817 introduces a validator
that may improve the situation
- Maybe pyright (#698) will
be better suited
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore General repository or code care

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants