Follow-up tasks for `redundant-condition(-strict)` rules
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.Similarly, mypy's
redundant-exprdiagnostic triggers on code such as this (multiplay gist: bdd9ec316ce12e024e6be186392bd703), which is not flagged by any ty rule currently:def normalize(s: str): return s is not None and sIt 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:def f(x: int, y: bool): if not isinstance(x, int): assert yThat 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.
Currently 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:assert 1 == 2 print('this looks like it was meant to be reachable')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?while True: pass print('this looks like it was 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?Improve internal and external documentation reguarding "short-circuit" conditions, per @carljm's excellent analysis here
Source: astral-sh/ty