Skip to content

Fix case mismatching modules during namespace package search #11261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions mypy/fscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,14 @@ def isfile_case(self, path: str, prefix: str) -> bool:
res = False
if res:
# Also recursively check the other path components in case sensitive way.
res = self._exists_case(head, prefix)
res = self.exists_case(head, prefix)
self.isfile_case_cache[path] = res
return res

def _exists_case(self, path: str, prefix: str) -> bool:
"""Helper to check path components in case sensitive fashion, up to prefix."""
def exists_case(self, path: str, prefix: str) -> bool:
"""Return whether path exists - checking path components in case sensitive
fashion, up to prefix.
"""
if path in self.exists_case_cache:
return self.exists_case_cache[path]
head, tail = os.path.split(path)
Expand All @@ -242,7 +244,7 @@ def _exists_case(self, path: str, prefix: str) -> bool:
res = False
if res:
# Also recursively check other path components.
res = self._exists_case(head, prefix)
res = self.exists_case(head, prefix)
self.exists_case_cache[path] = res
return res

Expand Down
2 changes: 1 addition & 1 deletion mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:

# In namespace mode, register a potential namespace package
if self.options and self.options.namespace_packages:
if fscache.isdir(base_path) and not has_init:
if fscache.exists_case(base_path, dir_prefix) and not has_init:
near_misses.append((base_path, dir_prefix))

# No package, look for module.
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-modules-case.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
-- Type checker test cases dealing with modules and imports on case-insensitive filesystems.

[case testCaseSensitivityDir]
# flags: --no-namespace-packages
from a import B # E: Module "a" has no attribute "B"

[file a/__init__.py]
[file a/b/__init__.py]

[case testCaseSensitivityDirNamespacePackages]
# flags: --namespace-packages
from a import B # E: Module "a" has no attribute "B"

[file a/__init__.py]
Expand Down