Open jamesbraza opened 1 year ago
from typing import Literal, TypeAlias Sign: TypeAlias = Literal[-1, 0, 1] def sign(x) -> Sign: """ Extract the sign (-1 if negative, 0 if zero, 1 if positive). SEE: https://stackoverflow.com/a/41411231 """ return bool(x > 0) - bool(x < 0)
If x were int/float, then the SIM901 should be thrown, as the bool cast is unnecessary.
x
int
float
SIM901
bool
However, since __lt__/__gt__ can return non-bool if overridden, a bool cast is useful here.
__lt__
__gt__
Conclusion: I think flake8-simplify should examine the type of x for an overridden __lt__/__gt__ before throwing SIM901.
Desired change
Explanation
If
x
wereint
/float
, then theSIM901
should be thrown, as thebool
cast is unnecessary.However, since
__lt__
/__gt__
can return non-bool
if overridden, abool
cast is useful here.Conclusion: I think flake8-simplify should examine the type of
x
for an overridden__lt__
/__gt__
before throwingSIM901
.