Skip to content
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

No way to autofix F401 errors in __init__.py #16609

Closed
ezyang opened this issue Mar 10, 2025 · 3 comments
Closed

No way to autofix F401 errors in __init__.py #16609

ezyang opened this issue Mar 10, 2025 · 3 comments
Labels
question Asking for support or clarification

Comments

@ezyang
Copy link

ezyang commented Mar 10, 2025

Summary

Repro:

init.py

from a import foo

def baz() -> None:
  pass

a.py

def foo():
  pass
ruff check --fix --unsafe-fixes --select ALL

The unused import is not removed. Now, I assume this is special cased because __init__.py is special. It would be nice if there a way to bypass the special case anyway though. For now, though, I think I'll get my code out of __init__.py...

As a stopgap, it would be nice if it said something about why the import was not fixed here, this was very difficult to Google for why it was not fixing it since everyone claimed unused imports would be removed by this.

Version

ruff 0.9.10

@ntBre
Copy link
Contributor

ntBre commented Mar 10, 2025

This fix is only available with preview mode enabled:

> cat __init__.py
from a import foo


def baz() -> None:
  pass
> ruff check __init__.py --diff --select F401 --unsafe-fixes --preview
--- __init__.py
+++ __init__.py
@@ -1,4 +1,3 @@
-from a import foo


 def baz() -> None:

Would fix 1 error.

This seems to work for me, even for __init__.py, which was a pleasant surprise since there are several existing issues around F401 for __init__.py files.

@MichaReiser MichaReiser added the question Asking for support or clarification label Mar 11, 2025
@vikyd
Copy link

vikyd commented Mar 12, 2025

https://docs.astral.sh/ruff/tutorial/

--preview --unsafe-fixes are not mentioned in the tutorial page.

@MichaReiser
Copy link
Member

docs.astral.sh/ruff/tutorial

--preview --unsafe-fixes are not mentioned in the tutorial page.

This is intentional. The tutorial should cover the bascis. Both --preview and --unsafe-fixes are more advanced concepts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

5 participants