Skip to content

Commit 842a8fd

Browse files
wyattscarpenterpre-commit-ci[bot]ilevkivskyi
authored
Add a regression test for #15979, and fix linecount-report for Windows (#20111)
#16019, which fixed this issue (for linux) was not accompanied by a regression test. Thus, nobody noticed that it doesn't work on Windows! This fixes an Internal Error where namespace packages were not supported properly. This fix was inspired by @sterliakov noticing that #18128 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?) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ivan Levkivskyi <[email protected]>
1 parent 31a915a commit 842a8fd

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

mypy/report.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ def on_file(
169169
) -> None:
170170
# Count physical lines. This assumes the file's encoding is a
171171
# superset of ASCII (or at least uses \n in its line endings).
172-
try:
172+
if not os.path.isdir(tree.path): # can happen with namespace packages
173173
with open(tree.path, "rb") as f:
174174
physical_lines = len(f.readlines())
175-
except IsADirectoryError:
176-
# can happen with namespace packages
175+
else:
177176
physical_lines = 0
178177

179178
func_counter = FuncCounterVisitor()

test-data/unit/reports.test

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

551+
[case testLinecountReportCrashOnNamespacePackages]
552+
# cmd: mypy --linecount-report report -p folder
553+
-- Regression test for https://github.com/python/mypy/issues/15979
554+
[file folder/subfolder/something.py]
555+
class Something:
556+
pass
557+
551558
[case testReportIsADirectoryErrorCrashOnNamespacePackages]
552559
# cmd: mypy --linecoverage-report report -p folder
553560
-- Regression test for https://github.com/python/mypy/issues/18128

0 commit comments

Comments
 (0)