-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Possible error when trying to fix with ICN001 #14439
Comments
Interesting... I think this is not quite what ICN001 was designed for. It's not possible to import (and then alias) a function like Were you maybe looking for something like |
I'm also not super sold on whether to count this as a bug, since the user has required Ruff to insert the syntax error. It would be like writing a required import in the required imports linter setting that had a syntax error in it. This is especially difficult to detect in advance because one has to resolve the module you're trying to import in order to tell whether you're attempting to alias a submodule or a function. |
This should count as a bug because something of the form $ echo 'from collections import abc' | ruff check --config 'lint.flake8-import-conventions.aliases = {"collections.abc" = "collections.abc"}' --select ICN001 - --unsafe-fixes --fix --isolated
error: Fix introduced a syntax error. Reverting all changes.
This indicates a bug in Ruff. If you could open an issue at:
https://github.com/astral-sh/ruff/issues/new?title=%5BFix%20error%5D
...quoting the contents of `-`, the rule codes ICN001, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
from collections import abc
-:1:25: ICN001 `collections.abc` should be imported as `collections.abc`
|
1 | from collections import abc
| ^^^ ICN001
|
= help: Alias `collections.abc` to `collections.abc`
Found 1 error.
[*] 1 fixable with the --fix option. Compare I002: if the user configures it to insert a syntax error, Ruff reports it as a user error instead of a generic “This indicates a bug in Ruff” error: $ echo 1 | ruff check --config 'lint.isort.required-imports = ["from math import sin as math.sin"]' --select I002 - --fix --isolated
error: invalid value 'lint.isort.required-imports = ["from math import sin as math.sin"]' for '--config <CONFIG_OPTION>'
tip: A `--config` flag must either be a path to a `.toml` configuration file
or a TOML `<KEY> = <VALUE>` pair overriding a specific configuration
option
Could not parse the supplied argument as a `ruff.toml` configuration option:
Expected ',', found '.' at byte range 28..29
in `lint`
For more information, try '--help'. |
Ah thanks for the investigation! I was mistakenly assuming that we were trying to do We should certainly correct this! As for detecting syntax errors like |
Just a little additional context about our objective: We have internal modules such that there exists a function import a.b.c
import d
a.b.c.moo_func(42)
d.moo_func(42) But there might exist We use the ICN001 rule to enforce this -- and it does seem (perhaps coincidentally!) to do a good job of detecting what we want. It's just that the autofix doesn't work :) |
The good news is that I can resolve the panic by surfacing an error to the user upon loading the configuration. The bad news is that this breaks @tpgillam 's "hack" 😞 . Unfortunately I think I do have to prefer fixing the panic over preserving the hack, because the latter is truly not how the rule was intended to work. If ICN003 does not help, perhaps you can open another issue describing a lint rule or configuration option that would make sense for your use case and we can consider it there? Apologies. |
No apologies required! Correctness is clearly preferable, I agree. |
I obtain errors with ICN001 when trying to apply autofix (in 'unsafe' mode) that should ideally convert the import from
from A1.A2.AN import B
toimport A1.A2.AN
.For example, consider the following simple source file
moo.py
:And then the following configuration for ruff in
pyproject.toml
I get the following error:
I'm not 100% sure that I'm not abusing the intention of this rule, since here I'm trying to use the rule to enforce the absence of an alias, rather than a conventional alias :)
The text was updated successfully, but these errors were encountered: