Skip to content

if-stmt-min-max (PLR1730)

Derived from the Pylint linter.

Fix is sometimes available.

What it does

Checks for if statements that can be replaced with min() or max() calls.

Why is this bad?

An if statement that selects the lesser or greater of two sub-expressions can be replaced with a min() or max() call respectively. Where possible, prefer min() and max(), as they're more concise and readable than the equivalent if statements.

Example

if score > highest_score:
    highest_score = score

Use instead:

highest_score = max(highest_score, score)

Fix safety

This fix is marked unsafe if it would delete any comments within the replacement range.

An example to illustrate where comments are preserved and where they are not:

a, b = 0, 10

if a >= b: # deleted comment
    # deleted comment
    a = b # preserved comment

References