Skip to content

Commit 28c6ca7

Browse files
committed
Added clarifying comments
1 parent 6b6ce1c commit 28c6ca7

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 sys
2425
import time
@@ -780,19 +781,32 @@ def is_file(path: str) -> bool:
780781
return os.path.isfile(path)
781782

782783

783-
class ModuleType:
784-
PACKAGE = 1
785-
MODULE = 2
786-
NAMESPACE = 3
784+
class ModuleType(enum.Enum):
785+
package = 'package'
786+
module = 'module'
787+
namespace = 'namespace'
787788

788789

789790
class ImportContext:
791+
"""
792+
Describes module import context
793+
794+
Do we already discovered implementation?
795+
What kind of module we discovered?
796+
"""
790797
def __init__(self) -> None:
791798
self.has_py = False # type: bool
792-
self.type = None # type: Optional[int]
799+
self.type = None # type: Optional[ModuleType]
800+
# Paths can contain only one ".py" path, but multiple stubs
793801
self.paths = [] # type: List[str]
794802

795-
def maybe_add_path(self, path: str, type: int) -> None:
803+
def maybe_add_path(self, path: str, type: ModuleType) -> None:
804+
"""
805+
Add path to import context.
806+
Modifies self.paths in case if arguments satisfy import context state
807+
"""
808+
assert path.endswith((os.path.sep,) + tuple(PYTHON_EXTENSIONS))
809+
796810
if self.type is not None and self.type != type:
797811
return None
798812

@@ -802,7 +816,7 @@ def maybe_add_path(self, path: str, type: int) -> None:
802816
if self.has_py and py_path:
803817
return None
804818

805-
if type == ModuleType.NAMESPACE:
819+
if type == ModuleType.namespace:
806820
ok = self._verify_namespace(path)
807821
else:
808822
ok = self._verify_module(path)
@@ -817,6 +831,8 @@ def maybe_add_path(self, path: str, type: int) -> None:
817831
self.paths.append(path)
818832

819833
def _verify_module(self, path: str) -> bool:
834+
# At this point we already know that that it's valid python path
835+
# We only need to check file existence
820836
if not is_file(path):
821837
return False
822838

@@ -860,6 +876,10 @@ def find_module(self, id: str) -> Optional[str]:
860876
return None
861877

862878
def find_modules_recursive(self, module: str) -> List[BuildSource]:
879+
"""
880+
Discover module and all it's children
881+
Remove duplicates from discovered paths
882+
"""
863883
hits = set() # type: Set[str]
864884
result = [] # type: List[BuildSource]
865885
for src in self._find_modules_recursive(module):
@@ -903,6 +923,9 @@ def _collect_paths(self, paths: List[str], last_comp: str) -> List[str]:
903923
sepinit = '__init__'
904924
ctx = ImportContext()
905925

926+
# Detect modules in following order: package, module, namespace.
927+
# First hit determines module type, consistency of paths to given type
928+
# ensured in ImportContext
906929
for path_item in paths:
907930
if is_module_path(path_item):
908931
continue
@@ -912,15 +935,15 @@ def _collect_paths(self, paths: List[str], last_comp: str) -> List[str]:
912935

913936
for ext in PYTHON_EXTENSIONS:
914937
path = os.path.join(path_item, last_comp, sepinit + ext)
915-
ctx.maybe_add_path(path, ModuleType.PACKAGE)
938+
ctx.maybe_add_path(path, ModuleType.package)
916939

917940
for ext in PYTHON_EXTENSIONS:
918941
path = os.path.join(path_item, last_comp + ext)
919-
ctx.maybe_add_path(path, ModuleType.MODULE)
942+
ctx.maybe_add_path(path, ModuleType.module)
920943

921944
if self.namespaces_allowed:
922945
path = os.path.join(path_item, last_comp)
923-
ctx.maybe_add_path(path + os.sep, ModuleType.NAMESPACE)
946+
ctx.maybe_add_path(path + os.sep, ModuleType.namespace)
924947

925948
return ctx.paths
926949

0 commit comments

Comments
 (0)