Tuples in if statements are causing FP for python:S1721

python

(Testworksau) #1

The if statement in the following code currently triggers S1721 Remove the parentheses after this "if" keyword.

my_tuple_list = [('foo', 'bar')]
if ('foo', 'bar') in my_tuple_list: 
    print("True")
>> True

The parenthesis in the tuple is required in order to match the in condition correctly:

('foo', 'bar') in my_tuple_list
>> True

'foo', 'bar' in my_tuple_list
>> ('foo', False)

It’s not valid syntax to remove the parenthesis around the tuple within the if statement either.

if 'foo', 'bar' in my_tuple_list: print("True")
  File "<ipython-input-18-0fc353d04681>", line 1
    if 'foo', 'bar' in my_tuple_list: print("True")
            ^
SyntaxError: invalid syntax

SonarQube details are:

INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Windows 10 10.0 amd64
INFO: SonarQube server 7.2.1

Python version is Python 3.6.6 on Windows.


(Pierre-Yves Nicolas) #2

Indeed, that’s a false positive.
I created a ticket to track it: SONARPY-292

Thanks a lot for the feedback!