astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
28.78k stars 932 forks source link

`TCH` rules sometimes don't autofix #9131

Closed tylerlaprade closed 6 months ago

tylerlaprade commented 6 months ago

I haven't figured out the pattern yet, but there are some edge cases where the CLI says the fixes are autofixable, but doesn't do so even with the --fix flag and the appropriate config:

image image
charliermarsh commented 6 months ago

If you're able to share even a single file or snippet that exhibits this behavior, I'd love to investigate!

tylerlaprade commented 6 months ago

@charliermarsh, I'll DM you on Discord

tylerlaprade commented 6 months ago

Sorry, I didn't notice this stack trace earlier:

error: Panicked while linting abacus/permissions/default.py: This indicates a bug in Ruff. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BLinter%20panic%5D

...with the relevant file contents, the `pyproject.toml` settings, and the following stack trace, we'd be very appreciative!

panicked at /Users/runner/work/ruff/ruff/crates/ruff_text_size/src/range.rs:48:9:
assertion failed: start.raw <= end.raw
Backtrace:    0: _rust_eh_personality
   1: _main
   2: _rust_eh_personality
   3: _rust_eh_personality
   4: _rust_eh_personality
   5: _rust_eh_personality
   6: __rjem_je_witnesses_cleanup
   7: __rjem_je_witnesses_cleanup
   8: _main
   9: _main
  10: _main
  11: _main
  12: _main
  13: _main
  14: _main
  15: _main
  16: _main
  17: _main
  18: __rjem_je_witnesses_cleanup
  19: _main
  20: _main
  21: _main
  22: __rjem_je_witnesses_cleanup
  23: _main
  24: _main
  25: _rust_eh_personality
  26: __pthread_joiner_wake
tylerlaprade commented 6 months ago

Here's one of the errors causing the above stack trace:

from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
...
def foo(
    self, user: AbstractBaseUser | AnonymousUser, view: 'type[CondorBaseViewSet]'
)

When I run ruff check, I simply see 3 errors and a message saying all 3 are fixable. When I run ruff check --fix, I get the stack trace and no errors.

tylerlaprade commented 6 months ago

Manually quoting 'AbstractBaseUser | AnonymousUser' and then saving the file (to trigger autofix) resolved all errors correctly.

charliermarsh commented 6 months ago

Thanks! I'm gonna file a few issues here based on the info you provided.

charliermarsh commented 6 months ago

Filing as: