Skip to content

Rule: prefer if over ternary operation evaluating to None if cond doesn't hold #6850

@dorschw

Description

@dorschw

Proposal

Disallow ternary operations that:

  • if condition holds, call a method
  • otherwise, do nothing (the whole expression evaluates toNone)

e.g. method(...) if condition else None

Why is it bad?

Can be autofixed, simplified, and be clearer for future maintainers.

Examples & autofix

- print("🐍") if condition else None
+ if condition:
+     print("🐍")
- dictionary.update({"foo":"bar"}) if condition else None
+ if condition:
+     dictionary.update({"foo":"bar"})

(the second example can be simplified to dictionary["foo"]="bar", but the example is what inspired this proposal, and the simplification doesn't serve as good example)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionAwaiting a decision from a maintainerruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions