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

[Linter panic] when fixing TCH003 violation for typing import #5331

Closed
bendoerry opened this issue Jun 23, 2023 · 2 comments · Fixed by #9214
Closed

[Linter panic] when fixing TCH003 violation for typing import #5331

bendoerry opened this issue Jun 23, 2023 · 2 comments · Fixed by #9214
Assignees
Labels
bug Something isn't working

Comments

@bendoerry
Copy link
Contributor

Ruff settings and version

Version: 0.0.275

Settings

# ruff.toml
select = ['TCH']

[flake8-type-checking]
exempt-modules = []

Description

When I try to autofix a TCH003 violation for an import from typing, I get the following stack trace:

panicked at 'assertion failed: start.raw <= end.raw', /root/.cargo/git/checkouts/rustpython-parser-c806f105bc27c7db/f60e204/ruff_text_size/src/range.rs:48:9
Backtrace:    0: <unknown>
   1: <unknown>
   2: <unknown>
   3: <unknown>
   4: <unknown>
   5: <unknown>
   6: <unknown>
   7: <unknown>
   8: <unknown>
   9: <unknown>
  10: <unknown>
  11: <unknown>
  12: <unknown>
  13: <unknown>
  14: <unknown>
  15: <unknown>
  16: __libc_start_call_main
             at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
  17: __libc_start_main_impl
             at ./csu/../csu/libc-start.c:360:3
  18: <unknown>

I have confirmed this only occurs for imports from typing.

Files

I used two files for testing, one using an import from typing and one from collections.abc:

non_typing.py

# non_typing.py
from __future__ import annotations

from collections.abc import Iterable

def foo(a: Iterable[int]) -> None:
    pass

Running ruff check non_typing.py --fix works as expected

typing.py

# typing.py
from __future__ import annotations

from typing import Iterable

def foo(a: Iterable[int]) -> None:
    pass

However if I run ruff check typing.py --fix I get the stacktrace above.
This also occurs for typing imports that aren't part of collections.abc as well. e.g. Any.

@charliermarsh
Copy link
Member

Thank you!

@charliermarsh charliermarsh self-assigned this Jun 23, 2023
@charliermarsh charliermarsh added the bug Something isn't working label Jun 23, 2023
@charliermarsh
Copy link
Member

This might need special-casing. The problem is that we're trying to modify the from typing import to add TYPE_CHECKING, but we're also trying to remove the Iterable member from it.

charliermarsh added a commit that referenced this issue Dec 20, 2023
## Summary

If you remove `typing` from `exempt-modules`, we tend to panic, since we
try to add `TYPE_CHECKING` to `from typing import ...` statements while
concurrently attempting to remove other members from that import. This
PR adds special-casing for typing imports to avoid such panics.

Closes #5331
Closes #9196.
Closes #9197.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants