Run without module detection logic
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
Allow mypy --no-module-detection (or similar flag) to run without mypy attempting to map files as modules. For example, linting standalone scripts should not require module detection magic.
Pitch
Mypy has (somewhat confusing?) file-to-module mapping logic explained in https://mypy.readthedocs.io/en/latest/running_mypy.html#mapping-file-paths-to-modules. The key take away is that mypy attempts to determine a module name for each imported file (in various ways). For example, a/b/c.py -> a.b.c.
However, the documentation doesn't give rationale for why this actually needs to be always done. If you merely want to check standalone files like mypy one.py then why does this require determining module name for the file?
(AFAIK, other linters like pylint don't have this kind of magic, perhaps because they don't follow imports like mypy does.)
If we have two files mypy one.py two.py and two imports one, then the detection logic is needed, so that module one is found (and it's fine).
However, if I have sub-directories with similarly named files, doing mypy one/file.py two/file.py throws:
two/file.py: error: Duplicate module named 'file' (also at 'one/file.py')
But these are two standalone files, in separate directories, not part of the same package. Why is the error there?
Similar trouble is faced when maintaining a monorepo with multiple packages:
package1/
setup.py
...
package2/
setup.py
...
Mypy will throw error about duplicate module name on setup.py files (and others).
Now, without arguing on how mypy should determine module name of the files is looks up (surely there's reasoning for that), I'm suggesting to add an option to completely skip this magic. In my case, I always pip install -e . the packages that I'm developing, so whatever imports the code (wherever), those imports are already resolvable. All the packages are already importable and whatever files reside out of the packages, are stand-alone scripts and never imported.
One downside is that something like this would no longer work:
package1/
src/
...
setup.py
tasks.py # imports util.py
util.py
For that I don't have suggestion :( Could it be solved by multiple entries in MYPYPATH or PYTHONPATH?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with mypy's file-to-module mapping behavior described in the issue and reproduce the duplicate-module examples using the shown commands. Define the option's behavior for standalone files, imported files, and multiple package roots; done means the proposed flag avoids unwanted duplicate-module errors without breaking required import resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100