Skip to content

mypy recurses into build directory and fails #18429

@asarkar

Description

@asarkar

Bug Report

I’ve a project structured as follows:

.
├── hello
│   ├── __init__.py
│   └── animal.py
├── tests
│   ├── __init__.py
│   └── test_animal.py
├── README
└── pyproject.toml

This is just a personal Python library, and doesn’t need to be published or distributed. The usage consists of running pytest and mypy from the root directory.

Among other things, the pyproject.toml contains the following sections:

[project.optional-dependencies]
test = [
    "pytest",
]
lint = [
    "ruff",
    "mypy",
]

[tool.mypy]
exclude = [
    'venv',
]
ignore_errors = false
warn_return_any = true
disallow_untyped_defs = true

The dependencies are installed as follows:

% ./venv/bin/python -m pip install --upgrade pip '.[test]' '.[lint]'

But my GitHub CI fails with the following error:

hello/__init__.py: error: Duplicate module named "hello" (also at "./build/lib/hello/__init__.py")
hello/__init__.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
hello/__init__.py: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
Found 1 error in 1 file (errors prevented further checking)
Error: Process completed with exit code 2.

To Reproduce

Create a project as shown above, and run the pip install command.

Expected Behavior

I found a pip ticket for this exact problem where pytest was confused by the presence of a build directory. Apparently, in-place builds were introduced in pip 20.1, and is now the default.

One of the suggestions in the ticket was to ignore the build directory. pytest seems to have caught up since it no longer fails with the error reported in the ticket. Surprisingly, mypy does't exclude the build directory automatically, and needs to be told explicitly.

[tool.mypy]
exclude = [
    "venv",
    "build",
]

The expectation from this ticket is to ignore the directories like venv and build by default without needing to be specified by the user. Better yet, use the .gitignore.

Actual Behavior

Shown above.

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: --strict .
  • Mypy configuration options from mypy.ini (and other config files): Shown above.
  • Python version used: 3.13.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions