Parse Error in SonarPython when trailing comma on kwargs

python

(Adam Parkin) #1

Steps to Reproduce

Define a function that takes *args & **kwargs and has a trailing comma on the **kwargs argument, ex:

class FooBar:
    def __init__(self, arg1, arg2, arg3, arg4, arg5, arg6, *args, *kwargs,):
        pass

With v1.10 of the SonarPython plugin, this produces a parse error:

 ERROR: Parse error at line 250 column 20:
 242: ,
 243: arg1: bytes,
 244: arg2: bytes,
 245: arg3: bytes,
 246: arg4: bytes,
 247: arg5: bytes,
 248: arg6: int,
 249: *args,
 --> **kwargs,
 251: ):

Yet this is valid syntax for Python 3.6+, see:


and
https://bugs.python.org/issue9232#msg248449

It’s worth noting that the popular code formatter Black (https://github.com/ambv/black) adds the trailing comma when formatting a function like this (we run our code through Black to ensure consistency across the team, which means even if we remove the comma manually, once the code is run through Black the comma will be returned).


(Alban Auzeill) #2

Hi Adam,
You’re right, I can reproduce the SonarPython 1.10 “Parse error” on valid code according to python 3.6.4.
I created the bug ticket SONARPY-290, it will be fixed in the next version.
Thanks for your detailed feedback, I really appreciated,
Alban


(Adam Parkin) #3

Awesome thanks, much appreciated.