python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.55k stars 2.84k forks source link

`IsADirectoryError` for namespace packages when using `--linecoverage-report` #18128

Open AAriam opened 2 weeks ago

AAriam commented 2 weeks ago

Crash Report

This is similar to #15979:

For namespace packages, e.g., when a package contains a data directory without any Python files, IsADirectoryError is raised when using the --linecoverage-report option. This happens when using the --package option, but not when passing the directory path.

Traceback

version: 1.13.0
Traceback (most recent call last):
  File "/Users/home/mambaforge/envs/test-mypy/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ~~~~~~~~~~~~~^^
  File "/Users/home/mambaforge/envs/test-mypy/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
    ~~~~^^
  File "mypy/main.py", line 102, in main
  File "mypy/main.py", line 186, in run_build
  File "mypy/build.py", line 194, in build
  File "mypy/build.py", line 269, in _build
  File "mypy/build.py", line 2935, in dispatch
  File "mypy/build.py", line 3333, in process_graph
  File "mypy/build.py", line 3438, in process_stale_scc
  File "mypy/build.py", line 2397, in finish_passes
  File "mypy/build.py", line 873, in report_file
  File "mypy/report.py", line 91, in file
  File "mypy/report.py", line 426, in on_file
IsADirectoryError: [Errno 21] Is a directory: '/Users/home/mambaforge/envs/test-mypy/lib/python3.13/site-packages/test_package/data'

To Reproduce

  1. Create a package:
    .
    ├── src/
    │   └── test_package/
    │       ├── data/
    │       │   └── test.txt
    │       ├── __init__.py
    │       └── py.typed
    └── pyproject.toml

    with pyproject.toml:

    
    [project]
    name = "test_package"
    version = "0.0.0"

[build-system] requires = ["setuptools >= 72.1.0"] build-backend = "setuptools.build_meta"

[tool] [tool.setuptools] include-package-data = true

[tool.setuptools.packages.find] where = ["src"] namespaces = true



2. Install the package: `pip install .`
3. Run mypy: `mypy --package test_package --linecoverage-report . --raise-exceptions`

**Your Environment**

<!-- Include as many relevant details about the environment you experienced the bug in -->

- Mypy version used: mypy 1.13.0 (compiled: yes)
- Mypy command-line flags: `--package`, `--linecoverage-report`, `--raise-exceptions`
- Python version used: 3.13.0
- Operating system and version: macOS 13.7
brianschubert commented 2 weeks ago

I am able to reproduce on master (3b00002ac), ~though only when using an editable install. It seems that include-package-data = true isn't enough to make setuptools include the data directory in the installed package for me.~ (edit: nevermind, this was due to the files being in a .gitignore'd directory, which caused setuptools to exclude them)

/home/brian/Projects/open-contrib/mypy/temp_build/src/src/test_package/data: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues
version: 1.14.0+dev.3b00002acdf098e7241df8f2e1843f8b8260b168
Traceback (most recent call last):
  File "/home/brian/Projects/open-contrib/mypy/.venv313/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ~~~~~~~~~~~~~^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/__main__.py", line 15, in console_entry
    main()
    ~~~~^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/main.py", line 109, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
                              ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/main.py", line 193, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 194, in build
    result = _build(
        sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr, extra_plugins
    )
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 269, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 2940, in dispatch
    process_graph(graph, manager)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 3338, in process_graph
    process_stale_scc(graph, scc, manager)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 3443, in process_stale_scc
    graph[id].finish_passes()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 2402, in finish_passes
    manager.report_file(self.tree, self.type_map(), self.options)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 873, in report_file
    self.reports.file(file, self.modules, type_map, options)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/report.py", line 91, in file
    reporter.on_file(tree, modules, type_map, options)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/report.py", line 426, in on_file
    with open(tree.path) as f:
         ~~~~^^^^^^^^^^^
IsADirectoryError: [Errno 21] Is a directory: '/home/brian/Projects/open-contrib/mypy/temp_build/src/src/test_package/data'