Skip to content

Commit

Permalink
Add missing lines-covered and lines-valid attributes (#17738)
Browse files Browse the repository at this point in the history
This PR resolves an issue where the Cobertura XML report generated by
MyPy was missing the lines-covered and lines-valid attributes in the
<coverage> element.

Changes made:

- Added the lines-covered and lines-valid attributes to the <coverage>
element in the Cobertura XML report.
- Updated the CoberturaReportSuite test suite to validate that these
attributes are correctly included in the generated XML.

Fixes #17689
  • Loading branch information
x612skm committed Sep 15, 2024
1 parent 8b1e606 commit 77919cf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mypy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ def on_finish(self) -> None:
self.root_package.covered_lines, self.root_package.total_lines
)
self.root.attrib["branch-rate"] = "0"
self.root.attrib["lines-covered"] = str(self.root_package.covered_lines)
self.root.attrib["lines-valid"] = str(self.root_package.total_lines)
sources = etree.SubElement(self.root, "sources")
source_element = etree.SubElement(sources, "source")
source_element.text = os.getcwd()
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/reports.test
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def bar() -> str:
def untyped_function():
return 42
[outfile build/cobertura.xml]
<coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="0.7500" branch-rate="0">
<coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="0.7500" branch-rate="0" lines-covered="6" lines-valid="8">
<sources>
<source>$PWD</source>
</sources>
Expand Down Expand Up @@ -81,7 +81,7 @@ def foo(a: int) -> MyDict:
return {"a": a}
md: MyDict = MyDict(**foo(42))
[outfile build/cobertura.xml]
<coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="1.0000" branch-rate="0">
<coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="1.0000" branch-rate="0" lines-covered="6" lines-valid="6">
<sources>
<source>$PWD</source>
</sources>
Expand Down

0 comments on commit 77919cf

Please sign in to comment.