Skip to content

nested-min-max (PLW3301)

Derived from the Pylint linter.

Fix is sometimes available.

What it does

Checks for nested min and max calls.

Why is this bad?

Nested min and max calls can be flattened into a single call to improve readability.

Example

minimum = min(1, 2, min(3, 4, 5))
maximum = max(1, 2, max(3, 4, 5))
diff = maximum - minimum

Use instead:

minimum = min(1, 2, 3, 4, 5)
maximum = max(1, 2, 3, 4, 5)
diff = maximum - minimum

Fix safety

This fix is always unsafe and may change the program's behavior for types without full equivalence relations, such as float comparisons involving NaN.

print(min(2.0, min(float("nan"), 1.0)))  # before fix: 2.0
print(min(2.0, float("nan"), 1.0))  # after fix: 1.0

print(max(1.0, max(float("nan"), 2.0)))  # before fix: 1.0
print(max(1.0, float("nan"), 2.0))  # after fix: 2.0

References