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

new rule - ban nested if expressions #13564

Open
DetachHead opened this issue Sep 30, 2024 · 2 comments
Open

new rule - ban nested if expressions #13564

DetachHead opened this issue Sep 30, 2024 · 2 comments
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule

Comments

@DetachHead
Copy link

i keep stumbling across code where, for whatever reason, the person writing it thought it was a good idea to have several nested conditions in an if expression, which looks absolutely awful:

foo = (
    "a"
    if bool()
    else str(
        "b"
        if bool()
        else (
            "c"
            if bool()
            else str(
                "d"
                if bool()
                else "e"
            )
        )
    )
)

it would be nice if ruff had a rule to enforce that if expressions do not have any nested if expressions inside them, and instead suggests writing it as a regular if statement which is far easier to read

@AlexWaygood AlexWaygood added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Sep 30, 2024
@lsouder-ozone3d
Copy link

It's possible you just want some combination of:

@DetachHead
Copy link
Author

hmm, it looks like neither of those rules report on if expressions no matter how nested i make it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants