Sonar-scanner not able to parse python fstrings

  • Versions used (SonarQube, Scanner, Plugin, and any relevant extension)
    • sonar-scanner: 4.6.2.2472
  • Error observed (wrap logs/code around triple quote ``` for proper formatting)
    • Couldn't parse '/home/.../internet_monitor.py' as Python source: 'invalid syntax' at line 72
    • Content of the line: print(f'No internet')
  • Steps to reproduce
    • Analyse a python file with such content
  • Potential workaround
    • Do not use format strings?
  • In case of SonarCloud:
    • ALM used (GitHub, Bitbucket Cloud, Azure DevOps)
      • Bitbucket
    • CI system used (Bitbucket Cloud, Azure DevOps, Travis CI, Circle CI, Jenkins, other)
      • Bitbucket pipelines

Hey there.

I had no trouble analyzing a file with just this line

print(f'No internet')

test.py.zip (214 Bytes)

Does such a file analyze successfully for you? If so, can you provide a more detailed reproducer?

1 Like

Our whole CI system takes a while. Do you have a simple test to verify operation using sonar-scanner?

Alright, one step further. I can analyse files with print(f'No internet') in it.

But not when coverage tests have been ran against these files. Disabling python coverage fixes the analysis.

Would you mind trying with coverage?

Hi @Tmiple ,
Are you sure this is coming from sonar-scanner? Python analyzer doesn’t do any parsing related to coverage: it just reads the provided coverage file.

The error message seems coming from coverage tool instead. Since f-string have been introduced from Python 3.6 , can you double check what Python version is used in your CI environment?

That is very well spotted! Turns out the issue was there. The logging from our CI was not synchonous it seems. Leading me to blame sonar-scanner.

Appologies!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.