Skip to content

Commit d62f0f5

Browse files
committed
Add types for filepaths and completion_engine.
1 parent bfbe8d4 commit d62f0f5

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

litecli/packages/completion_engine.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
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+
47
import sqlparse
5-
from sqlparse.sql import Comparison, Identifier, Where
8+
from sqlparse.sql import Comparison, Identifier, Where, Token
69
from .parseutils import last_word, extract_tables, find_prev_keyword
710
from .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))

litecli/packages/filepaths.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
# -*- coding: utf-8
22
# mypy: ignore-errors
33

4-
from __future__ import unicode_literals
4+
from __future__ import annotations
5+
6+
from typing import List, Tuple
57

68
import os
79

810

9-
def list_path(root_dir):
11+
def list_path(root_dir: str) -> List[str]:
1012
"""List directory if exists.
1113
1214
:param dir: str
1315
:return: list
1416
1517
"""
16-
res = []
18+
res: List[str] = []
1719
if os.path.isdir(root_dir):
1820
for name in os.listdir(root_dir):
1921
res.append(name)
2022
return res
2123

2224

23-
def complete_path(curr_dir, last_dir):
25+
def complete_path(curr_dir: str, last_dir: str) -> str | None:
2426
"""Return the path to complete that matches the last entered component.
2527
2628
If the last entered component is ~, expanded path would not
@@ -35,9 +37,10 @@ def complete_path(curr_dir, last_dir):
3537
return curr_dir
3638
elif last_dir == "~":
3739
return os.path.join(last_dir, curr_dir)
40+
return None
3841

3942

40-
def parse_path(root_dir):
43+
def parse_path(root_dir: str) -> Tuple[str, str, int]:
4144
"""Split path into head and last component for the completer.
4245
4346
Also return position where last component starts.
@@ -53,7 +56,7 @@ def parse_path(root_dir):
5356
return base_dir, last_dir, position
5457

5558

56-
def suggest_path(root_dir):
59+
def suggest_path(root_dir: str) -> List[str]:
5760
"""List all files and subdirectories in a directory.
5861
5962
If the directory is not specified, suggest root directory,
@@ -64,7 +67,7 @@ def suggest_path(root_dir):
6467
6568
"""
6669
if not root_dir:
67-
return map(str, [os.path.abspath(os.sep), "~", os.curdir, os.pardir])
70+
return [str(x) for x in [os.path.abspath(os.sep), "~", os.curdir, os.pardir]]
6871

6972
if "~" in root_dir:
7073
root_dir = str(os.path.expanduser(root_dir))
@@ -75,7 +78,7 @@ def suggest_path(root_dir):
7578
return list_path(root_dir)
7679

7780

78-
def dir_path_exists(path):
81+
def dir_path_exists(path: str) -> bool:
7982
"""Check if the directory path exists for a given file.
8083
8184
For example, for a file /home/user/.cache/litecli/log, check if

0 commit comments

Comments
 (0)