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

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.

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

Hello Alban,

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

Met vriendelijke groet / Kind regards,

Robin

I’m still getting this error even with the latest docker-image from sonar-scanner. Here’s the version I tested this with:

bash-5.0# sonar-scanner --version
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.5.0.2216
INFO: Java 11.0.6 AdoptOpenJDK (64-bit)
INFO: Linux 4.15.0-115-generic amd64

And the corresponding docker image hash:

sonarsource/sonar-scanner-cli   d2689cf15e84

Considering that the linked bug has been closed in Jan. 2019 I wonder if there’s not something I’m missing here?

Do I also need to upgrade SonarQube itself instead of only the scanner to get this working?