python / python/mypy

MYPYPATH directory used even if no __init__.py

Open
#19,129 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with main.py, foo/types/init.py, and the empty include/foo/types/ directory using the two mypy commands shown. Trace MYPYPATH module resolution and add a regression test for this layout; done means adding the empty path does not change the successful type-checking result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.