You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd argue a ternary expression is more simple than if/else blocks:
A ternary expression has exactly one assignment, so there's less ambiguity around the effect of the conditional. One variable gets assigned unconditionally or not at all.
It's not possible to interleave other concerns, such as assigning other variables.
It's not possible to have arbitrary side-effects in either logical branch (e.g. sys.path.insert()).
These are the same reasons that I encourage use of the ternary expression, as these constraints also help protect the logical safety of the operation.
Bug Report
mypy can recognize
sys.platform
inif cond: ... / else: ...
conditions but fails to do it if ternary operator (a if cond else b
) is used.To Reproduce
Consider this piece of code, assuming we are on Linux:
Expected Behavior
mypy should not raise any error.
Actual Behavior
mypy fails with:
Environment
The text was updated successfully, but these errors were encountered: