pylint-dev/pylint
C0325 (superfluous-parens) appears to not trigger when contents are a string
Closed
#9,878 opened on Aug 16, 2024
False Negative 🦋Good first issueNeeds investigation 🔬
Repository metrics
- Stars
- (4,978 stars)
- PR merge metrics
- (Avg merge 12h 9m) (29 merged PRs in 30d)
Description
Current problem
When superfluous-parens surround a string, no warning is raised. This can lead to errors in functions designed to test strings against tuples of acceptable values, such as when loading a bunch of configurations from a text file.
def testMethod(testString):
"""
Test method to demonstrate C0325 (superfluous-parens) gets triggered after if, but not in keywords
"""
if (testString == "option1"): # superfluous-parens
return 1
if testString == ("option2"): # no warning
return 2
if testString in ("option1","option2"):
return 3
if testString in ("option4"): # no warning
return 4
if testString == "option":
raise ValueError("Did you forget to specify an option number?")
return "None of the above"
print(testMethod("option"))
Desired solution
Warning on ("string") after a keyword.
Additional context
The example code (specifically the return 4 block) might arise when comparing against tuples of valid strings, and all but one option has been removed. If the comma is forgotten, then this method will return 4, which might be unexpected to a user. This change would help avoid this issue in an evolving code base.