Commit e965275
authored
Don't read scripts without extensions as modules in namespace mode (#14335)
The `FindModuleCache` currently matches files without an extension when
`--namespace_packages` is enabled while [the
docs](https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules)
don't mention that this should be the case. The "near-miss" logic
collects candidates for modules, which could correctly include a
_directory_ `foo/bar/baz` when looking for `foo/bar/baz`. However, the
current logic also picks up a _file_ `foo/bar/baz`. This means that, if
both a file `foo/bar/baz` and `foo/bar/baz.py` exist, the first one is
actually picked, resulting in unexpected behaviour.
The condition that checks `exists_case` on `foo/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:
* add `test-data/packages/modulefinder/pkg1/a` to verify that
`ModuleFinderSuite.test__no_namespace_packages__find_a_in_pkg1` is
indeed working correctly even without the patch because it's not running
in namespace mode.
* add `test-data/packages/modulefinder/nsx-pkg3/nsx/c/c`, making
`ModuleFinderSuite.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 both
`foo/bar/baz` and `foo/bar/baz.py` in the same directory, incorrectly
picking up the wrapper-script instead of the module. Dependencies on a
`py_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.1 parent 2475643 commit e965275
File tree
3 files changed
+5
-1
lines changed- mypy
- test-data/packages/modulefinder
- nsx-pkg3/nsx/c
- pkg1
3 files changed
+5
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
507 | 507 | | |
508 | 508 | | |
509 | 509 | | |
510 | | - | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
511 | 515 | | |
512 | 516 | | |
513 | 517 | | |
| |||
Whitespace-only changes.
Whitespace-only changes.
0 commit comments