Skip to content

Commit a99c644

Browse files
committed
More refinements to type checking.
1 parent 17b6e2a commit a99c644

File tree

3 files changed

+29
-40
lines changed

3 files changed

+29
-40
lines changed

litecli/packages/special/favoritequeries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FavoriteQueries(object):
3939
def __init__(self, config: Any) -> None:
4040
self.config = config
4141

42-
def list(self) -> List[Optional[str]]:
42+
def list(self) -> List[str]:
4343
return self.config.get(self.section_name, [])
4444

4545
def get(self, name: str) -> Optional[str]:

litecli/packages/special/iocommands.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -135,42 +135,28 @@ def get_editor_query(sql: str) -> str:
135135

136136
@export
137137
def open_external_editor(filename: Optional[str] = None, sql: Optional[str] = None) -> Tuple[str, Optional[str]]:
138-
"""Open external editor, wait for the user to type in their query, return
139-
the query.
140-
141-
:return: list with one tuple, query as first element.
142-
143-
"""
144-
145-
message = None
146-
filename = filename.strip().split(" ", 1)[0] if filename else None
147-
138+
"""Open external editor, wait for the user to type in their query, return the query."""
139+
message: Optional[str] = None
148140
sql = sql or ""
149141
MARKER = "# Type your query above this line.\n"
150142

151-
# Populate the editor buffer with the partial sql (if available) and a
152-
# placeholder comment.
153-
query = click.edit(
154-
"{sql}\n\n{marker}".format(sql=sql, marker=MARKER),
155-
filename=filename,
156-
extension=".sql",
157-
)
158-
159143
if filename:
144+
filename = filename.strip().split(" ", 1)[0]
145+
click.edit(filename=filename)
160146
try:
161147
with open(filename, encoding="utf-8") as f:
162-
query = f.read()
148+
text = f.read()
163149
except IOError:
164-
message = "Error reading file: %s." % filename
150+
message = f"Error reading file: {filename}."
151+
text = sql
152+
return (text, message)
165153

166-
if query is not None:
167-
query = query.split(MARKER, 1)[0].rstrip("\n")
154+
edited = click.edit(f"{sql}\n\n{MARKER}", extension=".sql")
155+
if edited:
156+
edited = edited.split(MARKER, 1)[0].rstrip("\n")
168157
else:
169-
# Don't return None for the caller to deal with.
170-
# Empty string is ok.
171-
query = sql
172-
173-
return (query, message)
158+
edited = sql
159+
return (edited, None)
174160

175161

176162
@special_command(
@@ -310,13 +296,9 @@ def execute_system_command(arg: str, **_: Any) -> List[Tuple]:
310296
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
311297
output, error = process.communicate()
312298
raw = output if not error else error
313-
314299
# Python 3 returns bytes. This needs to be decoded to a string.
315-
if isinstance(raw, bytes):
316-
encoding = locale.getpreferredencoding(False)
317-
response = raw.decode(encoding)
318-
else:
319-
response = raw
300+
encoding = locale.getpreferredencoding(False)
301+
response: str = raw.decode(encoding) if isinstance(raw, bytes) else str(raw)
320302

321303
return [(None, None, None, response)]
322304
except OSError as e:
@@ -348,7 +330,9 @@ def set_tee(arg: str, **_: Any) -> List[Tuple]:
348330

349331
try:
350332
file, mode = parseargfile(arg)
351-
tee_file = open(file, mode)
333+
from typing import cast
334+
335+
tee_file = cast(TextIO, open(file, mode))
352336
except (IOError, OSError) as e:
353337
raise OSError("Cannot write to file '{}': {}".format(e.filename, e.strerror))
354338

@@ -387,7 +371,10 @@ def write_tee(output: str) -> None:
387371
def set_once(arg: str, **_: Any) -> List[Tuple]:
388372
global once_file, written_to_once_file
389373
try:
390-
once_file = open(**parseargfile(arg))
374+
file, mode = parseargfile(arg)
375+
from typing import cast
376+
377+
once_file = cast(TextIO, open(file, mode))
391378
except (IOError, OSError) as e:
392379
raise OSError("Cannot write to file '{}': {}".format(e.filename, e.strerror))
393380
written_to_once_file = False
@@ -480,7 +467,7 @@ def watch_query(arg: str, **kwargs: Any) -> Generator[Tuple, None, None]:
480467
if not arg:
481468
yield (None, None, None, usage)
482469
raise StopIteration
483-
seconds = 5
470+
seconds: float = 5.0
484471
clear_screen = False
485472
statement = None
486473
while statement is None:

litecli/packages/special/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def format_uptime(uptime_in_seconds: str) -> str:
5252
return uptime
5353

5454

55-
def check_if_sqlitedotcommand(command: str) -> bool:
55+
def check_if_sqlitedotcommand(command: object) -> bool:
5656
"""Does a check if the command supplied is in the list of SQLite dot commands.
5757
5858
:param command: A command (str) supplied from the user
@@ -128,5 +128,7 @@ def check_if_sqlitedotcommand(command: str) -> bool:
128128
".width",
129129
]
130130

131-
command = command.split(" ", 1)[0].lower()
132-
return command in sqlite3dotcommands
131+
if isinstance(command, str):
132+
head = command.split(" ", 1)[0].lower()
133+
return head in sqlite3dotcommands
134+
return False

0 commit comments

Comments
 (0)