diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-06-11 10:42:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-11 10:42:01 -0400 |
commit | 3ae71e80e61a908ad0a0dec84ee6496592176022 (patch) | |
tree | 8ceeddb81ff1a660e1ada5ba55f379968f23bfb6 /cmd2/utils.py | |
parent | 1bcf59856fae624e72153c631b94a1c7cca17d9e (diff) | |
parent | 44282fa49b2db9e4de7dc2f08e4d60e8b2be145f (diff) | |
download | cmd2-git-3ae71e80e61a908ad0a0dec84ee6496592176022.tar.gz |
Merge pull request #440 from python-cmd2/type_hinting
Improve type hinting
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 |