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
Use instead:
Known issues
The resulting code may be slower and use more memory, especially for nested iterables. For example, this code:
will be fixed to:
At least on current versions of CPython, this allocates a collection for the whole iterable
before calling min
and could cause performance regressions, at least for large iterables.
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
The fix will also remove any comments within the outer call.