Skip to content

SIM101 lint and fix is sometimes incorrect #7964

@Zac-HD

Description

@Zac-HD

The SIM101 rule suggests replacing isinstance(a. b) or isinstance(a, c) with isinstance(a, (b, c)) - but the lint and autofix also trigger when there is an intermediate term in the or-expression. This is a problem because code can (and does) rely on short-circuiting evaluation in some such cases. For example, ruff --isolated --select=SIM101 --fix demo.py will break:

x = isinstance("", int) or True or isinstance("", undefined_name)
# x = isinstance("", (int, undefined_name)) or True  # incorrect fix!

I found this while experimenting with ruff in shed, which has a similar fixer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions