10000 Avoid reassignment in interface functions by ValerianRey · Pull Request #368 · TorchJD/torchjd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Avoid reassignment in interface functions #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged

Conversation

ValerianRey
Copy link
Contributor

In backward and mtl_backward, we're defining an interface for the users. This means that we accept many different types for the parameters, and we then change their type to use our internal machinery with the required types. This is not something that mypy likes, because it leads to assignment errors. We could use the --allow-redefinition flag of mypy, but I think they're kind of right in not allowing redefinitions by default. With redefinitions, it's harder to read the code (since variable types change throughout the same function) and it makes refactors harder (because you may want to rename just the variable after it changed type for instance). We could also solve it using cast, but it's more or less the same as allowing the redefinition but just locally: it tells mypy "trust me, the type is [...]". So I think the cleanest solution is to use different variables after type has changed. Naming them can be challenging, because we don't like to have type included in the name, but at the same time, in these interfaces, we have two types for the same thing. My solution is to have a new convention to name xyz the original variable and xyz_ the one that has been changed to the right format / type.

Copy link
codecov bot commented May 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Files with missing lines Coverage Δ
src/torchjd/_autojac/_backward.py 100.00% <100.00%> (ø)
src/torchjd/_autojac/_mtl_backward.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ValerianRey ValerianRey changed the title Create new variables when changing their type Avoid reassignment May 29, 2025
@ValerianRey ValerianRey changed the title Avoid reassignment Avoid reassignment in interface functions May 29, 2025
@ValerianRey ValerianRey mentioned this pull request May 29, 2025
1 task
@ValerianRey ValerianRey requested a review from PierreQuinton May 29, 2025 19:19
@ValerianRey ValerianRey merged commit 0174765 into main Jun 2, 2025
16 checks passed
@ValerianRey ValerianRey deleted the use-new-variables branch June 2, 2025 10:52
@ValerianRey ValerianRey mentioned this pull request Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0