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
Mypy's truthy-function rule also triggers for Callable annotations; ours does not, currently, because we model non-function-like Callables as having ambiguous truthiness (Multiplay gist: d120c34286b5c1b438f128e33cdbc789). We could experiment with having a third rule that triggers on Callable-annotated variables that are used in boolean contexts. Similarly, mypy's truthy-iterable error code flags arbitrary Iterable[]- and Iterator[]-typed variables used in boolean contexts. We currently only flag generators, since Iterable and Iterator have ambiguous truthiness.
Similarly, mypy's redundant-expr diagnostic triggers on code such as this (multiplay gist: bdd9ec316ce12e024e6be186392bd703), which is not flagged by any ty rule currently:
defnormalize(s: str):
returnsisnotNoneands
It would be good to look into exactly what heuristics mypy is using to flag that without having too many false-positive errors. We left all and and or expressions out of the initial implementation when they occurred outside boolean tests; it felt too complex to get this right without too many false positives. Perhaps we could flag these as part of redundant-condition(-strict), or perhaps we could have a separate rule for cases like this.
The rules currently err on the side of caution by assuming that this assert is meant to mark the branch as "deliberately unreachable", because the assertcould evaluate to False:
That probably leads to us having false negatives in some situations; it would be good to experiment with this and see if refining the heuristic to only count assert statements that are definitelyFalse actually leads to an increase in false positives or not
Add subdiagnostics highlighting the reachability impliciations of a test being either always-truthy or always-falsy.
Currently we only flag assert statements if the type of the test being asserted is not assignable to bool or int. But perhaps we could also detect cases like this, where it appears that the suite below the assert is intended to be reachable, but we infer the assert as always failing due to the condition always being falsy:
assert1==2print('this looks like it was meant to be reachable')
Currently we assume that any condition that is an AST-literal True, False, 1 or 0 is deliberate. But maybe we should flag something like this, on the grounds that it clearly looks like the suite after the while is meant to be reachable?
whileTrue:
passprint('this looks like it was meant to be reachable')
Suppress diagnostics on Call expressions that return None, when they occur inside and, or or not expressions? These often have side effects.
Mypy's
truthy-functionrule also triggers forCallableannotations; ours does not, currently, because we model non-function-likeCallables as having ambiguous truthiness (Multiplay gist: d120c34286b5c1b438f128e33cdbc789). We could experiment with having a third rule that triggers onCallable-annotated variables that are used in boolean contexts. Similarly, mypy'struthy-iterableerror code flags arbitraryIterable[]- andIterator[]-typed variables used in boolean contexts. We currently only flag generators, sinceIterableandIteratorhave ambiguous truthiness.Callable,Iterable,IteratororGeneratortypes in a boolean context ruff#28554Similarly, mypy's
redundant-exprdiagnostic triggers on code such as this (multiplay gist: bdd9ec316ce12e024e6be186392bd703), which is not flagged by any ty rule currently:It would be good to look into exactly what heuristics mypy is using to flag that without having too many false-positive errors. We left all
andandorexpressions out of the initial implementation when they occurred outside boolean tests; it felt too complex to get this right without too many false positives. Perhaps we could flag these as part ofredundant-condition(-strict), or perhaps we could have a separate rule for cases like this.The rules currently err on the side of caution by assuming that this
assertis meant to mark the branch as "deliberately unreachable", because theassertcould evaluate toFalse:That probably leads to us having false negatives in some situations; it would be good to experiment with this and see if refining the heuristic to only count
assertstatements that are definitelyFalseactually leads to an increase in false positives or notAdd subdiagnostics highlighting the reachability impliciations of a test being either always-truthy or always-falsy.
redundant-conditionrules warning about reachability implications ruff#28263while,assertandmatchstatements ruff#28290Currently we only flag
assertstatements if the type of the test being asserted is not assignable toboolorint. But perhaps we could also detect cases like this, where it appears that the suite below theassertis intended to be reachable, but we infer theassertas always failing due to the condition always being falsy:[ty] Diagnose failing assertions followed by nontrivial statements ruff#28357
Currently we assume that any condition that is an AST-literal
True,False,1or0is deliberate. But maybe we should flag something like this, on the grounds that it clearly looks like the suite after thewhileis meant to be reachable?Suppress diagnostics on
Callexpressions that returnNone, when they occur insideand,orornotexpressions? These often have side effects.Move "short-circuit" conditions from
redundant-condition-stricttoredundant-condition?redundant-conditionruff#28356