Skip to content

Lone unused import in if-statement should not get autofixed, or the if-statement should removed #9472

@jakkdl

Description

@jakkdl

input

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from types import TracebackType

if sys.version_info < (3, 11):
    from exceptiongroup import ExceptionGroup

ruff --fix

from typing import TYPE_CHECKING
import sys

if TYPE_CHECKING:
    pass

if sys.version_info < (3, 11):
    pass

problem

The latter is clearly awful code with fully redundant if statements, and the sys and TYPE_CHECKING imports are in fact unused. There seems to be some custom logic for inserting the pass statement, which I think should be removed and this case should not be treated as an auto-fix - since currently you need to manually fix it anyway (except currently you aren't warned about it, and have to check the diff).
Other solutions include warning and/or autofixing* if [...]: pass statements. If they exist, they should be included by default.

This is running a new, clean, venv with no config. Version 0.1.11

  • probably only autofix if the condition solely contains checks against typing.TYPE_CHECKING, sys.platform, sys.version_info and maybe a few more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions