Skip to content

Conversation

@siiptuo
Copy link
Contributor

@siiptuo siiptuo commented Nov 13, 2023

Summary

Implements FURB136 that checks for if expressions that can be replaced with min() or max() calls. See issue #1348 for more information.

This implementation diverges from Refurb's original implementation by retaining the order of equal values. For example, Refurb suggest that the following expressions:

highest_score1 = score1 if score1 > score2 else score2
highest_score2 = score1 if score1 >= score2 else score2

should be to rewritten as:

highest_score1 = max(score1, score2)
highest_score2 = max(score1, score2)

whereas this implementation provides more correct alternatives:

highest_score1 = max(score2, score1)
highest_score2 = max(score1, score2)

Test Plan

Unit test checks all eight possibilities.

This implementation diverges from Refurb's original implementation by
retaining the order of equal values. For example, Refurb suggest that
the following expressions:

    highest_score1 = score1 if score1 > score2 else score2
    highest_score2 = score1 if score1 >= score2 else score2

should be to rewritten as:

    highest_score1 = max(score1, score2)
    highest_score2 = max(score1, score2)

whereas this implementation provides more correct alternatives:

    highest_score1 = max(score2, score1)
    highest_score2 = max(score1, score2)
@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Nov 13, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Nov 13, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 41 projects)

apache/airflow (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --select ALL --preview

+ airflow/providers/apache/kafka/operators/consume.py:158:30: FURB136 [*] Replace `if` expression with `min(messages_left, self.max_batch_size)`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
FURB136 1 1 0 0 0

Copy link
Member

@charliermarsh charliermarsh 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, this looks great! I may make some small tweaks to better conform to some of our idioms but shouldn't require any major changes.

flake8_simplify::rules::twisted_arms_in_ifexpr(checker, expr, test, body, orelse);
}
if checker.enabled(Rule::IfExprMinMax) {
refurb::rules::if_expr_min_max(checker, expr, test, body, orelse);
Copy link
Member

Choose a reason for hiding this comment

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

Can you instead pass in the ast::ExprIfExp here? That's what we prefer for new rules. So e.g. on line 1284, you'd change to:

Expr::IfExp(if_exp @ ast::ExprIfExp {
    test,
    body,
    orelse,
    range: _,
}) => {
    ...
}

And then here, you'd pass in if_exp instead of the destructured fields. That way, callers can't pass in the "wrong" values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks!

@charliermarsh charliermarsh enabled auto-merge (squash) November 15, 2023 17:54
@charliermarsh charliermarsh merged commit 0e2ece5 into astral-sh:main Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants