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

Import aliases configured for ICN001 can cause syntax errors #14662

Open
dscorbett opened this issue Nov 28, 2024 · 1 comment
Open

Import aliases configured for ICN001 can cause syntax errors #14662

dscorbett opened this issue Nov 28, 2024 · 1 comment
Assignees
Labels
bug Something isn't working configuration Related to settings and configuration

Comments

@dscorbett
Copy link

Ruff 0.8.0 does not validate the aliases in lint.flake8-import-conventions.extend-aliases for unconventional-import-alias (ICN001).

$ cat icn001.py
import sys

$ ruff check --select ICN001 --config 'lint.flake8-import-conventions.extend-aliases = {"sys" = "def"}' --isolated --unsafe-fixes --fix icn001.py

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 `icn001.py`, the rule codes ICN001, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

icn001.py:1:8: ICN001 `sys` should be imported as `def`
  |
1 | import sys
  |        ^^^ ICN001
  |
  = help: Alias `sys` to `def`

Found 1 error.
[*] 1 fixable with the --fix option.

The extra aliases should be validated like the ones in lint.flake8-import-conventions.aliases (#14477).

$ ruff check --select ICN001 --config 'lint.flake8-import-conventions.aliases = {"sys" = "def"}' --isolated --unsafe-fixes --fix icn001.py
error: invalid value 'lint.flake8-import-conventions.aliases = {"sys" = "def"}' 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:

invalid value: string "def", expected a Python identifier
in `lint`

For more information, try '--help'.

There is still one problem with lint.flake8-import-conventions.aliases, and therefore also extend-aliases: they allow __debug__ as an alias, but that is a syntax error.

$ ruff check --select ICN001 --config 'lint.flake8-import-conventions.aliases = {"sys" = "__debug__"}' --isolated --unsafe-fixes --fix icn001.py
Found 1 error (1 fixed, 0 remaining).

$ cat icn001.py
import sys as __debug__

$ python icn001.py
  File "icn001.py", line 1
    import sys as __debug__
    ^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: cannot assign to __debug__
@AlexWaygood
Copy link
Member

Cc. @dylwil3, if you're interested in this one!

@AlexWaygood AlexWaygood added bug Something isn't working configuration Related to settings and configuration labels Nov 28, 2024
@dylwil3 dylwil3 self-assigned this Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working configuration Related to settings and configuration
Projects
None yet
Development

No branches or pull requests

3 participants