diff options
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 07969ff1..11d48b78 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -4,10 +4,11 @@ import collections import os -from typing import Optional +from typing import Any, List, Optional, Union from . import constants + def strip_ansi(text: str) -> str: """Strip ANSI escape codes from a string. @@ -30,7 +31,8 @@ def strip_quotes(arg: str) -> str: return arg -def namedtuple_with_defaults(typename, field_names, default_values=()): +def namedtuple_with_defaults(typename: str, field_names: Union[str, List[str]], + default_values: collections.Iterable=()): """ Convenience function for defining a namedtuple with default values @@ -58,7 +60,9 @@ def namedtuple_with_defaults(typename, field_names, default_values=()): T.__new__.__defaults__ = tuple(prototype) return T -def namedtuple_with_two_defaults(typename, field_names, default_values=('', '')): + +def namedtuple_with_two_defaults(typename: str, field_names: Union[str, List[str]], + default_values: collections.Iterable=('', '')): """Wrapper around namedtuple which lets you treat the last value as optional. :param typename: str - type name for the Named tuple @@ -72,7 +76,8 @@ def namedtuple_with_two_defaults(typename, field_names, default_values=('', '')) T.__new__.__defaults__ = default_values return T -def cast(current, new): + +def cast(current: Any, new: str) -> Any: """Tries to force a new value into the same type as the current when trying to set the value for a parameter. :param current: current value for the parameter, type varies @@ -101,6 +106,7 @@ def cast(current, new): print("Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)) return current + def which(editor: str) -> Optional[str]: """Find the full path of a given editor. @@ -118,7 +124,8 @@ def which(editor: str) -> Optional[str]: editor_path = None return editor_path -def is_text_file(file_path): + +def is_text_file(file_path: str) -> bool: """Returns if a file contains only ASCII or UTF-8 encoded text :param file_path: path to the file being checked |