diff --git a/mypy/report.py b/mypy/report.py index 398127a33026..4a0b965077f6 100644 --- a/mypy/report.py +++ b/mypy/report.py @@ -169,11 +169,10 @@ def on_file( ) -> None: # Count physical lines. This assumes the file's encoding is a # superset of ASCII (or at least uses \n in its line endings). - try: + if not os.path.isdir(tree.path): # can happen with namespace packages with open(tree.path, "rb") as f: physical_lines = len(f.readlines()) - except IsADirectoryError: - # can happen with namespace packages + else: physical_lines = 0 func_counter = FuncCounterVisitor() diff --git a/test-data/unit/reports.test b/test-data/unit/reports.test index 396414575948..cce2f7295e3b 100644 --- a/test-data/unit/reports.test +++ b/test-data/unit/reports.test @@ -548,6 +548,13 @@ namespace_packages = True +[case testLinecountReportCrashOnNamespacePackages] +# cmd: mypy --linecount-report report -p folder +-- Regression test for https://github.com/python/mypy/issues/15979 +[file folder/subfolder/something.py] +class Something: + pass + [case testReportIsADirectoryErrorCrashOnNamespacePackages] # cmd: mypy --linecoverage-report report -p folder -- Regression test for https://github.com/python/mypy/issues/18128