Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff1b092

Browse files
authored
Handle files ending with __init__ better (python#13314)
Fixes python#10329
1 parent 38b2444 commit ff1b092

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

mypy/modulefinder.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,7 @@ def _can_find_module_in_parent_dir(self, id: str) -> bool:
369369
self.options,
370370
stdlib_py_versions=self.stdlib_py_versions,
371371
)
372-
while any(
373-
file.endswith(("__init__.py", "__init__.pyi")) for file in os.listdir(working_dir)
374-
):
372+
while any(is_init_file(file) for file in os.listdir(working_dir)):
375373
working_dir = os.path.dirname(working_dir)
376374
parent_search.search_paths = SearchPaths((working_dir,), (), (), ())
377375
if not isinstance(parent_search._find_module(id, False), ModuleNotFoundReason):
@@ -585,7 +583,7 @@ def find_modules_recursive(self, module: str) -> List[BuildSource]:
585583
sources = [BuildSource(module_path, module, None)]
586584

587585
package_path = None
588-
if module_path.endswith(("__init__.py", "__init__.pyi")):
586+
if is_init_file(module_path):
589587
package_path = os.path.dirname(module_path)
590588
elif self.fscache.isdir(module_path):
591589
package_path = module_path
@@ -648,9 +646,13 @@ def matches_exclude(
648646
return False
649647

650648

649+
def is_init_file(path: str) -> bool:
650+
return os.path.basename(path) in ("__init__.py", "__init__.pyi")
651+
652+
651653
def verify_module(fscache: FileSystemCache, id: str, path: str, prefix: str) -> bool:
652654
"""Check that all packages containing id have a __init__ file."""
653-
if path.endswith(("__init__.py", "__init__.pyi")):
655+
if is_init_file(path):
654656
path = os.path.dirname(path)
655657
for i in range(id.count(".")):
656658
path = os.path.dirname(path)
@@ -664,7 +666,7 @@ def verify_module(fscache: FileSystemCache, id: str, path: str, prefix: str) ->
664666

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

0 commit comments

Comments
 (0)