Open tauferp opened 1 year ago
I recently found another case where W8201
is a false positive.
A minimal reproducible example:
x, y = 1, 2
for t in range(2):
if t % 2:
a, b = x, y
else:
a, b = 2, 2
# logic involving `a` and `b`
And another case:
def test():
d = {'key': 'some value here'}
o = object()
for key, value in d.items():
if key == 'foo':
s1, *_ = value.split(' ')
a = getattr(o, s1) # W8201: Consider moving this expression outside of the loop. (loop-invariant-statement)
return a
It looks like a = getattr(o, s1)
is independent of the loop variables key
and value
, but s1
← value.split(' ')
depends on a loop variable and therefore isn’t invariant.
Following sample yields incorrect loop-invariant-statement
sample.py:4:15: W8201: Consider moving this expression outside of the loop. (loop-invariant-statement)