-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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-alias (UP020) has unexpected auto-fix behaviour #15915
Comments
I think both of these examples boil down to the rule not removing the $ cat <<EOF | ruff check --select UP020,F401 --diff -
import io
io.open("Cargo.toml")
EOF Output @@ -1,2 +1 @@
-import io
-io.open("Cargo.toml")
+open("Cargo.toml")
Would fix 2 errors. But I agree that the first case is not really ideal. Even with F401, $ cat <<EOF | ruff check --select UP020,F401 --diff --no-cache -
from io import open
open("Cargo.toml")
EOF Output @@ -1,2 +1,2 @@
-from io import open
-open("Cargo.toml")
+import builtins
+builtins.open("Cargo.toml")
Would fix 2 errors. |
There is another rule that looks promising for cleaning up the |
Adjusting UP029 seems reasonable to me. I'm a bit surprised that it wouldn't catch
|
Ahhh, with cat <<EOF | ruff check --select UP020,UP029,F401 --diff --no-cache --unsafe-fixes -
from io import open
open("Cargo.toml")
EOF @@ -1,2 +1 @@
-from io import open
open("Cargo.toml")
Would fix 1 error. But without |
Ah nice find. I wonder why the fix is marked as unsafe. |
Not sure either, but it's being tracked for improving that in the docs in #15584 at least. |
Description
Running the UP020 rule with --fix produces weird and unexpected results.
Example 1 (ImportStyle::ImportFrom)
When running
ruff check --fix --select --isolated UP020 /path/to/file.py
on the particular file below:you end up with this result:
Not only is the
from io import open
statement still there, but it also imports builtins which is not needed in this minimal example.Example 2 (ImportStyle::Import)
When running
ruff check --fix --select --isolated UP020 /path/to/file.py
on the particular file below:you end up with the following:
where the
import io
statement is still there despite being unused.ruff --version
:ruff 0.9.4
The text was updated successfully, but these errors were encountered: