Skip to content

Commit 00df9a6

Browse files
Fix an INTERNAL ERROR when creating cobertura output for namespace package (#20112)
Fixes #19843 This fixes an Internal Error where namespace packages were not supported properly. We have to use `os.path.isdir(path)` instead of trying to catch an `IsADirectoryError` exception because of a bug on Windows which causes it to throw a `PermissionError` instead in [the relevant situation](https://discuss.python.org/t/permissionerror-errno-13-permission-denied-python-2023/22360/8), which makes `except IsADirectoryError` unreliable. (We also can't just `except (IsADirectoryError, PermissionError)` because what if there is an actual permission error?)
1 parent 0ece662 commit 00df9a6

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

mypy/report.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,9 @@ def should_skip_path(path: str) -> bool:
141141

142142
def iterate_python_lines(path: str) -> Iterator[tuple[int, str]]:
143143
"""Return an iterator over (line number, line text) from a Python file."""
144-
try:
144+
if not os.path.isdir(path): # can happen with namespace packages
145145
with tokenize.open(path) as input_file:
146146
yield from enumerate(input_file, 1)
147-
except IsADirectoryError:
148-
# can happen with namespace packages
149-
pass
150147

151148

152149
class FuncCounterVisitor(TraverserVisitor):

test-data/unit/reports.test

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,37 @@ namespace_packages = True
547547
</table>
548548
</body>
549549
</html>
550+
551+
[case testReportCoberturaCrashOnNamespacePackages]
552+
# cmd: mypy --cobertura-xml-report report -p folder
553+
-- Regression test for https://github.com/python/mypy/issues/19843
554+
[file folder/subfolder/something.py]
555+
-- This output is not important, but due to the way tests are run we need to check it.
556+
[outfile report/cobertura.xml]
557+
<coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="1.0" branch-rate="0" lines-covered="0" lines-valid="0">
558+
<sources>
559+
<source>$PWD</source>
560+
</sources>
561+
<packages>
562+
<package complexity="1.0" name="folder" branch-rate="0" line-rate="1.0">
563+
<classes>
564+
<class complexity="1.0" filename="folder" name="folder" branch-rate="0" line-rate="1.0">
565+
<methods/>
566+
<lines/>
567+
</class>
568+
<class complexity="1.0" filename="folder/subfolder" name="subfolder" branch-rate="0" line-rate="1.0">
569+
<methods/>
570+
<lines/>
571+
</class>
572+
</classes>
573+
</package>
574+
<package complexity="1.0" name="folder.subfolder" branch-rate="0" line-rate="1.0">
575+
<classes>
576+
<class complexity="1.0" filename="folder/subfolder/something.py" name="something.py" branch-rate="0" line-rate="1.0">
577+
<methods/>
578+
<lines/>
579+
</class>
580+
</classes>
581+
</package>
582+
</packages>
583+
</coverage>

0 commit comments

Comments
 (0)