Skip to content

Commit 28536b5

Browse files
Fix IsADirectoryError for namespace packages when using --linecoverage-report (#20109)
Fixes #18128. This fixes an Internal Error where namespace packages were not supported properly. This fix was inspired by @sterliakov noticing that this bug was very similar to #19843, which has a similar fix. Note that we use `os.path.isdir(tree.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?) Anyway, we just early-return — which, based on my manual testing and reading the code, seems to be the right thing to do here. --------- Co-authored-by: Ivan Levkivskyi <[email protected]>
1 parent 00df9a6 commit 28536b5

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

mypy/report.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ def on_file(
421421
type_map: dict[Expression, Type],
422422
options: Options,
423423
) -> None:
424+
if os.path.isdir(tree.path): # can happen with namespace packages
425+
return
426+
424427
with open(tree.path) as f:
425428
tree_source = f.readlines()
426429

test-data/unit/reports.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ namespace_packages = True
548548
</body>
549549
</html>
550550

551+
[case testReportIsADirectoryErrorCrashOnNamespacePackages]
552+
# cmd: mypy --linecoverage-report report -p folder
553+
-- Regression test for https://github.com/python/mypy/issues/18128
554+
-- "IsADirectoryError for namespace packages when using --linecoverage-report"
555+
[file folder/subfolder/something.py]
556+
class Something:
557+
pass
558+
551559
[case testReportCoberturaCrashOnNamespacePackages]
552560
# cmd: mypy --cobertura-xml-report report -p folder
553561
-- Regression test for https://github.com/python/mypy/issues/19843

0 commit comments

Comments
 (0)