Sonarcloud: Unable to parse file -> parse error at line # when analysing Python code formatted by Black

python

(Robin Mackaij) #1

Edit: I found that this issue is already logged.
https://jira.sonarsource.com/browse/SONARPY-290

===================================================================================
Today we first ran an analysis on Python code that is auto-formatted by Black (https://github.com/ambv/black). In a number of files, Sonarcloud throws a parse error: Unable to parse file: … Parse error at line # …

After looking into the code, we found the following issue with the parsing. If this example function

def do_nothing(*,some_string: str,another_string: str,some_number: int,another_number: int = 8) -> None:
        pass

is formatted by Black, it comes out as

def do_nothing(
    *,
    some_string: str,
    another_string: str,
    some_number: int,
    another_number: int = 8,
) -> None:
    pass

The parse error is causes by the , after the 8. Note that that , is not necessary (and other auto-formatters do not add it) but it is not wrong either; the code runs perfectly fine with or without that extra , and no linting warnings are generated in either case. So Sonarcloud throws an error on working, pep8-compliant code.


(Alban Auzeill) #3

Hi Robin,
You are right, if code auto-formatted by Black produces a parse error when there’s variable number of arguments, we should fix this bug in the next version of SonarPython. I added a comment on SONARPY-290 to ensure this.
Thanks for the feedback,
Alban


(Robin Mackaij) #4

Hello Alban,

Great to hear that, we’re looking forward to it!

Met vriendelijke groet / Kind regards,

Robin