Duplicate module error with stub files
Bug Report
When a project contains an untyped source (.py) file and a corresponding stub (.pyi) file, Mypy may emit "Duplicate module" errors depending on how the files were passed via the command line.
Consider a simple package foo with this project layout:
foo
├── __init__.py
└── __init__.pyi
Mypy runs without error if I pass it the foo directory as argument
$ mypy foo
Success: no issues found in 1 source file
But if I pass the directory contents as separate arguments, Mypy emits an error due to the two files having the same module name
$ mypy foo/*
foo/__init__.pyi: error: Duplicate module named "foo" (also at "foo/__init__.py")
foo/__init__.pyi: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
foo/__init__.pyi: 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)
Is this the intended behavior?
To Reproduce
(see above)
Expected Behavior
I expected that the .pyi file would supersede the .py file in both cases above (regardless of how the files were passed from the command line), or perhaps that Mypy would process the files independently in the second case and ignore naming collisions.
In my actual use case, I am running Mypy via a pre-commit hook, which always passes individual files as separate arguments. The addition of a stub file caused Mypy to raise the "Duplicate module" error. I was able to work around the error by configuring pre-commit to exclude the original .py file, but this seems like a pretty typical use case that Mypy might want to support.
https://github.com/isce-framework/snaphu-py/pull/68
Actual Behavior
(see above)
Your Environment
- Mypy version used: 1.10.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.3
Hello, I'm having the same issue here with my pre-commit hook. @gmgunter how did you manage to exclude your file ? It seems like my pre-commit hook is not picking up the args i'm giving.
Using command line works fine though.
mypy v1.11.2 Python 3.10.11 Mypy command-line flags: none Mypy configuration options from mypy.ini (and other config files): none
If you're using pre-commit with mypy, see the following advice: https://github.com/python/mypy/issues/13916
Use pass_filenames: false in your pre-commit config, as per https://github.com/python/mypy/issues/13916
This behavior of treating directory input differently from passing the files individually is highly unexpected, and breaks expectations in causing the issue in https://github.com/bazel-contrib/rules_mypy/issues/125 .