11# mypy: ignore-errors
22
3- from __future__ import print_function
3+ from __future__ import annotations
4+
5+ from typing import Any , Dict , List , Optional
6+
47import sqlparse
5- from sqlparse .sql import Comparison , Identifier , Where
8+ from sqlparse .sql import Comparison , Identifier , Where , Token
69from .parseutils import last_word , extract_tables , find_prev_keyword
710from .special import parse_special_command
811
912
10- def suggest_type (full_text , text_before_cursor ) :
13+ def suggest_type (full_text : str , text_before_cursor : str ) -> List [ Dict [ str , Any ]] :
1114 """Takes the full_text that is typed so far and also the text before the
1215 cursor to suggest completion type and scope.
1316
@@ -17,7 +20,7 @@ def suggest_type(full_text, text_before_cursor):
1720
1821 word_before_cursor = last_word (text_before_cursor , include = "many_punctuations" )
1922
20- identifier = None
23+ identifier : Optional [ Identifier ] = None
2124
2225 # here should be removed once sqlparse has been fixed
2326 try :
@@ -86,7 +89,7 @@ def suggest_type(full_text, text_before_cursor):
8689 return suggest_based_on_last_token (last_token , text_before_cursor , full_text , identifier )
8790
8891
89- def suggest_special (text ) :
92+ def suggest_special (text : str ) -> List [ Dict [ str , Any ]] :
9093 text = text .lstrip ()
9194 cmd , _ , arg = parse_special_command (text )
9295
@@ -126,7 +129,7 @@ def suggest_special(text):
126129 return [{"type" : "keyword" }, {"type" : "special" }]
127130
128131
129- def _expecting_arg_idx (arg , text ) :
132+ def _expecting_arg_idx (arg : str , text : str ) -> int :
130133 """Return the index of expecting argument.
131134
132135 >>> _expecting_arg_idx("./da", ".import ./da")
@@ -142,7 +145,12 @@ def _expecting_arg_idx(arg, text):
142145 return len (args ) + int (text [- 1 ].isspace ())
143146
144147
145- def suggest_based_on_last_token (token , text_before_cursor , full_text , identifier ):
148+ def suggest_based_on_last_token (
149+ token : Optional [str | Token ],
150+ text_before_cursor : str ,
151+ full_text : str ,
152+ identifier : Optional [Identifier ],
153+ ) -> List [Dict [str , Any ]]:
146154 if isinstance (token , str ):
147155 token_v = token .lower ()
148156 elif isinstance (token , Comparison ):
@@ -163,8 +171,8 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier
163171 else :
164172 token_v = token .value .lower ()
165173
166- def is_operand (x ) :
167- return x and any ([x .endswith (op ) for op in ["+" , "-" , "*" , "/" ]])
174+ def is_operand (x : Optional [ str ]) -> bool :
175+ return bool ( x ) and any ([x .endswith (op ) for op in ["+" , "-" , "*" , "/" ]])
168176
169177 if not token :
170178 return [{"type" : "keyword" }, {"type" : "special" }]
@@ -253,7 +261,7 @@ def is_operand(x):
253261 {"type" : "alias" , "aliases" : aliases },
254262 {"type" : "keyword" },
255263 ]
256- elif (token_v .endswith ("join" ) and token .is_keyword ) or (
264+ elif (token_v .endswith ("join" ) and isinstance ( token , Token ) and token .is_keyword ) or (
257265 token_v in ("copy" , "from" , "update" , "into" , "describe" , "truncate" , "desc" , "explain" )
258266 ):
259267 schema = (identifier and identifier .get_parent_name ()) or []
@@ -322,5 +330,5 @@ def is_operand(x):
322330 return [{"type" : "keyword" }]
323331
324332
325- def identifies (id , schema , table , alias ) :
333+ def identifies (id : Any , schema : Optional [ str ] , table : str , alias : Optional [ str ]) -> bool :
326334 return id == alias or id == table or (schema and (id == schema + "." + table ))
0 commit comments