Skip to content

Commit 6f06dd9

Browse files
committed
Working on improving type hinting
Also: - Refactored perror() to remove a rarely used optional argument which was unecessary
1 parent dd299bf commit 6f06dd9

File tree

6 files changed

+131
-118
lines changed

6 files changed

+131
-118
lines changed

cmd2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
22
# -*- coding: utf-8 -*-
3+
"""This simply imports certain things for backwards compatibility."""
34
from .cmd2 import __version__, Cmd, CmdResult, Statement, EmptyStatement, categorize
45
from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category

cmd2/cmd2.py

Lines changed: 112 additions & 104 deletions
Large diffs are not rendered by default.

cmd2/pyscript_bridge.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import argparse
1111
import functools
1212
import sys
13-
from typing import List, Tuple, Callable
13+
from typing import List, Callable
1414

1515
# Python 3.4 require contextlib2 for temporarily redirecting stderr and stdout
1616
if sys.version_info < (3, 5):
@@ -298,4 +298,5 @@ def __dir__(self):
298298
return commands
299299

300300
def __call__(self, args: str):
301-
return _exec_cmd(self._cmd2_app, functools.partial(self._cmd2_app.onecmd_plus_hooks, args + '\n'), self.cmd_echo)
301+
return _exec_cmd(self._cmd2_app, functools.partial(self._cmd2_app.onecmd_plus_hooks, args + '\n'),
302+
self.cmd_echo)

cmd2/transcript.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import re
1313
import glob
1414
import unittest
15+
from typing import Tuple
1516

1617
from . import utils
1718

19+
1820
class Cmd2TestCase(unittest.TestCase):
1921
"""A unittest class used for transcript testing.
2022
@@ -50,7 +52,7 @@ def runTest(self): # was testall
5052
for (fname, transcript) in its:
5153
self._test_transcript(fname, transcript)
5254

53-
def _test_transcript(self, fname, transcript):
55+
def _test_transcript(self, fname: str, transcript):
5456
line_num = 0
5557
finished = False
5658
line = utils.strip_ansi(next(transcript))
@@ -103,7 +105,7 @@ def _test_transcript(self, fname, transcript):
103105
fname, line_num, command, expected, result)
104106
self.assertTrue(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
105107

106-
def _transform_transcript_expected(self, s):
108+
def _transform_transcript_expected(self, s: str) -> str:
107109
"""Parse the string with slashed regexes into a valid regex.
108110
109111
Given a string like:
@@ -151,7 +153,7 @@ def _transform_transcript_expected(self, s):
151153
return regex
152154

153155
@staticmethod
154-
def _escaped_find(regex, s, start, in_regex):
156+
def _escaped_find(regex: str, s: str, start: int, in_regex: bool) -> Tuple[str, int, int]:
155157
"""Find the next slash in {s} after {start} that is not preceded by a backslash.
156158
157159
If we find an escaped slash, add everything up to and including it to regex,
@@ -162,7 +164,6 @@ def _escaped_find(regex, s, start, in_regex):
162164
{in_regex} specifies whether we are currently searching in a regex, we behave
163165
differently if we are or if we aren't.
164166
"""
165-
166167
while True:
167168
pos = s.find('/', start)
168169
if pos == -1:
@@ -211,14 +212,11 @@ class OutputTrap(object):
211212
def __init__(self):
212213
self.contents = ''
213214

214-
def write(self, txt):
215-
"""Add text to the internal contents.
216-
217-
:param txt: str
218-
"""
215+
def write(self, txt: str):
216+
"""Add text to the internal contents."""
219217
self.contents += txt
220218

221-
def read(self):
219+
def read(self) -> str:
222220
"""Read from the internal contents and then clear them out.
223221
224222
:return: str - text from the internal contents

cmd2/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from . import constants
1010

11+
1112
def strip_ansi(text: str) -> str:
1213
"""Strip ANSI escape codes from a string.
1314
@@ -58,6 +59,7 @@ def namedtuple_with_defaults(typename, field_names, default_values=()):
5859
T.__new__.__defaults__ = tuple(prototype)
5960
return T
6061

62+
6163
def namedtuple_with_two_defaults(typename, field_names, default_values=('', '')):
6264
"""Wrapper around namedtuple which lets you treat the last value as optional.
6365
@@ -72,6 +74,7 @@ def namedtuple_with_two_defaults(typename, field_names, default_values=('', ''))
7274
T.__new__.__defaults__ = default_values
7375
return T
7476

77+
7578
def cast(current, new):
7679
"""Tries to force a new value into the same type as the current when trying to set the value for a parameter.
7780
@@ -101,6 +104,7 @@ def cast(current, new):
101104
print("Problem setting parameter (now %s) to %s; incorrect type?" % (current, new))
102105
return current
103106

107+
104108
def which(editor: str) -> Optional[str]:
105109
"""Find the full path of a given editor.
106110
@@ -118,7 +122,8 @@ def which(editor: str) -> Optional[str]:
118122
editor_path = None
119123
return editor_path
120124

121-
def is_text_file(file_path):
125+
126+
def is_text_file(file_path: str) -> bool:
122127
"""Returns if a file contains only ASCII or UTF-8 encoded text
123128
124129
:param file_path: path to the file being checked

tests/test_cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def test_pyscript_with_nonexist_file(base_app, capsys):
230230
python_script = 'does_not_exist.py'
231231
run_cmd(base_app, "pyscript {}".format(python_script))
232232
out, err = capsys.readouterr()
233-
assert err.startswith('ERROR: [Errno 2] No such file or directory:')
233+
assert err.startswith("EXCEPTION of type 'FileNotFoundError' occurred with message:")
234234

235235
def test_pyscript_with_exception(base_app, capsys, request):
236236
test_dir = os.path.dirname(request.module.__file__)

0 commit comments

Comments
 (0)