Respect the semantics of EXPLICIT #10780
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR removes the implicitly added current working directory when
--explicit-package-bases
is specifiedand either
MYPYPATH
ormypy_path
(or both) are non-empty. This follows the principle of least surprise,as it respects the reasonable expectation that prescribing explicit package bases will not magically include
implicit bases. On the other hand, when explicit bases are not actually present, it does fall back to the current
practice of including the current working directory.
Rationale:
With the
--explicit-package-bases
option,mypy
has a tendency to erroneously treat foldersin the current working directory that happen to match the name of an imported module in a source
file being checked as "the" implementation of said module, producing errors like the following:
Ideally, any such directory should be possible to
--exclude
, but I've yet to figure out how.The seemingly obvious
--exclude "$PWD/docker"
certainly doesn't work (as evidenced above).[NB#1: I've also tried endless variations of absolute and relative paths with or without a trailing slash.]
[NB#2: Including
--follow-imports=skip
has no effect on the above.]Setting
MYPYPATH
explicitly to include the current working directory is simple,but telling
mypy
not to use the current working directory appears close to impossible.As noted in
docs/source/running_mypy.rst
:In such a situation, the working directory may or may not hold relevant Python modules.
This is not a problem when
__init__.py
markers can be used to decide what is a valid module.The
--explicit-package-bases
option, however, precludes that possibility.The following comment in
mypy/modulefinder.py
appears to display some confusionregarding why and when including the working directory is actually useful:
In my mind, this is a clear warning flag that control should be passed to the user.
Test Plan
Unfortunately, it's unclear how to write good test cases for this change, since the existing tests
for the relevant modules (essentially,
mypy/test/{test_find_sources,testmodulefinder}.py
)appear to pass unaffected, which is not all too surprising, considering that the error reported
as part of this PR depends on the contents of the files and the tests only consider paths.
Here's a short script that recreates a minimal test case:
The results with stock
mypy
are included above. With the changes in this PR, the output becomes:The standard battery of test cases still succeeds, albeit I only tested on Python 3.8,
since I don't have any of the earlier versions installed. Tests were run using: