Skip to content

Commit 455a2a2

Browse files
committed
Make ty pass all the type hints
1 parent 9c6b498 commit 455a2a2

8 files changed

Lines changed: 47 additions & 26 deletions

File tree

litecli/config.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from __future__ import annotations
22

33
import errno
4-
import shutil
54
import os
65
import platform
7-
from os.path import expanduser, exists, dirname
8-
6+
import shutil
7+
from os.path import dirname, exists, expanduser
98

109
from configobj import ConfigObj
1110

@@ -55,7 +54,7 @@ def upgrade_config(config: str, def_config: str) -> None:
5554
def get_config(liteclirc_file: str | None = None) -> ConfigObj:
5655
from litecli import __file__ as package_root
5756

58-
package_root = os.path.dirname(package_root)
57+
package_root = os.path.dirname(str(package_root))
5958

6059
liteclirc_file = liteclirc_file or f"{config_location()}config"
6160

litecli/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from io import open
1414

1515
try:
16-
from sqlean import OperationalError, sqlite_version
16+
from sqlean import OperationalError, sqlite_version # ty: ignore[unresolved-import]
1717
except ImportError:
1818
from sqlite3 import OperationalError, sqlite_version
1919
from time import time
@@ -521,7 +521,8 @@ def one_iteration(text: str | None = None) -> None:
521521
raise e
522522
except KeyboardInterrupt:
523523
try:
524-
sqlexecute.conn.interrupt()
524+
# since connection can sqlite3 or sqlean, it's hard to annotate the type for interrupt. so ignore the type hint warning.
525+
sqlexecute.conn.interrupt() # ty: ignore[possibly-missing-attribute]
525526
except Exception as e:
526527
self.echo(
527528
"Encountered error while cancelling query: {}".format(e),
@@ -754,6 +755,7 @@ def refresh_completions(self, reset: bool = False) -> list[tuple]:
754755
if reset:
755756
with self._completer_lock:
756757
self.completer.reset_completions()
758+
assert self.sqlexecute is not None
757759
self.completion_refresher.refresh(
758760
self.sqlexecute,
759761
self._on_completions_refreshed,

litecli/packages/parseutils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from typing import Generator, Iterable, Literal
55

66
import sqlparse
7-
from sqlparse.sql import IdentifierList, Identifier, Function, Token, TokenList
8-
from sqlparse.tokens import Keyword, DML, Punctuation
7+
from sqlparse.sql import Function, Identifier, IdentifierList, Token, TokenList
8+
from sqlparse.tokens import DML, Keyword, Punctuation
99

1010
cleanup_regex: dict[str, re.Pattern[str]] = {
1111
# This matches only alphanumerics and underscores.
@@ -18,10 +18,10 @@
1818
"all_punctuations": re.compile(r"([^\s]+)$"),
1919
}
2020

21+
LAST_WORD_INCLUDE_TYPE = Literal["alphanum_underscore", "many_punctuations", "most_punctuations", "all_punctuations"]
2122

22-
def last_word(
23-
text: str, include: Literal["alphanum_underscore", "many_punctuations", "most_punctuations", "all_punctuations"] = "alphanum_underscore"
24-
) -> str:
23+
24+
def last_word(text: str, include: LAST_WORD_INCLUDE_TYPE = "alphanum_underscore") -> str:
2525
R"""
2626
Find the last word in a sentence.
2727

litecli/packages/special/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
def export(defn: Callable[..., Any]) -> Callable[..., Any]:
1212
"""Decorator to explicitly mark functions that are exposed in a lib."""
13-
if isinstance(defn, FunctionType):
13+
# ty, requires explict check for callable of tyep | function type to access __name__
14+
if isinstance(defn, (type, FunctionType)):
1415
globals()[defn.__name__] = defn
1516
__all__.append(defn.__name__)
1617
return defn
@@ -19,3 +20,24 @@ def export(defn: Callable[..., Any]) -> Callable[..., Any]:
1920
from . import dbcommands
2021
from . import iocommands
2122
from . import llm
23+
from . import utils
24+
from .main import CommandNotFound, register_special_command, execute
25+
from .iocommands import (
26+
set_favorite_queries,
27+
editor_command,
28+
get_filename,
29+
get_editor_query,
30+
open_external_editor,
31+
is_expanded_output,
32+
set_expanded_output,
33+
write_tee,
34+
unset_once_if_written,
35+
unset_pipe_once_if_written,
36+
disable_pager,
37+
set_pager,
38+
is_pager_enabled,
39+
write_once,
40+
write_pipe_once,
41+
close_tee,
42+
)
43+
from .llm import is_llm_command, handle_llm, FinishIteration

litecli/packages/special/llm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
LLM_TEMPLATE_NAME = "litecli-llm-template"
2626
LLM_CLI_COMMANDS: list[str] = list(cli.commands.keys())
2727
# Mapping of model_id to None used for completion tree leaves.
28-
MODELS: dict[str, None] = {x.model_id: None for x in llm.get_models()}
28+
# the file name is llm.py and module name is llm, hence ty is complaining that get_models is missing.
29+
MODELS: dict[str, None] = {x.model_id: None for x in llm.get_models()} # ty: ignore[unresolved-attribute]
2930

3031

3132
def run_external_cmd(

litecli/sqlcompleter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from __future__ import annotations
22

33
import logging
4-
from re import compile, escape
54
from collections import Counter
5+
from re import compile, escape
66
from typing import Any, Collection, Generator, Iterable, Literal, Sequence
77

88
from prompt_toolkit.completion import CompleteEvent, Completer, Completion
99
from prompt_toolkit.completion.base import Document
1010

1111
from .packages.completion_engine import suggest_type
12-
from .packages.parseutils import last_word
13-
from .packages.special.iocommands import favoritequeries
12+
from .packages.filepaths import complete_path, parse_path, suggest_path
13+
from .packages.parseutils import LAST_WORD_INCLUDE_TYPE, last_word
1414
from .packages.special import llm
15-
from .packages.filepaths import parse_path, complete_path, suggest_path
15+
from .packages.special.iocommands import favoritequeries
1616

1717
_logger = logging.getLogger(__name__)
1818

@@ -381,7 +381,7 @@ def extend_functions(self, func_data: Iterable[Sequence[str]]) -> None:
381381
metadata[self.dbname][func[0]] = None
382382
self.all_completions.add(func[0])
383383

384-
def set_dbname(self, dbname: str) -> None:
384+
def set_dbname(self, dbname: str | None) -> None:
385385
self.dbname = dbname
386386

387387
def reset_completions(self) -> None:
@@ -397,7 +397,7 @@ def find_matches(
397397
start_only: bool = False,
398398
fuzzy: bool = True,
399399
casing: str | None = None,
400-
punctuations: str = "most_punctuations",
400+
punctuations: LAST_WORD_INCLUDE_TYPE = "most_punctuations",
401401
) -> Generator[Completion, None, None]:
402402
"""Find completion matches for the given text.
403403

litecli/sqlexecute.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
try:
88
import sqlean as sqlite3
9-
from sqlean import OperationalError
9+
from sqlean import OperationalError # ty: ignore[unresolved-import]
1010

1111
sqlite3.extensions.enable_all()
1212
except ImportError:
@@ -17,10 +17,9 @@
1717

1818
import sqlparse
1919

20+
from litecli.packages import special
2021
from litecli.packages.special.utils import check_if_sqlitedotcommand
2122

22-
from .packages import special
23-
2423
_logger = logging.getLogger(__name__)
2524

2625
# FIELD_TYPES = decoders.copy()

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies = [
1818
"setuptools", # Required by llm commands to install models
1919
"pip",
2020
"llm>=0.25.0",
21-
"ty>=0.0.2",
21+
"ty>=0.0.4",
2222
]
2323

2424
[build-system]
@@ -92,10 +92,8 @@ exclude = [
9292

9393
[tool.ty.environment]
9494
python-version = "3.9"
95-
root = ["litecli"]
95+
root = [".", "litecli", "litecli/packages", "litecli/packages/special"]
9696

97-
[tool.ty.rules]
98-
unresolved-import = "ignore"
9997

10098
[tool.ty.src]
10199
exclude = [

0 commit comments

Comments
 (0)