Repository: python/mypy

Issue:
While checking package: 'IsADirectoryError' on directories not containing any '.py'-files
**Crash Report**

With the configuration listed below mypy seems to choke on the directory '2023-08-17_16-21-31'. (Paths have been shortened to the relevant parts)

**Traceback**

```
(.venv) pc: .../recipe2txt$ mypy --show-traceback
.../recipe2txt/test/reports_test4recipe2txt/2023-08-17_16-21-31: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.6.0+dev.010da0b2f48dc92be2f79495fd4551c92351868f
Traceback (most recent call last):
  File ".../recipe2txt/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/main.py", line 99, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/main.py", line 178, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 189, in build
    result = _build(
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 262, in _build
    graph = dispatch(sources, manager, stdout)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2938, in dispatch
    process_graph(graph, manager)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 3336, in process_graph
    process_stale_scc(graph, scc, manager)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 3441, in process_stale_scc
    graph[id].finish_passes()
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2364, in finish_passes
    with self.wrap_context():
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2063, in wrap_context
    yield
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2399, in finish_passes
    manager.report_file(self.tree, self.type_map(), self.options)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 866, in report_file
    self.reports.file(file, self.modules, type_map, options)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/report.py", line 91, in file
    reporter.on_file(tree, modules, type_map, options)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/report.py", line 174, in on_file
    with open(tree.path, "rb") as f:
         ^^^^^^^^^^^^^^^^^^^^^
IsADirectoryError: [Errno 21] Is a directory: '.../recipe2txt/test/reports_test4recipe2txt/2023-08-17_16-21-31'
.../recipe2txt/test/reports_test4recipe2txt/2023-08-17_16-21-31: : note: use --pdb to drop into pdb)
```

**To Reproduce**

TODO

**Your Environment**

- Mypy version used: 1.6.0+dev.010da0b2f48dc92be2f79495fd4551c92351868f
- Mypy command-line flags: `--show-traceback`
- Mypy configuration options from `pyproject.toml`:
```toml
[tool.mypy]
python_version = '3.11'
strict = true
python_executable = ".venv/bin/python"
pretty = true
warn_unused_configs = true
exclude = 'test/test_*'
packages = ['recipe2txt', 'test']
linecount_report = 'report.txt'

[[tool.mypy.overrides]]
module = 'validators'
ignore_missing_imports = true
```
- Python version used: 3.11.4
- OS: Kubuntu 23.04 x86_64
- Kernel: 6.2.0-27-generic


Output of the `tree`-command in the parent-directory of the crash-causing-directory
```
pc:.../recipe2txt/test/reports_test4recipe2txt$ tree
.
├── 2023-08-08_17-30-40
│   ├── output.md
│   ├── parameters.txt
│   └── urls_used.txt
├── 2023-08-08_17-38-55
│   ├── parameters.txt
│   └── urls_used.txt
├── 2023-08-16_12-43-46
├── 2023-08-16_12-44-22.zip
├── 2023-08-17_15-50-57.zip
├── 2023-08-17_15-51-53.zip
├── 2023-08-17_15-52-50.zip
├── 2023-08-17_15-53-50.zip
├── 2023-08-17_15-54-15
│   ├── output.md
│   ├── parameters.txt
│   └── urls_used.txt
├── 2023-08-17_15-54-15.zip
├── 2023-08-17_15-57-14.zip
├── 2023-08-17_16-18-54.zip
├── 2023-08-17_16-19-55.zip
├── 2023-08-17_16-21-31
│   ├── output.md
│   ├── parameters.txt
│   └── urls_used.txt
├── 2023-08-17_16-22-28.zip
├── 2023-08-17_16-28-32.zip
└── urls_shuffled.txt
```



Discussion:
```
.../recipe2txt/test/testfiles/permanent/txt: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.6.0+dev.010da0b2f48dc92be2f79495fd4551c92351868f
Traceback (most recent call last):
  File ".../recipe2txt/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/main.py", line 99, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/main.py", line 178, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 189, in build
    result = _build(
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 262, in _build
    graph = dispatch(sources, manager, stdout)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2938, in dispatch
    process_graph(graph, manager)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 3336, in process_graph
    process_stale_scc(graph, scc, manager)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 3441, in process_stale_scc
    graph[id].finish_passes()
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2364, in finish_passes
    with self.wrap_context():
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2063, in wrap_context
    yield
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 2399, in finish_passes
    manager.report_file(self.tree, self.type_map(), self.options)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/build.py", line 866, in report_file
    self.reports.file(file, self.modules, type_map, options)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/report.py", line 91, in file
    reporter.on_file(tree, modules, type_map, options)
  File ".../recipe2txt/.venv/lib/python3.11/site-packages/mypy/report.py", line 174, in on_file
    with open(tree.path, "rb") as f:
         ^^^^^^^^^^^^^^^^^^^^^
IsADirectoryError: [Errno 21] Is a directory: '.../recipe2txt/test/testfiles/permanent/txt'
.../recipe2txt/test/testfiles/permanent/txt: : note: use --pdb to drop into pdb
```
pyproject.toml:

```toml
[tool.mypy]
python_version = '3.11'
strict = true
python_executable = ".venv/bin/python"
pretty = true
warn_unused_configs = true
#exclude = ['test/test_*', 'test/reports*']
packages = ['recipe2txt', 'test.testfiles.permanent']
linecount_report = '.'
warn_unreachable = true

[[tool.mypy.overrides]]
module = 'validators'
ignore_missing_imports = true
```
tree (of `recipe2txt/test/testfiles/permanent`):
```
├── -7_-29_23_37.txt
├── all_urls.txt
├── gen_log.py
├── gen_stack.py
├── html
│   ├── Arroz_con_Pollo.html
│   ├── Asian_Chicken_Salad.html
│   ├── Au_Jus_Sandwich.html
│   ├── Borscht.html
│   └── FAIL_RECIPE.html
├── __init__.py
├── LICENSE
├── logfiles
│   ├── critical
│   ├── debug
│   ├── error
│   ├── info
│   └── warning
├── md
│   ├── Arroz_con_Pollo.md
│   ├── Asian_Chicken_Salad.md
│   ├── Au_Jus_Sandwich.md
│   ├── Borscht.md
│   └── recipes.md
├── obscured_urls.txt
├── parsed
│   ├── Arroz_con_Pollo.parsed
│   ├── Asian_Chicken_Salad.parsed
│   ├── Au_Jus_Sandwich.parsed
│   └── Borscht.parsed
├── __pycache__
│   ├── gen_log.cpython-311.pyc
│   ├── gen_stack.cpython-311.pyc
│   ├── __init__.cpython-311.pyc
│   └── testfile_generator.cpython-311.pyc
├── testfile_db.sqlite3
├── testfile_generator.py
├── traces
│   └── -7_-29_23_37.txt
├── txt
│   ├── Arroz_con_Pollo.txt
│   ├── Asian_Chicken_Salad.txt
│   ├── Au_Jus_Sandwich.txt
│   ├── Borscht.txt
│   └── recipes.txt
├── unobscured_urls.txt
└── URLs
```




Resolving patch:
diff --git a/mypy/report.py b/mypy/report.py
--- a/mypy/report.py
+++ b/mypy/report.py
@@ -171,8 +171,12 @@ 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).
-        with open(tree.path, "rb") as f:
-            physical_lines = len(f.readlines())
+        try:
+            with open(tree.path, "rb") as f:
+                physical_lines = len(f.readlines())
+        except IsADirectoryError:
+            # can happen with namespace packages
+            physical_lines = 0
 
         func_counter = FuncCounterVisitor()
         tree.accept(func_counter)


Test patch: