Bug with sonar-python parsing mypy output

Deployment: Docker container
Sonarqube Version: Developer 2025.4.2
MYPY Version 1.17.0

Bug:

The regex used to parse Mypy’s text output is not robust enough to handle multi-line error messages caused by terminal line wrapping. This is running on debian 12 from a bash shell in CICD using jenkins.

The current regex in MypySensor.java on line 52 is:

Pattern.compile("^(?<file>[^:]+):(?<startLine>\\d+)(?::(?<startCol>\\d+))?(?::\\d+:\\d+)?: (?<severity>\\S+[^:]): (?<message>.*?)(?: \\[(?<code>.*)])?\\s*$");

Side note: Your tests should really be running on the live output of the various tooling.

Seemingly you need to run mypy as(this stuff should really be in your documentation, which is woefully lacking):

mypy . > mypy_report.txt

to capture the output in a format that sonarqube can parse.

I’ve run into an issue where the shell output ends split over multiple lines.

Parser expects(single line):
	src/file.py:12: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
	
Mypy output redirected from stdout to a file(multiple lines):
	src/file.py:12: error: Function is missing a type 
	annotation for one or more arguments  [no-untyped-def]

Workaround:

This helps avoid it splitting the line into 2

export COLUMNS=200; mypy . > mypy_report.txt

Quick Fix:

  1. Change the regex to match multiple lines.

Long-term Fix:
Use a structured output format and parse that.

mypy . -O=json > mypy.json

Sample json output: 
	{
		"file": "src/file.py", 
		"line": 12, 
		"column": 4, 
		"message": "Function is missing a type annotation for one or more arguments", 
		"hint": "See https://mypy.rtfd.io/en/stable/_refs.html#code-no-untyped-def for more info", 
		"code": "no-untyped-def", 
		"severity": "error"
	}

Thanks

Hi @Yardyharhar ,

Welcome and thank you very much for your detailed report. I am glad that you have found a workaround, still I have created a ticket to fix this issue.

Thank you again for reporting this issue.

Regards,

David