Skip to content

[pyflakes] Fix infinite loop with unused local import in __init__.py (F401)#15517

Merged
ntBre merged 11 commits intomainfrom
brent/f401-loop
Jan 16, 2025
Merged

[pyflakes] Fix infinite loop with unused local import in __init__.py (F401)#15517
ntBre merged 11 commits intomainfrom
brent/f401-loop

Conversation

@ntBre
Copy link
Copy Markdown
Contributor

@ntBre ntBre commented Jan 15, 2025

Summary

This fixes the infinite loop reported in #12897, where an unused-import that is undefined at the scope of __all__ is "fixed" by adding it to __all__ repeatedly. These changes make it so that only imports in the global scope will be suggested to add to __all__ and the unused local import is simply removed.

Test Plan

Added a CLI integration test that sets up the same module structure as the original report

Closes #12897

@ntBre ntBre added the bug Something isn't working label Jan 15, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 15, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I'd prefer if we can move the test closer to the rule rather than making it a CLI tests. We only use CLI tests for features that can't be tested otherwise but we prefer unit tests or tool specific integration tests for everything else because they're faster to run, easier to find, and there's less code "between" the test and the implementation if something goes wrong.

I'm requesting review from @AlexWaygood to review the rule behavior change.

Comment thread crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs Outdated
Comment thread crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs Outdated
@AlexWaygood
Copy link
Copy Markdown
Member

AlexWaygood commented Jan 16, 2025

Nice diagnosis! The change to the rule's semantics look correct to me.

It's nice that this is a targeted fix as well. I think I tried to fix this a while back, but made the mistake of trying to untangle some of the more spaghetti-code-like parts of this rule at the same time, and got bogged down in a tricky refactor that was hard to pull off.

I agree that I think it should be possible to test this fix using fixtures rather than a CLI integration test. We already have several mock packages set up here that we use for testing F401; can we create another mock package for this scenario? https://github.com/astral-sh/ruff/tree/main/crates/ruff_linter/resources/test/fixtures/pyflakes. The actual tests that use the fixtures are here: https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/rules/pyflakes/mod.rs

@AlexWaygood AlexWaygood removed their request for review January 16, 2025 12:06
@ntBre
Copy link
Copy Markdown
Contributor Author

ntBre commented Jan 16, 2025

Thank you both for the suggestions! For some reason I immediately assumed that multiple files meant I needed an integration test, but these examples were very helpful. I first made the unit test changes on main to make sure they triggered the original error before applying them here. And using the ScopeId is much nicer, thank you!

Copy link
Copy Markdown
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Very nice!

Comment thread crates/ruff_linter/src/rules/pyflakes/mod.rs
@AlexWaygood AlexWaygood added preview Related to preview mode features fixes Related to suggested fixes for violations labels Jan 16, 2025
ntBre and others added 3 commits January 16, 2025 07:57
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@ntBre ntBre merged commit ca3b210 into main Jan 16, 2025
@ntBre ntBre deleted the brent/f401-loop branch January 16, 2025 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Infinite loop] Package-relative import issue with module __all__ when using F401 and F822 with the preview option

3 participants