@@ -787,12 +787,25 @@ class ModuleType:
787
787
788
788
789
789
class ImportContext :
790
+ """
791
+ Describes module import context
792
+
793
+ Do we already discovered implementation?
794
+ What kind of module we discovered?
795
+ """
790
796
def __init__ (self ) -> None :
791
797
self .has_py = False # type: bool
792
798
self .type = None # type: Optional[int]
799
+ # Paths can contain only one ".py" path, but multiple stubs
793
800
self .paths = [] # type: List[str]
794
801
795
802
def maybe_add_path (self , path : str , type : int ) -> None :
803
+ """
804
+ Add path to import context.
805
+ Modifies self.paths in case if arguments satisfy import context state
806
+ """
807
+ assert path .endswith (('/' ,) + tuple (PYTHON_EXTENSIONS ))
808
+
796
809
if self .type is not None and self .type != type :
797
810
return None
798
811
@@ -817,6 +830,8 @@ def maybe_add_path(self, path: str, type: int) -> None:
817
830
self .paths .append (path )
818
831
819
832
def _verify_module (self , path : str ) -> bool :
833
+ # At this point we already know that that it's valid python path
834
+ # We only need to check file existence
820
835
if not is_file (path ):
821
836
return False
822
837
@@ -860,6 +875,10 @@ def find_module(self, id: str) -> Optional[str]:
860
875
return None
861
876
862
877
def find_modules_recursive (self , module : str ) -> List [BuildSource ]:
878
+ """
879
+ Discover module and all it's children
880
+ Remove duplicates from discovered paths
881
+ """
863
882
hits = set () # type: Set[str]
864
883
result = [] # type: List[BuildSource]
865
884
for src in self ._find_modules_recursive (module ):
@@ -903,6 +922,9 @@ def _collect_paths(self, paths: List[str], last_comp: str) -> List[str]:
903
922
sepinit = '__init__'
904
923
ctx = ImportContext ()
905
924
925
+ # Detect modules in following order: package, module, namespace.
926
+ # First hit determines module type, consistency of paths to given type
927
+ # ensured in ImportContext
906
928
for path_item in paths :
907
929
if is_module_path (path_item ):
908
930
continue
0 commit comments