4242import sys
4343import tempfile
4444import traceback
45+ from typing import Callable , List , Optional , Union
4546import unittest
4647from code import InteractiveConsole
4748
@@ -141,13 +142,13 @@ def __subclasshook__(cls, C):
141142HELP_SUMMARY = 'help_summary'
142143
143144
144- def categorize (func , category ) :
145+ def categorize (func : Union [ Callable , Iterable ], category : str ) -> None :
145146 """Categorize a function.
146147
147148 The help command output will group this function under the specified category heading
148149
149- :param func: Union[Callable, Iterable] - function to categorize
150- :param category: str - category to put it in
150+ :param func: function to categorize
151+ :param category: category to put it in
151152 """
152153 if isinstance (func , Iterable ):
153154 for item in func :
@@ -156,25 +157,25 @@ def categorize(func, category):
156157 setattr (func , HELP_CATEGORY , category )
157158
158159
159- def set_posix_shlex (val ) :
160+ def set_posix_shlex (val : bool ) -> None :
160161 """ Allows user of cmd2 to choose between POSIX and non-POSIX splitting of args for decorated commands.
161162
162- :param val: bool - True => POSIX, False => Non-POSIX
163+ :param val: True => POSIX, False => Non-POSIX
163164 """
164165 global POSIX_SHLEX
165166 POSIX_SHLEX = val
166167
167168
168- def set_strip_quotes (val ) :
169+ def set_strip_quotes (val : bool ) -> None :
169170 """ Allows user of cmd2 to choose whether to automatically strip outer-quotes when POSIX_SHLEX is False.
170171
171- :param val: bool - True => strip quotes on args for decorated commands if POSIX_SHLEX is False.
172+ :param val: True => strip quotes on args for decorated commands if POSIX_SHLEX is False.
172173 """
173174 global STRIP_QUOTES_FOR_NON_POSIX
174175 STRIP_QUOTES_FOR_NON_POSIX = val
175176
176177
177- def _which (editor ) :
178+ def _which (editor : str ) -> Optional [ str ] :
178179 try :
179180 editor_path = subprocess .check_output (['which' , editor ], stderr = subprocess .STDOUT ).strip ()
180181 editor_path = editor_path .decode ()
@@ -183,13 +184,13 @@ def _which(editor):
183184 return editor_path
184185
185186
186- def strip_quotes (arg ) :
187+ def strip_quotes (arg : str ) -> str :
187188 """ Strip outer quotes from a string.
188189
189190 Applies to both single and double quotes.
190191
191- :param arg: str - string to strip outer quotes from
192- :return str - same string with potentially outer quotes stripped
192+ :param arg: string to strip outer quotes from
193+ :return: same string with potentially outer quotes stripped
193194 """
194195 quote_chars = '"' + "'"
195196
@@ -198,7 +199,7 @@ def strip_quotes(arg):
198199 return arg
199200
200201
201- def parse_quoted_string (cmdline ) :
202+ def parse_quoted_string (cmdline : str ) -> List [ str ] :
202203 """Parse a quoted string into a list of arguments."""
203204 if isinstance (cmdline , list ):
204205 # arguments are already a list, return the list we were passed
@@ -215,15 +216,15 @@ def parse_quoted_string(cmdline):
215216 return lexed_arglist
216217
217218
218- def with_category (category ) :
219- """A decorator to apply a category to a command function"""
219+ def with_category (category : str ) -> Callable :
220+ """A decorator to apply a category to a command function. """
220221 def cat_decorator (func ):
221222 categorize (func , category )
222223 return func
223224 return cat_decorator
224225
225226
226- def with_argument_list (func ) :
227+ def with_argument_list (func : Callable ) -> Callable :
227228 """A decorator to alter the arguments passed to a do_* cmd2
228229 method. Default passes a string of whatever the user typed.
229230 With this decorator, the decorated method will receive a list
@@ -237,16 +238,15 @@ def cmd_wrapper(self, cmdline):
237238 return cmd_wrapper
238239
239240
240- def with_argparser_and_unknown_args (argparser ) :
241+ def with_argparser_and_unknown_args (argparser : argparse . ArgumentParser ) -> Callable :
241242 """A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given
242243 instance of argparse.ArgumentParser, but also returning unknown args as a list.
243244
244245 :param argparser: argparse.ArgumentParser - given instance of ArgumentParser
245246 :return: function that gets passed parsed args and a list of unknown args
246247 """
247-
248248 # noinspection PyProtectedMember
249- def arg_decorator (func ):
249+ def arg_decorator (func : Callable ):
250250 @functools .wraps (func )
251251 def cmd_wrapper (instance , cmdline ):
252252 lexed_arglist = parse_quoted_string (cmdline )
@@ -289,7 +289,7 @@ def cmd_wrapper(instance, cmdline):
289289 return arg_decorator
290290
291291
292- def with_argparser (argparser ) :
292+ def with_argparser (argparser : argparse . ArgumentParser ) -> Callable :
293293 """A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
294294 with the given instance of argparse.ArgumentParser.
295295
@@ -298,7 +298,7 @@ def with_argparser(argparser):
298298 """
299299
300300 # noinspection PyProtectedMember
301- def arg_decorator (func ):
301+ def arg_decorator (func : Callable ):
302302 @functools .wraps (func )
303303 def cmd_wrapper (instance , cmdline ):
304304 lexed_arglist = parse_quoted_string (cmdline )
@@ -361,25 +361,25 @@ def cmd_wrapper(instance, cmdline):
361361 can_clip = True
362362
363363
364- def disable_clip ():
364+ def disable_clip () -> None :
365365 """ Allows user of cmd2 to manually disable clipboard cut-and-paste functionality."""
366366 global can_clip
367367 can_clip = False
368368
369369
370- def get_paste_buffer ():
370+ def get_paste_buffer () -> str :
371371 """Get the contents of the clipboard / paste buffer.
372372
373- :return: str - contents of the clipboard
373+ :return: contents of the clipboard
374374 """
375375 pb_str = pyperclip .paste ()
376376 return pb_str
377377
378378
379- def write_to_paste_buffer (txt ) :
379+ def write_to_paste_buffer (txt : str ) -> None :
380380 """Copy text to the clipboard / paste buffer.
381381
382- :param txt: str - text to copy to the clipboard
382+ :param txt: text to copy to the clipboard
383383 """
384384 pyperclip .copy (txt )
385385
@@ -400,11 +400,11 @@ def full_parsed_statement(self):
400400 return new
401401
402402
403- def replace_with_file_contents (fname ) :
403+ def replace_with_file_contents (fname : str ) -> str :
404404 """Action to perform when successfully matching parse element definition for inputFrom parser.
405405
406- :param fname: str - filename
407- :return: str - contents of file "fname"
406+ :param fname: filename
407+ :return: contents of file "fname"
408408 """
409409 try :
410410 # Any outer quotes are not part of the filename
@@ -432,16 +432,16 @@ class EmptyStatement(Exception):
432432ANSI_ESCAPE_RE = re .compile (r'\x1b[^m]*m' )
433433
434434
435- def strip_ansi (text ) :
435+ def strip_ansi (text : str ) -> str :
436436 """Strip ANSI escape codes from a string.
437437
438- :param text: str - a string which may contain ANSI escape codes
439- :return: str - the same string with any ANSI escape codes removed
438+ :param text: string which may contain ANSI escape codes
439+ :return: the same string with any ANSI escape codes removed
440440 """
441441 return ANSI_ESCAPE_RE .sub ('' , text )
442442
443443
444- def _pop_readline_history (clear_history = True ):
444+ def _pop_readline_history (clear_history : bool = True ) -> List [ str ] :
445445 """Returns a copy of readline's history and optionally clears it (default)"""
446446 # noinspection PyArgumentList
447447 history = [
0 commit comments