python:S1721 walrus operator parentheses false positive

Hello.

Problematic rule is Python static code analysis: Parentheses should not be used after certain keywords Parentheses should not be used after certain keywords. In code view it says “Remove the parentheses after this “not” keyword.”.

SonarQube
Version: 8.9.5.50698
Date: 2022-05-02

Sample code (I don’t think more context is necessary):

if not (article_tag := soup.find("article")):
    ...

Sonar reports to remove parentheses around walrus operator assignment. This isn’t a valid issue in Python 3.8 (at least) because removing parentheses causes SyntaxError (run through pytest):

E     File "/home/some/path/file.py.", line 9999
E       if not article_tag := soup.find("article"):
E          ^
E   SyntaxError: cannot use assignment expressions with operator

Removing not keyword in non-parentheses version “solves” SyntaxError but changing condition is not a valid solution here. Parentheses are required.

Hello @Hubert_Wlodarczyk,

Thank you for reporting this false positive. I created this ticket to track it. Hopefully we should fix this soon.

Cheers,
Guillaume

1 Like