Don't read scripts without extensions as modules in namespace mode #14335
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.
The
FindModuleCache
currently matches files without an extension when--namespace_packages
is enabled while the docs don't mention that this should be the case. The "near-miss" logic collects candidates for modules, which could correctly include a directoryfoo/bar/baz
when looking forfoo/bar/baz
. However, the current logic also picks up a filefoo/bar/baz
. This means that, if both a filefoo/bar/baz
andfoo/bar/baz.py
exist, the first one is actually picked, resulting in unexpected behaviour.The condition that checks
exists_case
onfoo/bar/baz
should also check that it is indeed a directory by checking that it is not a file. I'm open to different fixes of course, but this seemed like the most obvious and least impactful change to make.This PR modifies 2 tests:
test-data/packages/modulefinder/pkg1/a
to verify thatModuleFinderSuite.test__no_namespace_packages__find_a_in_pkg1
is indeed working correctly even without the patch because it's not running in namespace mode.test-data/packages/modulefinder/nsx-pkg3/nsx/c/c
, makingModuleFinderSuite.test__find_nsx_c_c_in_pkg3
fail, which the patch fixes.To give one real-world example of this scenario: Bazel's Python rules construct a wrapper-script with the same name as the main Python-file without the extension for a
py_binary
-target. If some other Python rule depends on this//foo/bar:baz
py_binary
-target, it sees bothfoo/bar/baz
andfoo/bar/baz.py
in the same directory, incorrectly picking up the wrapper-script instead of the module. Dependencies on apy_binary
might be a bit of an edge-case, but Python execution of these targets does pick up the right file, so Mypy should probably as well.