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

Rule F401 causes autofix error #6758

Closed
qarmin opened this issue Aug 22, 2023 · 1 comment · Fixed by #6774
Closed

Rule F401 causes autofix error #6758

qarmin opened this issue Aug 22, 2023 · 1 comment · Fixed by #6774
Assignees
Labels
bug Something isn't working fuzzer Surfaced via fuzzing.

Comments

@qarmin
Copy link

qarmin commented Aug 22, 2023

Ruff 0.0.285

ruff  *.py --select ALL --no-cache

file content:

if typing.TYPE_CHECKING:
    import logging

    from tenacity import RetryCallStat

error:

error: Autofix 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=%5BAutofix%20error%5D

...quoting the contents of `PY_FILE_TEST_7845308560.py`, the rule codes F401, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

PY_FILE_TEST_7845308560.py.zip

@charliermarsh charliermarsh self-assigned this Aug 22, 2023
@charliermarsh charliermarsh added bug Something isn't working fuzzer Surfaced via fuzzing. labels Aug 22, 2023
@charliermarsh
Copy link
Member

This is a regression from #6694 so not in live Ruff. Will fix today.

charliermarsh added a commit that referenced this issue Aug 22, 2023
## Summary

The isolation group for unused imports was relying on
`checker.semantic().current_statement()`, which isn't valid for that
rule, since it runs over the _scope_, not the statement. Instead, we
need to lookup the isolation group based on the `NodeId` of the
statement.

Our tests didn't catch this, because we mostly have cases that look like
this:

```python
if TYPE_CHECKING:
    import shelve
    import importlib
```

In this case, the two fixes to remove the two unused imports are
considered overlapping (since we delete the _full_ line, and the two
_full_ lines touch, and we consider exactly-adjacent fixes to be
overlapping), and so they don't run in a single pass due to the
non-overlapping-fixes requirement. That is: the isolation groups aren't
required for this case. They are, however, required for cases like:

```python
if TYPE_CHECKING:
    import shelve

    import importlib
```

...where the fixes don't overlap.

Closes #6758.

## Test Plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fuzzer Surfaced via fuzzing.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants