Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pylint] Fix PL1730: min/max auto-fix and suggestion #15930

Merged

Conversation

VascoSch92
Copy link
Contributor

Summary

The PR addresses the issue #15887

For two objects a and b, we ensure that the auto-fix and the suggestion is of the form a = min(a, b) (or a = max(a, b)). This is because we want to be consistent with the python implementation of the methods: min and max. See the above issue for more details.

Copy link
Contributor

github-actions bot commented Feb 4, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Feb 4, 2025
@MichaReiser MichaReiser linked an issue Feb 4, 2025 that may be closed by this pull request
@VascoSch92
Copy link
Contributor Author

@MichaReiser I implemented your suggestion ;-) Let me know if you like it...

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think we can simplify this further.

Comment on lines 129 to 151
let (min_max, arg1, arg2) = match (
left_is_target,
right_is_target,
left_is_value,
right_is_value,
) {
(true, false, false, true) => match op {
CmpOp::Lt => (MinMax::Max, true),
CmpOp::LtE => (MinMax::Max, false),
CmpOp::Gt => (MinMax::Min, true),
CmpOp::GtE => (MinMax::Min, false),
CmpOp::Lt => (MinMax::Max, left.as_ref(), right),
CmpOp::LtE => (MinMax::Max, left.as_ref(), right),
CmpOp::Gt => (MinMax::Min, left.as_ref(), right),
CmpOp::GtE => (MinMax::Min, left.as_ref(), right),
_ => return,
},
(false, true, true, false) => match op {
CmpOp::Lt => (MinMax::Min, true),
CmpOp::LtE => (MinMax::Min, false),
CmpOp::Gt => (MinMax::Max, true),
CmpOp::GtE => (MinMax::Max, false),
CmpOp::Lt => (MinMax::Min, right, left.as_ref()),
CmpOp::LtE => (MinMax::Min, right, left.as_ref()),
CmpOp::Gt => (MinMax::Max, right, left.as_ref()),
CmpOp::GtE => (MinMax::Max, right, left.as_ref()),
_ => return,
},
_ => return,
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this to (and also reduce the diff size):

    let min_max = match (
        left_is_target,
        right_is_target,
        left_is_value,
        right_is_value,
    ) {
        (true, false, false, true) => match op {
            CmpOp::Lt | CmpOp::LtE => MinMax::Max,
            CmpOp::Gt | CmpOp::GtE => MinMax::Min,
            _ => return,
        },
        (false, true, true, false) => match op {
            CmpOp::Lt | CmpOp::LtE => MinMax::Min,
            CmpOp::Gt | CmpOp::GtE => MinMax::Max,
            _ => return,
        },
        _ => return,
    };

		// Determine whether to use `min()` or `max()`, and make sure that the first
    // arg of the `min()` or `max()` method is equal to the target of the comparison.
    // This is to be consistent with the Python implementation of the methods `min()` and `max()`.
    let (arg1, arg2) = if left_is_target {
        (&**left, right)
    } else {
        (right, &**left)
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... I did the changes ;-)

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you

@MichaReiser MichaReiser added the bug Something isn't working label Feb 5, 2025
@MichaReiser MichaReiser enabled auto-merge (squash) February 5, 2025 09:27
@MichaReiser MichaReiser merged commit 827a076 into astral-sh:main Feb 5, 2025
21 checks passed
@VascoSch92 VascoSch92 deleted the fix-PLR1730-autofix-and-suggestion branch February 5, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PLR1730 autofixes max/mins in the wrong order
2 participants