1- # mypy: ignore-errors
2-
31from __future__ import annotations
42
53import locale
108import subprocess
119from io import open
1210from time import sleep
13- from typing import Any , Dict , Generator , List , Optional , Tuple , TextIO
11+ from typing import Any , Generator , List , Optional , Tuple , TextIO
1412
1513import click
1614import sqlparse
@@ -115,8 +113,9 @@ def editor_command(command: str) -> bool:
115113@export
116114def get_filename (sql : str ) -> Optional [str ]:
117115 if sql .strip ().startswith ("\\ e" ):
118- command , _ , filename = sql .partition (" " )
116+ _cmd , _sep , filename = sql .partition (" " )
119117 return filename .strip () or None
118+ return None
120119
121120
122121@export
@@ -188,7 +187,7 @@ def execute_favorite_query(cur: Any, arg: str, verbose: bool = False, **_: Any)
188187 yield result
189188
190189 """Parse out favorite name and optional substitution parameters"""
191- name , _ , arg_str = arg .partition (" " )
190+ name , _sep , arg_str = arg .partition (" " )
192191 args = shlex .split (arg_str )
193192
194193 query = favoritequeries .get (name )
@@ -269,7 +268,7 @@ def save_favorite_query(arg: str, **_: Any) -> List[Tuple]:
269268 if not arg :
270269 return [(None , None , None , usage )]
271270
272- name , _ , query = arg .partition (" " )
271+ name , _sep , query = arg .partition (" " )
273272
274273 # If either name or query is missing then print the usage and complain.
275274 if (not name ) or (not query ):
@@ -310,19 +309,21 @@ def execute_system_command(arg: str, **_: Any) -> List[Tuple]:
310309 args = arg .split (" " )
311310 process = subprocess .Popen (args , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
312311 output , error = process .communicate ()
313- response = output if not error else error
312+ raw = output if not error else error
314313
315314 # Python 3 returns bytes. This needs to be decoded to a string.
316- if isinstance (response , bytes ):
315+ if isinstance (raw , bytes ):
317316 encoding = locale .getpreferredencoding (False )
318- response = response .decode (encoding )
317+ response = raw .decode (encoding )
318+ else :
319+ response = raw
319320
320321 return [(None , None , None , response )]
321322 except OSError as e :
322323 return [(None , None , None , "OSError: %s" % e .strerror )]
323324
324325
325- def parseargfile (arg : str ) -> Dict [str , str ]:
326+ def parseargfile (arg : str ) -> Tuple [str , str ]:
326327 if arg .startswith ("-o " ):
327328 mode = "w"
328329 filename = arg [3 :]
@@ -333,7 +334,7 @@ def parseargfile(arg: str) -> Dict[str, str]:
333334 if not filename :
334335 raise TypeError ("You must provide a filename." )
335336
336- return { "file" : os .path .expanduser (filename ), " mode" : mode }
337+ return ( os .path .expanduser (filename ), mode )
337338
338339
339340@special_command (
@@ -346,7 +347,8 @@ def set_tee(arg: str, **_: Any) -> List[Tuple]:
346347 global tee_file
347348
348349 try :
349- tee_file = open (** parseargfile (arg ))
350+ file , mode = parseargfile (arg )
351+ tee_file = open (file , mode )
350352 except (IOError , OSError ) as e :
351353 raise OSError ("Cannot write to file '{}': {}" .format (e .filename , e .strerror ))
352354
@@ -409,7 +411,8 @@ def unset_once_if_written() -> None:
409411 global once_file , written_to_once_file
410412 if once_file and written_to_once_file :
411413 once_file .close ()
412- once_file = written_to_once_file = None
414+ once_file = None
415+ written_to_once_file = False
413416
414417
415418@special_command (
@@ -453,7 +456,7 @@ def write_pipe_once(output: str) -> None:
453456def unset_pipe_once_if_written () -> None :
454457 """Unset the pipe_once cmd, if it has been written to."""
455458 global pipe_once_process , written_to_pipe_once_process
456- if written_to_pipe_once_process :
459+ if written_to_pipe_once_process and pipe_once_process :
457460 (stdout_data , stderr_data ) = pipe_once_process .communicate ()
458461 if len (stdout_data ) > 0 :
459462 print (stdout_data .rstrip ("\n " ))
0 commit comments