Invalid JUnit XML being generated due to <type> hint
Bug Report The JUnit XML generated by MyPy is invalid.
To Reproduce Create a list without a type annotation and generate a JUnit XML report using MyPy.
def dummy_function():
abc = []
for i in ["asd", 123]:
abc.append(i)
Expected Behavior I expect a valid XML document to be generated.
Actual Behavior
The XML is unable to be parsed due to a mismatched tag. I believe this is because of the presence of a <type> mask in a hint.
error: Need type annotation for "abc" (hint: "abc: List[<type>] = ...")
This is the XML error message that is displayed
XML Parsing Error: mismatched tag. Expected: </type>
Location: [file_path]
Line Number XXX, Column XXX:
[Error location]:8: error: Function is missing a type annotation [no-untyped-def]</failure>
Your Environment Mac with Sonoma 14.3 and using the Poetry package manager
- Mypy version used: 1.8.0
- Mypy command-line flags: --explicit-package-bases --strict --junit-xml ./mypy-report.xml
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.10
I have had this same problem (also on mypy 1.8.0) and came looking for similar reports. Can confirm it is not only <type>, the report does not seem to escape any xml control characters. For example also < and > as operators:
x: None = None
y: None = None
x < y
x > y
mypy example.py --junit-xml=./mypy-report.xml
<?xml version="1.0" encoding="utf-8"?>
<testsuite errors="0" failures="1" name="mypy" skips="0" tests="1" time="0.532">
<testcase classname="mypy" file="mypy" line="1" name="mypy-py3_10-darwin" time="0.532">
<failure message="mypy produced messages">example.py:3: error: Unsupported left operand type for < ("None") [operator]
example.py:4: error: Unsupported left operand type for > ("None") [operator]</failure>
</testcase>
</testsuite>
Try to parse the resulting report and it is confirmed invalid:
import xml.etree.ElementTree
tree = xml.etree.ElementTree.parse('mypy-report.xml')
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 4, column 102
Ah wait, maybe this has been fixed already? in https://github.com/python/mypy/commit/4aba5ca8450ad3f06805f2bd6dfcd06048a3f1d2
I also faced this issue and now replaced my command:
mypy files --junit-xml=outputs/mypy-results.xml --junit-format=per_file
with:
mypy files | mypy2junit > outputs/mypy-results.xml
see this comment which suggested it: https://github.com/python/mypy/issues/10102#issuecomment-1419741294