Skip to content

Commit e31490e

Browse files
committed
typehint completion_engine.py
* add type hints * rewrite identifies() to return only a bool, not the empty string * prefix some unused variable names with underscore * bugfix: check that token is a Token before invoking is_keyword() on it
1 parent 26dfd6d commit e31490e

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

mycli/packages/completion_engine.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# type: ignore
1+
from __future__ import annotations
2+
3+
from typing import Any
24

35
import sqlparse
4-
from sqlparse.sql import Comparison, Identifier, Where
6+
from sqlparse.sql import Comparison, Identifier, Token, Where
57

68
from mycli.packages.parseutils import extract_tables, find_prev_keyword, last_word
7-
from mycli.packages.special import parse_special_command
9+
from mycli.packages.special.main import parse_special_command
810

911

10-
def suggest_type(full_text, text_before_cursor):
12+
def suggest_type(full_text: str, text_before_cursor: str) -> list[dict[str, str]]:
1113
"""Takes the full_text that is typed so far and also the text before the
1214
cursor to suggest completion type and scope.
1315
@@ -17,7 +19,7 @@ def suggest_type(full_text, text_before_cursor):
1719

1820
word_before_cursor = last_word(text_before_cursor, include="many_punctuations")
1921

20-
identifier = None
22+
identifier: Identifier | None = None
2123

2224
# here should be removed once sqlparse has been fixed
2325
try:
@@ -80,9 +82,9 @@ def suggest_type(full_text, text_before_cursor):
8082
return suggest_based_on_last_token(last_token, text_before_cursor, full_text, identifier)
8183

8284

83-
def suggest_special(text):
85+
def suggest_special(text: str) -> list[dict[str, Any]]:
8486
text = text.lstrip()
85-
cmd, _, arg = parse_special_command(text)
87+
cmd, _separator, _arg = parse_special_command(text)
8688

8789
if cmd == text:
8890
# Trying to complete the special command itself
@@ -109,7 +111,12 @@ def suggest_special(text):
109111
return [{"type": "keyword"}, {"type": "special"}]
110112

111113

112-
def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier):
114+
def suggest_based_on_last_token(
115+
token: str | Token | None,
116+
text_before_cursor: str,
117+
full_text: str,
118+
identifier: Identifier,
119+
) -> list[dict[str, Any]]:
113120
if isinstance(token, str):
114121
token_v = token.lower()
115122
elif isinstance(token, Comparison):
@@ -157,7 +164,7 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier
157164

158165
# Check for a subquery expression (cases 3 & 4)
159166
where = p.tokens[-1]
160-
idx, prev_tok = where.token_prev(len(where.tokens) - 1)
167+
_idx, prev_tok = where.token_prev(len(where.tokens) - 1)
161168

162169
if isinstance(prev_tok, Comparison):
163170
# e.g. "SELECT foo FROM bar WHERE foo = ANY("
@@ -223,7 +230,7 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier
223230
{"type": "alias", "aliases": aliases},
224231
{"type": "keyword"},
225232
]
226-
elif (token_v.endswith("join") and token.is_keyword) or (
233+
elif (token_v.endswith("join") and isinstance(token, Token) and token.is_keyword) or (
227234
token_v in ("copy", "from", "update", "into", "describe", "truncate", "desc", "explain")
228235
):
229236
schema = (identifier and identifier.get_parent_name()) or []
@@ -292,5 +299,16 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier
292299
return [{"type": "keyword"}]
293300

294301

295-
def identifies(identifier, schema, table, alias):
296-
return identifier == alias or identifier == table or (schema and (identifier == schema + "." + table))
302+
def identifies(
303+
identifier: Any,
304+
schema: str | None,
305+
table: str,
306+
alias: str,
307+
) -> bool:
308+
if identifier == alias:
309+
return True
310+
if identifier == table:
311+
return True
312+
if schema and identifier == (schema + "." + table):
313+
return True
314+
return False

0 commit comments

Comments
 (0)