Skip to content

Improve the handling of "iteration dependent" errors and notes in finally clauses. #19270

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Jun 10, 2025

Fixes #19269

This PR refactors the logic implemented in #19118 (which only targeted repeatedly checked loops) and applies it to repeatedly checked finally clauses.

I moved nearly all relevant code to the class LoopErrorWatcher, which now has the more general name IterationErrorWatcher, to avoid code duplication. However, one duplication is left, which concerns error reporting. It would be nice and easy to move this functionality to IterationErrorWatcher, too, but this would result in import cycles, and I am unsure if working with TYPE_CHECKING and postponed importing is acceptable in such cases (both for Mypy and Mypyc).

After the refactoring, it should not be much effort to apply the logic to other cases where code sections are analysed iteratively. However, the only thing that comes to my mind is the repeated checking of functions with arguments that contain constrained type variables. I will check it. If anyone finds a similar case and the solution is as simple as expected, we could add the fix to this PR, of course.

@tyralla
Copy link
Collaborator Author

tyralla commented Jun 10, 2025

Oh, constrained type variables are indeed a problem, and I just realised this was very recently reported in #19256.

@tyralla tyralla changed the title Improve the handling of "iteration dependend" errors and notes in finally clauses. Improve the handling of "iteration dependent" errors and notes in finally clauses. Jun 10, 2025

This comment has been minimized.

@tyralla
Copy link
Collaborator Author

tyralla commented Jun 10, 2025

According to mypy primer, this fixes at least one real-code problem.

@A5rocks and @JukkaL: Perhaps reviewing this would not require too much effort for you, as #19118 is still fresh on our minds?

@tyralla tyralla requested review from JukkaL and A5rocks June 10, 2025 16:41
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

xarray (https://github.com/pydata/xarray)
+ xarray/backends/api.py:1943: error: Unused "type: ignore" comment  [unused-ignore]

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.

Wrong unreachable note in finally section
1 participant