Skip to content

Commit 5c77c2d

Browse files
committed
Added clarifying comments
1 parent 0737912 commit 5c77c2d

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

mypy/build.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import json
2020
import os.path
2121
import re
22+
import enum
2223
import site
2324
import stat
2425
import sys
@@ -799,19 +800,32 @@ def is_file(path: str) -> bool:
799800
return res
800801

801802

802-
class ModuleType:
803-
PACKAGE = 1
804-
MODULE = 2
805-
NAMESPACE = 3
803+
class ModuleType(enum.Enum):
804+
package = 'package'
805+
module = 'module'
806+
namespace = 'namespace'
806807

807808

808809
class ImportContext:
810+
"""
811+
Describes module import context
812+
813+
Do we already discovered implementation?
814+
What kind of module we discovered?
815+
"""
809816
def __init__(self) -> None:
810817
self.has_py = False # type: bool
811-
self.type = None # type: Optional[int]
818+
self.type = None # type: Optional[ModuleType]
819+
# Paths can contain only one ".py" path, but multiple stubs
812820
self.paths = [] # type: List[str]
813821

814-
def maybe_add_path(self, path: str, type: int) -> None:
822+
def maybe_add_path(self, path: str, type: ModuleType) -> None:
823+
"""
824+
Add path to import context.
825+
Modifies self.paths in case if arguments satisfy import context state
826+
"""
827+
assert path.endswith((os.path.sep,) + tuple(PYTHON_EXTENSIONS))
828+
815829
if self.type is not None and self.type != type:
816830
return None
817831

@@ -821,7 +835,7 @@ def maybe_add_path(self, path: str, type: int) -> None:
821835
if self.has_py and py_path:
822836
return None
823837

824-
if type == ModuleType.NAMESPACE:
838+
if type == ModuleType.namespace:
825839
ok = self._verify_namespace(path)
826840
else:
827841
ok = self._verify_module(path)
@@ -836,6 +850,8 @@ def maybe_add_path(self, path: str, type: int) -> None:
836850
self.paths.append(path)
837851

838852
def _verify_module(self, path: str) -> bool:
853+
# At this point we already know that that it's valid python path
854+
# We only need to check file existence
839855
if not is_file(path):
840856
return False
841857

@@ -879,6 +895,10 @@ def find_module(self, id: str) -> Optional[str]:
879895
return None
880896

881897
def find_modules_recursive(self, module: str) -> List[BuildSource]:
898+
"""
899+
Discover module and all it's children
900+
Remove duplicates from discovered paths
901+
"""
882902
hits = set() # type: Set[str]
883903
result = [] # type: List[BuildSource]
884904
for src in self._find_modules_recursive(module):
@@ -922,6 +942,9 @@ def _collect_paths(self, paths: List[str], last_comp: str) -> List[str]:
922942
sepinit = '__init__'
923943
ctx = ImportContext()
924944

945+
# Detect modules in following order: package, module, namespace.
946+
# First hit determines module type, consistency of paths to given type
947+
# ensured in ImportContext
925948
for path_item in paths:
926949
if is_module_path(path_item):
927950
continue
@@ -931,15 +954,15 @@ def _collect_paths(self, paths: List[str], last_comp: str) -> List[str]:
931954

932955
for ext in PYTHON_EXTENSIONS:
933956
path = os.path.join(path_item, last_comp, sepinit + ext)
934-
ctx.maybe_add_path(path, ModuleType.PACKAGE)
957+
ctx.maybe_add_path(path, ModuleType.package)
935958

936959
for ext in PYTHON_EXTENSIONS:
937960
path = os.path.join(path_item, last_comp + ext)
938-
ctx.maybe_add_path(path, ModuleType.MODULE)
961+
ctx.maybe_add_path(path, ModuleType.module)
939962

940963
if self.namespaces_allowed:
941964
path = os.path.join(path_item, last_comp)
942-
ctx.maybe_add_path(path + os.sep, ModuleType.NAMESPACE)
965+
ctx.maybe_add_path(path + os.sep, ModuleType.namespace)
943966

944967
return ctx.paths
945968

0 commit comments

Comments
 (0)