Skip to content

MYPYPATH directory used even if no __init__.py #19129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
brandonchinn178 opened this issue May 22, 2025 · 4 comments
Open

MYPYPATH directory used even if no __init__.py #19129

brandonchinn178 opened this issue May 22, 2025 · 4 comments
Labels
bug mypy got something wrong

Comments

@brandonchinn178
Copy link

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:
@brandonchinn178 brandonchinn178 added the bug mypy got something wrong label May 22, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented May 22, 2025

Is this because namespace packages?

@brandonchinn178
Copy link
Author

Yeah, possibly. In our actual codebase, we use --explicit-package-bases (we're using Bazel with Python, so we don't have explicit __init__.py files often).

From the docs:

The rules for searching for a module foo are as follows:

  • The search looks in each of the directories in the search path (see above) until a match is found.
  • If a package named foo is found (i.e. a directory foo containing an init.py or init.pyi file) that’s a match.
  • If a stub file named foo.pyi is found, that’s a match.
  • If a Python module named foo.py is found, that’s a match.

I don't see any mention of how namespace packages affect this; if they do, are these docs incomplete? It looks like namespace packages are only discussed in the section about specifying files on the command line.

@A5rocks
Copy link
Collaborator

A5rocks commented May 22, 2025

I assume namespace package support was added after the documentation was added. If this issue is indeed because of namespace package support and also this is intended (I'm not sure, I haven't got much experience with it) then this should just be a documentation update.

@brandonchinn178
Copy link
Author

You might be right; it looks like there's namespace logic in the module finder logic:

# In namespace mode, re-check those entries that had 'verify'.

Possibly just a docs update then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants