Skip to content

repeated-isinstance-calls (PLR1701)

Derived from the Pylint linter.

Warning: This rule has been removed and its documentation is only available for historical reasons.

Fix is always available.

Removed

This rule is identical to SIM101 which should be used instead.

What it does

Checks for repeated isinstance calls on the same object.

Why is this bad?

Repeated isinstance calls on the same object can be merged into a single call.

Fix safety

This rule's fix is marked as unsafe on Python 3.10 and later, as combining multiple isinstance calls with a binary operator (|) will fail at runtime if any of the operands are themselves tuples.

For example, given TYPES = (dict, list), then isinstance(None, TYPES | set | float) will raise a TypeError at runtime, while isinstance(None, set | float) will not.

Example

def is_number(x):
    return isinstance(x, int) or isinstance(x, float) or isinstance(x, complex)

Use instead:

def is_number(x):
    return isinstance(x, (int, float, complex))

Or, for Python 3.10 and later:

def is_number(x):
    return isinstance(x, int | float | complex)

Options

References