1313from io import open
1414
1515try :
16- from sqlean import OperationalError , sqlite_version
16+ from sqlean import OperationalError , sqlite_version # type: ignore[import-untyped]
1717except ImportError :
1818 from sqlite3 import OperationalError , sqlite_version
1919from time import time
20- from typing import Any , Iterable
20+ from typing import Any , Generator , Iterable , cast
2121
2222import click
2323import sqlparse
2424from cli_helpers .tabular_output import TabularOutputFormatter , preprocessors
2525from prompt_toolkit .auto_suggest import AutoSuggestFromHistory
26- from prompt_toolkit .completion import DynamicCompleter
26+ from prompt_toolkit .completion import Completion , DynamicCompleter
2727from prompt_toolkit .document import Document
2828from prompt_toolkit .enums import DEFAULT_BUFFER , EditingMode
2929from prompt_toolkit .filters import HasFocus , IsDone
3535)
3636from prompt_toolkit .lexers import PygmentsLexer
3737from prompt_toolkit .shortcuts import CompleteStyle , PromptSession
38- from typing import cast
39- from prompt_toolkit .completion import Completion
4038
4139from .__init__ import __version__
4240from .clibuffer import cli_is_multiline
5351from .sqlcompleter import SQLCompleter
5452from .sqlexecute import SQLExecute
5553
56- click .disable_unicode_literals_warning = True
57-
5854# Query tuples are used for maintaining history
5955Query = namedtuple ("Query" , ["query" , "successful" , "mutating" ])
6056
@@ -84,7 +80,8 @@ def __init__(
8480 self .key_bindings = c ["main" ]["key_bindings" ]
8581 special .set_favorite_queries (self .config )
8682 self .formatter = TabularOutputFormatter (format_name = c ["main" ]["table_format" ])
87- self .formatter .litecli = self
83+ # self.formatter.litecli = self, ty raises unresolved-attribute, hence use dynamic assignment
84+ setattr (self .formatter , "litecli" , self )
8885 self .syntax_style = c ["main" ]["syntax_style" ]
8986 self .less_chatty = c ["main" ].as_bool ("less_chatty" )
9087 self .show_bottom_toolbar = c ["main" ].as_bool ("show_bottom_toolbar" )
@@ -181,7 +178,7 @@ def register_special_commands(self) -> None:
181178 case_sensitive = True ,
182179 )
183180
184- def change_table_format (self , arg : str , ** _ : Any ) -> Iterable [tuple ]:
181+ def change_table_format (self , arg : str , ** _ : Any ) -> Generator [tuple [ None , None , None , str ], None , None ]:
185182 try :
186183 self .formatter .format_name = arg
187184 yield (None , None , None , "Changed table format to {}" .format (arg ))
@@ -200,11 +197,14 @@ def change_db(self, arg: str | None, **_: Any) -> Iterable[tuple]:
200197 self .sqlexecute .connect (database = arg )
201198
202199 self .refresh_completions ()
200+ # guard so that ty doesn't complain
201+ dbname = self .sqlexecute .dbname if self .sqlexecute is not None else ""
202+
203203 yield (
204204 None ,
205205 None ,
206206 None ,
207- 'You are now connected to database "%s"' % (self . sqlexecute . dbname ),
207+ 'You are now connected to database "%s"' % (dbname ),
208208 )
209209
210210 def execute_from_file (self , arg : str | None , ** _ : Any ) -> Iterable [tuple [Any , ...]]:
@@ -303,7 +303,7 @@ def get(key: str) -> str | None:
303303
304304 return {x : get (x ) for x in keys }
305305
306- def connect (self , database : str = "" ) -> None :
306+ def connect (self , database : str | None = "" ) -> None :
307307 cnf : dict [str , str | None ] = {"database" : None }
308308
309309 cnf = self .read_my_cnf_files (cnf .keys ())
@@ -510,7 +510,8 @@ def one_iteration(text: str | None = None) -> None:
510510 successful = False
511511 start = time ()
512512 res = sqlexecute .run (text )
513- self .formatter .query = text
513+ # Set query attribute dynamically on formatter
514+ setattr (self .formatter , "query" , text )
514515 successful = True
515516 special .unset_once_if_written ()
516517 # Keep track of whether or not the query is mutating. In case
@@ -522,7 +523,8 @@ def one_iteration(text: str | None = None) -> None:
522523 raise e
523524 except KeyboardInterrupt :
524525 try :
525- sqlexecute .conn .interrupt ()
526+ # since connection can be sqlite3 or sqlean, it's hard to annotate the type for interrupt. so ignore the type hint warning.
527+ sqlexecute .conn .interrupt () # type: ignore[attr-defined]
526528 except Exception as e :
527529 self .echo (
528530 "Encountered error while cancelling query: {}" .format (e ),
@@ -755,6 +757,7 @@ def refresh_completions(self, reset: bool = False) -> list[tuple]:
755757 if reset :
756758 with self ._completer_lock :
757759 self .completer .reset_completions ()
760+ assert self .sqlexecute is not None
758761 self .completion_refresher .refresh (
759762 self .sqlexecute ,
760763 self ._on_completions_refreshed ,
@@ -815,7 +818,7 @@ def run_query(self, query: str, new_line: bool = True) -> None:
815818 results = self .sqlexecute .run (query )
816819 for result in results :
817820 title , cur , headers , status = result
818- self .formatter . query = query
821+ setattr ( self .formatter , " query" , query )
819822 output = self .format_output (title , cur , headers )
820823 for line in output :
821824 click .echo (line , nl = new_line )
0 commit comments