Skip to content

MYPYPATH directory used even if no __init__.py #19129

Open
@brandonchinn178

Description

@brandonchinn178

Bug Report

According to the docs, when importing foo.bar, mypy will look through each directory in MYPYPATH and find a match. A directory is matched if it contains a __init__.py(i) file. However, I'm seeing behavior where an empty directory in MYPYPATH causes typechecking to fail.

To Reproduce

echo 'from foo.types import Foo' > main.py
mkdir -p foo/types/
echo 'Foo = 123' > foo/types/__init__.py
mkdir -p include/foo/types/

python3.13 -m venv venv
venv/bin/pip install mypy

# passes
rm -rf .mypy_cache && venv/bin/mypy main.py

# fails
rm -rf .mypy_cache && MYPYPATH=include venv/bin/mypy main.py

Expected Behavior

If <dir> does not contain any __init__.py files recursively, then adding MYPYPATH=<dir> should not make any difference

Actual Behavior

$ rm -rf .mypy_cache && venv/bin/mypy main.py
Success: no issues found in 1 source file

$ rm -rf .mypy_cache && MYPYPATH=include venv/bin/mypy main.py
main.py:1: error: Module "foo.types" has no attribute "Foo"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

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

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