|
12 | 12 | from datetime import datetime |
13 | 13 | from io import open |
14 | 14 | from time import time |
15 | | -from typing import Any, Generator, Iterable, cast |
| 15 | +from typing import Any, Generator, Iterable, Literal, TextIO, cast |
16 | 16 |
|
17 | 17 | import click |
18 | 18 | import sqlparse |
@@ -75,13 +75,13 @@ def __init__( |
75 | 75 | self, |
76 | 76 | sqlexecute: SQLExecute | None = None, |
77 | 77 | prompt: str | None = None, |
78 | | - logfile: Any | None = None, |
| 78 | + logfile: TextIO | None = None, |
79 | 79 | auto_vertical_output: bool = False, |
80 | 80 | warn: bool | None = None, |
81 | 81 | liteclirc: str | None = None, |
82 | 82 | ) -> None: |
83 | 83 | self.sqlexecute = sqlexecute |
84 | | - self.logfile = logfile |
| 84 | + self.logfile: TextIO | Literal[False] | None = logfile |
85 | 85 |
|
86 | 86 | # Load config. |
87 | 87 | c = self.config = get_config(liteclirc) |
@@ -473,7 +473,9 @@ def one_iteration(text: str | None = None) -> None: |
473 | 473 | try: |
474 | 474 | start = time() |
475 | 475 | assert self.sqlexecute is not None |
476 | | - cur = self.sqlexecute.conn and self.sqlexecute.conn.cursor() |
| 476 | + conn = self.sqlexecute.conn |
| 477 | + assert conn is not None |
| 478 | + cur = conn.cursor() |
477 | 479 | context, sql, duration = special.handle_llm(text, cur) |
478 | 480 | if context: |
479 | 481 | click.echo("LLM Reponse:") |
@@ -535,7 +537,9 @@ def one_iteration(text: str | None = None) -> None: |
535 | 537 | except KeyboardInterrupt: |
536 | 538 | try: |
537 | 539 | # since connection can be sqlite3 or sqlean, it's hard to annotate the type for interrupt. so ignore the type hint warning. |
538 | | - sqlexecute.conn.interrupt() # type: ignore[attr-defined] |
| 540 | + conn = sqlexecute.conn |
| 541 | + if conn is not None: |
| 542 | + conn.interrupt() # type: ignore[attr-defined] |
539 | 543 | except Exception as e: |
540 | 544 | self.echo( |
541 | 545 | "Encountered error while cancelling query: {}".format(e), |
@@ -792,7 +796,7 @@ def _on_completions_refreshed(self, new_completer: SQLCompleter) -> None: |
792 | 796 |
|
793 | 797 | def get_completions(self, text: str, cursor_positition: int) -> Iterable[Completion]: |
794 | 798 | with self._completer_lock: |
795 | | - return cast(Iterable[Completion], self.completer.get_completions(Document(text=text, cursor_position=cursor_positition), None)) |
| 799 | + return self.completer.get_completions(Document(text=text, cursor_position=cursor_positition), None) |
796 | 800 |
|
797 | 801 | def get_prompt(self, string: str) -> str: |
798 | 802 | self.logger.debug("Getting prompt %r", string) |
@@ -934,7 +938,7 @@ def cli( |
934 | 938 | database: str, |
935 | 939 | dbname: str, |
936 | 940 | prompt: str | None, |
937 | | - logfile: Any | None, |
| 941 | + logfile: TextIO | None, |
938 | 942 | auto_vertical_output: bool, |
939 | 943 | table: bool, |
940 | 944 | csv: bool, |
|
0 commit comments