2626import sys
2727import warnings
2828from pathlib import Path
29+ from typing import Optional , Set , Generator , Tuple , List
2930
3031from .line import (
3132 current_word ,
4849
4950
5051class ModuleGatherer :
51- def __init__ (self , path = None , skiplist = None ):
52+ def __init__ (self , path : Optional [ Path ] = None , skiplist = None ) -> None :
5253 # The cached list of all known modules
53- self .modules = set ()
54+ self .modules : Set [ str ] = set ()
5455 # List of (st_dev, st_ino) to compare against so that paths are not repeated
55- self .paths = set ()
56+ self .paths : Set [ Tuple [ int , int ]] = set ()
5657 # Patterns to skip
5758 self .skiplist = skiplist if skiplist is not None else tuple ()
5859 self .fully_loaded = False
5960 self .find_iterator = self .find_all_modules (path )
6061
61- def module_matches (self , cw , prefix = "" ):
62+ def module_matches (self , cw : str , prefix : str = "" ) -> Set [ str ] :
6263 """Modules names to replace cw with"""
6364
6465 full = f"{ prefix } .{ cw } " if prefix else cw
@@ -72,7 +73,9 @@ def module_matches(self, cw, prefix=""):
7273 else :
7374 return set (matches )
7475
75- def attr_matches (self , cw , prefix = "" , only_modules = False ):
76+ def attr_matches (
77+ self , cw : str , prefix : str = "" , only_modules : bool = False
78+ ) -> Set [str ]:
7679 """Attributes to replace name with"""
7780 full = f"{ prefix } .{ cw } " if prefix else cw
7881 module_name , _ , name_after_dot = full .rpartition ("." )
@@ -96,11 +99,11 @@ def attr_matches(self, cw, prefix="", only_modules=False):
9699
97100 return matches
98101
99- def module_attr_matches (self , name ) :
102+ def module_attr_matches (self , name : str ) -> Set [ str ] :
100103 """Only attributes which are modules to replace name with"""
101104 return self .attr_matches (name , prefix = "" , only_modules = True )
102105
103- def complete (self , cursor_offset , line ) :
106+ def complete (self , cursor_offset : int , line : str ) -> Optional [ Set [ str ]] :
104107 """Construct a full list of possibly completions for imports."""
105108 tokens = line .split ()
106109 if "from" not in tokens and "import" not in tokens :
@@ -136,7 +139,7 @@ def complete(self, cursor_offset, line):
136139 else :
137140 return None
138141
139- def find_modules (self , path ) :
142+ def find_modules (self , path : Path ) -> Generator [ str , None , None ] :
140143 """Find all modules (and packages) for a given directory."""
141144 if not path .is_dir ():
142145 # Perhaps a zip file
@@ -154,7 +157,7 @@ def find_modules(self, path):
154157 # Path is not readable
155158 return
156159
157- finder = importlib .machinery .FileFinder (str (path ), * LOADERS )
160+ finder = importlib .machinery .FileFinder (str (path ), * LOADERS ) # type: ignore
158161 for p in children :
159162 if any (fnmatch .fnmatch (p .name , entry ) for entry in self .skiplist ):
160163 # Path is on skiplist
0 commit comments