Skip to content

Commit 92b8a38

Browse files
committed
Minor type hinting fixes.
1 parent 872da20 commit 92b8a38

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

cmd2/utils.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import subprocess
1212
import sys
1313
import threading
14+
1415
import unicodedata
1516
from enum import Enum
16-
from typing import Any, Callable, Dict, Iterable, List, Optional, TextIO, Union
17+
from typing import Any, Callable, Dict, IO, Iterable, List, Optional, TextIO, Type, Union
1718

1819
from . import constants
1920

@@ -470,10 +471,15 @@ def getbytes(self) -> bytes:
470471
"""Get the internal contents as bytes"""
471472
return bytes(self.buffer.byte_buf)
472473

473-
def read(self) -> str:
474+
def read(self, size: Optional[int] = -1) -> str:
474475
"""Read from the internal contents as a str and then clear them out"""
475-
result = self.getvalue()
476-
self.clear()
476+
if size is None or size == -1:
477+
result = self.getvalue()
478+
self.clear()
479+
else:
480+
result = self.buffer.byte_buf[:size].decode(encoding=self.encoding, errors=self.errors)
481+
self.buffer.byte_buf = self.buffer.byte_buf[size:]
482+
477483
return result
478484

479485
def readbytes(self) -> bytes:
@@ -668,7 +674,7 @@ def __exit__(self, *args) -> None:
668674

669675
class RedirectionSavedState:
670676
"""Created by each command to store information required to restore state after redirection"""
671-
def __init__(self, self_stdout: Union[StdSim, TextIO], sys_stdout: Union[StdSim, TextIO],
677+
def __init__(self, self_stdout: Union[StdSim, IO[str]], sys_stdout: Union[StdSim, IO[str]],
672678
pipe_proc_reader: Optional[ProcReader], saved_redirecting: bool) -> None:
673679
"""
674680
RedirectionSavedState initializer
@@ -1025,11 +1031,12 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
10251031
10261032
:Example:
10271033
1034+
>>> import cmd2
10281035
>>> class MyApp(cmd2.Cmd):
10291036
>>> def do_echo(self, arglist):
10301037
>>> self.poutput(' '.join(arglist)
10311038
>>>
1032-
>>> utils.categorize(do_echo, "Text Processing")
1039+
>>> cmd2.utils.categorize(do_echo, "Text Processing")
10331040
10341041
For an alternative approach to categorizing commands using a decorator, see
10351042
:func:`~cmd2.decorators.with_category`
@@ -1044,7 +1051,7 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
10441051
setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)
10451052

10461053

1047-
def get_defining_class(meth):
1054+
def get_defining_class(meth) -> Type:
10481055
"""
10491056
Attempts to resolve the class that defined a method.
10501057

tests/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def test_stdsim_read(stdout_sim):
154154
assert stdout_sim.read() == my_str
155155
assert stdout_sim.getvalue() == ''
156156

157+
stdout_sim.write(my_str)
158+
159+
assert stdout_sim.getvalue() == my_str
160+
assert stdout_sim.read(2) == my_str[:2]
161+
assert stdout_sim.getvalue() == my_str[2:]
162+
163+
157164
def test_stdsim_read_bytes(stdout_sim):
158165
b_str = b'Hello World'
159166
stdout_sim.buffer.write(b_str)

0 commit comments

Comments
 (0)