Nested conditionals are hard to read and can make the order of operations complex to understand.
class Job:
@property
def readable_status(self):
return "Running" if job.is_running else "Failed" if job.errors else "Succeeded" # Noncompliant
Instead, use another line to express the nested operation in a separate statement.
class Job:
@property
def readable_status(self):
if job.is_running:
return "Running"
return "Failed" if job.errors else "Succeeded"
Exceptions
No issue is raised on conditional expressions in comprehensions.
job_statuses = ["Running" if job.is_running else "Failed" if job.errors else "Succeeded" for job in jobs] # Compliant by exception