Skip to content

Commit

Permalink
Handle files ending with __init__ better (#13314)
Browse files Browse the repository at this point in the history
Fixes #10329
  • Loading branch information
hauntsaninja authored Aug 4, 2022
1 parent 38b2444 commit ff1b092
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,7 @@ def _can_find_module_in_parent_dir(self, id: str) -> bool:
self.options,
stdlib_py_versions=self.stdlib_py_versions,
)
while any(
file.endswith(("__init__.py", "__init__.pyi")) for file in os.listdir(working_dir)
):
while any(is_init_file(file) for file in os.listdir(working_dir)):
working_dir = os.path.dirname(working_dir)
parent_search.search_paths = SearchPaths((working_dir,), (), (), ())
if not isinstance(parent_search._find_module(id, False), ModuleNotFoundReason):
Expand Down Expand Up @@ -585,7 +583,7 @@ def find_modules_recursive(self, module: str) -> List[BuildSource]:
sources = [BuildSource(module_path, module, None)]

package_path = None
if module_path.endswith(("__init__.py", "__init__.pyi")):
if is_init_file(module_path):
package_path = os.path.dirname(module_path)
elif self.fscache.isdir(module_path):
package_path = module_path
Expand Down Expand Up @@ -648,9 +646,13 @@ def matches_exclude(
return False


def is_init_file(path: str) -> bool:
return os.path.basename(path) in ("__init__.py", "__init__.pyi")


def verify_module(fscache: FileSystemCache, id: str, path: str, prefix: str) -> bool:
"""Check that all packages containing id have a __init__ file."""
if path.endswith(("__init__.py", "__init__.pyi")):
if is_init_file(path):
path = os.path.dirname(path)
for i in range(id.count(".")):
path = os.path.dirname(path)
Expand All @@ -664,7 +666,7 @@ def verify_module(fscache: FileSystemCache, id: str, path: str, prefix: str) ->

def highest_init_level(fscache: FileSystemCache, id: str, path: str, prefix: str) -> int:
"""Compute the highest level where an __init__ file is found."""
if path.endswith(("__init__.py", "__init__.pyi")):
if is_init_file(path):
path = os.path.dirname(path)
level = 0
for i in range(id.count(".")):
Expand Down

0 comments on commit ff1b092

Please sign in to comment.