Skip to content

typing: Replace mypy with ty#551

Merged
ValerianRey merged 29 commits intomainfrom
replace-mypy-with-ty
Feb 3, 2026
Merged

typing: Replace mypy with ty#551
ValerianRey merged 29 commits intomainfrom
replace-mypy-with-ty

Conversation

@PierreQuinton
Copy link
Copy Markdown
Contributor

@PierreQuinton PierreQuinton commented Feb 2, 2026

Typing changes (necessary to run ty without error):

  • Use positional-only arguments for methods from classes whose subclasses rename the parameters. This includes Weighting.forward, JacobianComputer._compute_jacobian, Transform.__call__ and Differentiate.__differentiate__. This fixes a break of LSP.
  • Remove now-useless typing ignore statement in MGDA
  • Add necessary casts in NashMTL
  • Add ignore statement for subclasses of autograd.Functions
  • Fix name of parameters of methods in OrderedSet
  • Make ModuleFactory generic
  • Add some missing casts to PSDMatrix
  • Add ignore statement when calling .grad of BatchedTensor
  • Ignore type errors in the lightning example's test

Structural changes:

  • Add ty check dependency
  • Remove mypy check dependency
  • Change section about type checking in CONTRIBUTING.md
  • Remove mypy badge
  • Add ty tool section in pyproject.toml
  • Change CI to run ty instead of mypy
  • Check typing in tests too

Need to add it in the project.toml and in the ci. Apparetnly ty is part of ruff, we may want to switch the linter too. (the astral product are fast, I'm still a bit scared of getting lockin their product though).

Fixes #550

… more happy about typing. The problem was that the name of the parameter in the class `Weighting` was `stat`, in subclasses, it was `tensor`, `matrix` or `gramian`, which don't match (it would make giving named parameters incorrect in terms of liskov).
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/torchjd/aggregation/_aligned_mtl.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_cagrad.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_constant.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_dualproj.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_imtl_g.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_krum.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_mean.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_mgda.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_nash_mtl.py 100.00% <100.00%> (ø)
src/torchjd/aggregation/_pcgrad.py 100.00% <100.00%> (ø)
... and 16 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ValerianRey
Copy link
Copy Markdown
Contributor

ValerianRey commented Feb 2, 2026

FYI making some param positional-only in a public interface is a breaking change. But let's go for it. I think I really like this way of fixing the name change in override problem.
Just need a changelog entry explaining how to transition for people using e.g. weighting(gramian=gramian)

@PierreQuinton
Copy link
Copy Markdown
Contributor Author

FYI making some param positional-only in a public interface is a breaking change. But let's go for it. I think I really like this way of fixing the name change in override problem. Just need a changelog entry explaining how to transition for people using e.g. weighting(gramian=gramian)

Yeah, realized that later. I think if we do that, we should consider all public functions and double check what we want them to be exactly in terms of positional vs named. That may go in another PR then.

@ValerianRey ValerianRey added breaking-change This PR introduces a breaking change. cc: ci Conventional commit type for changes to the CI (Github workflows and actions). cc: typing Conventional commit type for improvements to typing. labels Feb 2, 2026
@ValerianRey
Copy link
Copy Markdown
Contributor

Actually not a breaking change because the change is made to forward (which shouldn't be called directly by users and isn't documented) and not to __call__.

@ValerianRey ValerianRey removed the breaking-change This PR introduces a breaking change. label Feb 2, 2026
Similarly to the previous commit, this is not strictly necessary according to LSP, but I think it's weird that subclasses don't enforce pos-only arguments if the parent class enforces that.
- We could maybe re-add a ty badge but it's supposed to be covered by the Checks badge, which includes tests, type checking, and any other check
@ValerianRey
Copy link
Copy Markdown
Contributor

LGTM. This is great actually, and I like all of those changes.

We may still want to configure ty to be more strict, or to fix the type errors in the tests (we can just use the # ignore for some of them if they're boring to fix, in particular if we're testing something that's intentionally messing with types to simulate a user's mistake).

@ValerianRey ValerianRey removed the cc: ci Conventional commit type for changes to the CI (Github workflows and actions). label Feb 2, 2026
@ValerianRey ValerianRey changed the title ci: Replace mypy with ty typing: Replace mypy with ty Feb 2, 2026
@ValerianRey
Copy link
Copy Markdown
Contributor

@PierreQuinton I made quite a few changes. In particular, I fixed typing in tests and made the ci also check typing of tests (it was quite easy actually, and it could really protect us from user-facing typing problems, which may be invisible if we don't type-check tests).

@PierreQuinton
Copy link
Copy Markdown
Contributor Author

PierreQuinton commented Feb 3, 2026

I think LGTM, I think we can merge.

@ValerianRey ValerianRey merged commit 65c1165 into main Feb 3, 2026
17 checks passed
@ValerianRey ValerianRey deleted the replace-mypy-with-ty branch February 3, 2026 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cc: typing Conventional commit type for improvements to typing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve type checking

2 participants