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] Fixing TCH003 fails when no TYPE_CHECKING block exists and exempt-modules is empty #9196

Closed
picnixz opened this issue Dec 19, 2023 · 1 comment · Fixed by #9214
Assignees
Labels
bug Something isn't working linter Related to the linter

Comments

@picnixz
Copy link

picnixz commented Dec 19, 2023

I'm not sure that it's the place to ask since this bug appears to be caused by [tool.ruff.lint.flake8-type-checking]. Anyway, with the configuration

[tool.ruff]
target-version = 'py311'
ignore = ['all']
select = ['TCH']

[tool.ruff.lint.flake8-type-checking]
exempt-modules = []

invoking

python3.11 -m ruff file.py --fix --unsafe-fixes

on

from __future__ import annotations

from typing import Final
from collections.abc import Mapping

Const: Final[Mapping] = {}

fails with

panicked at /home/runner/work/ruff/ruff/crates/ruff_text_size/src/range.rs:48:9:
assertion failed: start.raw <= end.raw
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_main
             at /usr/src/debug/glibc-2.26-lp152.26.12.1.x86_64/csu/../csu/libc-start.c:308
  17: <unknown>

But the following succeeds:

from __future__ import annotations

from typing import TYPE_CHECKING, Final
from collections.abc import Mapping

if TYPE_CHECKING:
    from collections.abc import Sequence

Const: Final[Mapping[str, Sequence]] = {}

and gets reformatted as

from __future__ import annotations

from typing import TYPE_CHECKING, Final

if TYPE_CHECKING:
    from collections.abc import Mapping
    from collections.abc import Sequence

Const: Final[Mapping[str, Sequence]] = {}

I think it's because the TYPE_CHECKING block does not exist yet that it's like that. However, if the block is empty and only typing imports exist, it also fails:

from __future__ import annotations

from typing import TYPE_CHECKING, Final

if TYPE_CHECKING:
    pass

Const: Final[float] = 1

Now, if I change

[tool.ruff.lint.flake8-type-checking]
exempt-modules = []

to

[tool.ruff.lint.flake8-type-checking]
exempt-modules = ['typing']

it works as intended.

Environment

Platform:              linux; (Linux-5.3.18-lp152.106-default-x86_64-with-glibc2.26)
Python version:        3.11.6 (main, Nov 29 2023, 14:46:32) [GCC 7.5.0])
Python implementation: CPython
ruff version:          0.1.8
@picnixz picnixz changed the title [Linter panic] Fixing TCH003 when no TYPE_CHECKING block exists fails and empty exempt-modules [Linter panic] Fixing TCH003 fails when no TYPE_CHECKING block exists and exempt-modules is empty Dec 19, 2023
@picnixz
Copy link
Author

picnixz commented Dec 19, 2023

I think it is probably related to #5331 (feel free to close it if you think it's a duplicate)

@MichaReiser MichaReiser added bug Something isn't working linter Related to the linter labels Dec 19, 2023
@charliermarsh charliermarsh self-assigned this Dec 19, 2023
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 linter Related to the linter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants