Skip to content

Commit 71d8f57

Browse files
authored
Started refactoring docstrings to meet pydocstyle standards (#1428)
1 parent 4ebc67a commit 71d8f57

17 files changed

+331
-213
lines changed

cmd2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""This simply imports certain things for backwards compatibility."""
1+
"""Import certain things for backwards compatibility."""
22

33
import argparse
44
import contextlib

cmd2/ansi.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Support for ANSI escape sequences which are used for things like applying style to text,
2-
setting the window title, and asynchronous alerts.
1+
"""Support for ANSI escape sequences.
2+
3+
These are used for things like applying style to text, setting the window title, and asynchronous alerts.
34
"""
45

56
import functools
@@ -92,6 +93,7 @@ def strip_style(text: str) -> str:
9293

9394
def style_aware_wcswidth(text: str) -> int:
9495
"""Wrap wcswidth to make it compatible with strings that contain ANSI style sequences.
96+
9597
This is intended for single line strings. If text contains a newline, this
9698
function will return -1. For multiline strings, call widest_line() instead.
9799
@@ -105,8 +107,10 @@ def style_aware_wcswidth(text: str) -> int:
105107

106108

107109
def widest_line(text: str) -> int:
108-
"""Return the width of the widest line in a multiline string. This wraps style_aware_wcswidth()
109-
so it handles ANSI style sequences and has the same restrictions on non-printable characters.
110+
"""Return the width of the widest line in a multiline string.
111+
112+
This wraps style_aware_wcswidth() so it handles ANSI style sequences and has the same
113+
restrictions on non-printable characters.
110114
111115
:param text: the string being measured
112116
:return: The width of the string when printed to the terminal if no errors occur.
@@ -186,14 +190,16 @@ class AnsiSequence:
186190
"""Base class to create ANSI sequence strings."""
187191

188192
def __add__(self, other: Any) -> str:
189-
"""Support building an ANSI sequence string when self is the left operand
190-
e.g. Fg.LIGHT_MAGENTA + "hello".
193+
"""Support building an ANSI sequence string when self is the left operand.
194+
195+
e.g. Fg.LIGHT_MAGENTA + "hello"
191196
"""
192197
return str(self) + str(other)
193198

194199
def __radd__(self, other: Any) -> str:
195-
"""Support building an ANSI sequence string when self is the right operand
196-
e.g. "hello" + Fg.RESET.
200+
"""Support building an ANSI sequence string when self is the right operand.
201+
202+
e.g. "hello" + Fg.RESET
197203
"""
198204
return str(other) + str(self)
199205

@@ -262,7 +268,8 @@ class TextStyle(AnsiSequence, Enum):
262268
UNDERLINE_DISABLE = 24
263269

264270
def __str__(self) -> str:
265-
"""Return ANSI text style sequence instead of enum name
271+
"""Return ANSI text style sequence instead of enum name.
272+
266273
This is helpful when using a TextStyle in an f-string or format() call
267274
e.g. my_str = f"{TextStyle.UNDERLINE_ENABLE}hello{TextStyle.UNDERLINE_DISABLE}".
268275
"""
@@ -271,6 +278,7 @@ def __str__(self) -> str:
271278

272279
class Fg(FgColor, Enum):
273280
"""Create ANSI sequences for the 16 standard terminal foreground text colors.
281+
274282
A terminal's color settings affect how these colors appear.
275283
To reset any foreground color, use Fg.RESET.
276284
"""
@@ -295,7 +303,8 @@ class Fg(FgColor, Enum):
295303
RESET = 39
296304

297305
def __str__(self) -> str:
298-
"""Return ANSI color sequence instead of enum name
306+
"""Return ANSI color sequence instead of enum name.
307+
299308
This is helpful when using an Fg in an f-string or format() call
300309
e.g. my_str = f"{Fg.BLUE}hello{Fg.RESET}".
301310
"""
@@ -304,6 +313,7 @@ def __str__(self) -> str:
304313

305314
class Bg(BgColor, Enum):
306315
"""Create ANSI sequences for the 16 standard terminal background text colors.
316+
307317
A terminal's color settings affect how these colors appear.
308318
To reset any background color, use Bg.RESET.
309319
"""
@@ -328,7 +338,8 @@ class Bg(BgColor, Enum):
328338
RESET = 49
329339

330340
def __str__(self) -> str:
331-
"""Return ANSI color sequence instead of enum name
341+
"""Return ANSI color sequence instead of enum name.
342+
332343
This is helpful when using a Bg in an f-string or format() call
333344
e.g. my_str = f"{Bg.BLACK}hello{Bg.RESET}".
334345
"""
@@ -337,6 +348,7 @@ def __str__(self) -> str:
337348

338349
class EightBitFg(FgColor, Enum):
339350
"""Create ANSI sequences for 8-bit terminal foreground text colors. Most terminals support 8-bit/256-color mode.
351+
340352
The first 16 colors correspond to the 16 colors from Fg and behave the same way.
341353
To reset any foreground color, including 8-bit, use Fg.RESET.
342354
"""
@@ -599,7 +611,8 @@ class EightBitFg(FgColor, Enum):
599611
GRAY_93 = 255
600612

601613
def __str__(self) -> str:
602-
"""Return ANSI color sequence instead of enum name
614+
"""Return ANSI color sequence instead of enum name.
615+
603616
This is helpful when using an EightBitFg in an f-string or format() call
604617
e.g. my_str = f"{EightBitFg.SLATE_BLUE_1}hello{Fg.RESET}".
605618
"""
@@ -608,6 +621,7 @@ def __str__(self) -> str:
608621

609622
class EightBitBg(BgColor, Enum):
610623
"""Create ANSI sequences for 8-bit terminal background text colors. Most terminals support 8-bit/256-color mode.
624+
611625
The first 16 colors correspond to the 16 colors from Bg and behave the same way.
612626
To reset any background color, including 8-bit, use Bg.RESET.
613627
"""
@@ -870,7 +884,8 @@ class EightBitBg(BgColor, Enum):
870884
GRAY_93 = 255
871885

872886
def __str__(self) -> str:
873-
"""Return ANSI color sequence instead of enum name
887+
"""Return ANSI color sequence instead of enum name.
888+
874889
This is helpful when using an EightBitBg in an f-string or format() call
875890
e.g. my_str = f"{EightBitBg.KHAKI_3}hello{Bg.RESET}".
876891
"""
@@ -879,6 +894,7 @@ def __str__(self) -> str:
879894

880895
class RgbFg(FgColor):
881896
"""Create ANSI sequences for 24-bit (RGB) terminal foreground text colors. The terminal must support 24-bit/true-color.
897+
882898
To reset any foreground color, including 24-bit, use Fg.RESET.
883899
"""
884900

@@ -896,7 +912,8 @@ def __init__(self, r: int, g: int, b: int) -> None:
896912
self._sequence = f"{CSI}38;2;{r};{g};{b}m"
897913

898914
def __str__(self) -> str:
899-
"""Return ANSI color sequence instead of enum name
915+
"""Return ANSI color sequence instead of enum name.
916+
900917
This is helpful when using an RgbFg in an f-string or format() call
901918
e.g. my_str = f"{RgbFg(0, 55, 100)}hello{Fg.RESET}".
902919
"""
@@ -905,6 +922,7 @@ def __str__(self) -> str:
905922

906923
class RgbBg(BgColor):
907924
"""Create ANSI sequences for 24-bit (RGB) terminal background text colors. The terminal must support 24-bit/true-color.
925+
908926
To reset any background color, including 24-bit, use Bg.RESET.
909927
"""
910928

@@ -922,7 +940,8 @@ def __init__(self, r: int, g: int, b: int) -> None:
922940
self._sequence = f"{CSI}48;2;{r};{g};{b}m"
923941

924942
def __str__(self) -> str:
925-
"""Return ANSI color sequence instead of enum name
943+
"""Return ANSI color sequence instead of enum name.
944+
926945
This is helpful when using an RgbBg in an f-string or format() call
927946
e.g. my_str = f"{RgbBg(100, 255, 27)}hello{Bg.RESET}".
928947
"""
@@ -942,6 +961,7 @@ def style(
942961
underline: Optional[bool] = None,
943962
) -> str:
944963
"""Apply ANSI colors and/or styles to a string and return it.
964+
945965
The styling is self contained which means that at the end of the string reset code(s) are issued
946966
to undo whatever styling was done at the beginning.
947967

cmd2/argparse_completer.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
1+
"""Module efines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
2+
23
See the header of argparse_custom.py for instructions on how to use these features.
34
"""
45

@@ -70,13 +71,16 @@ def _build_hint(parser: argparse.ArgumentParser, arg_action: argparse.Action) ->
7071

7172

7273
def _single_prefix_char(token: str, parser: argparse.ArgumentParser) -> bool:
73-
"""Returns if a token is just a single flag prefix character."""
74+
"""Is a token just a single flag prefix character."""
7475
return len(token) == 1 and token[0] in parser.prefix_chars
7576

7677

7778
def _looks_like_flag(token: str, parser: argparse.ArgumentParser) -> bool:
78-
"""Determine if a token looks like a flag. Unless an argument has nargs set to argparse.REMAINDER,
79-
then anything that looks like a flag can't be consumed as a value for it.
79+
"""Determine if a token looks like a flag.
80+
81+
Unless an argument has nargs set to argparse.REMAINDER, then anything that looks like a flag
82+
can't be consumed as a value for it.
83+
8084
Based on argparse._parse_optional().
8185
"""
8286
# Flags have to be at least characters
@@ -131,7 +135,8 @@ def __init__(self, arg_action: argparse.Action) -> None:
131135

132136
class _UnfinishedFlagError(CompletionError):
133137
def __init__(self, flag_arg_state: _ArgumentState) -> None:
134-
"""CompletionError which occurs when the user has not finished the current flag
138+
"""CompletionError which occurs when the user has not finished the current flag.
139+
135140
:param flag_arg_state: information about the unfinished flag action.
136141
"""
137142
arg = f'{argparse._get_action_name(flag_arg_state.action)}'
@@ -142,8 +147,10 @@ def __init__(self, flag_arg_state: _ArgumentState) -> None:
142147

143148
class _NoResultsError(CompletionError):
144149
def __init__(self, parser: argparse.ArgumentParser, arg_action: argparse.Action) -> None:
145-
"""CompletionError which occurs when there are no results. If hinting is allowed, then its message will
146-
be a hint about the argument being tab completed.
150+
"""CompletionError which occurs when there are no results.
151+
152+
If hinting is allowed, then its message will be a hint about the argument being tab completed.
153+
147154
:param parser: ArgumentParser instance which owns the action being tab completed
148155
:param arg_action: action being tab completed.
149156
"""
@@ -241,8 +248,10 @@ def consume_argument(arg_state: _ArgumentState) -> None:
241248
consumed_arg_values[arg_state.action.dest].append(token)
242249

243250
def update_mutex_groups(arg_action: argparse.Action) -> None:
244-
"""Check if an argument belongs to a mutually exclusive group and either mark that group
245-
as complete or print an error if the group has already been completed
251+
"""Check if an argument belongs to a mutually exclusive group potenitally mark that group complete.
252+
253+
Either mark the group as complete or print an error if the group has already been completed.
254+
246255
:param arg_action: the action of the argument
247256
:raises CompletionError: if the group is already completed.
248257
"""
@@ -590,7 +599,8 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[list
590599
return cast(list[str], completions)
591600

592601
def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: int, tokens: list[str]) -> list[str]:
593-
"""Supports cmd2's help command in the completion of subcommand names
602+
"""Supports cmd2's help command in the completion of subcommand names.
603+
594604
:param text: the string prefix we are attempting to match (all matches must begin with it)
595605
:param line: the current input line with leading whitespace removed
596606
:param begidx: the beginning index of the prefix text
@@ -615,7 +625,8 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
615625
return []
616626

617627
def format_help(self, tokens: list[str]) -> str:
618-
"""Supports cmd2's help command in the retrieval of help text
628+
"""Supports cmd2's help command in the retrieval of help text.
629+
619630
:param tokens: arguments passed to help command
620631
:return: help text of the command being queried.
621632
"""
@@ -643,7 +654,8 @@ def _complete_arg(
643654
*,
644655
cmd_set: Optional[CommandSet] = None,
645656
) -> list[str]:
646-
"""Tab completion routine for an argparse argument
657+
"""Tab completion routine for an argparse argument.
658+
647659
:return: list of completions
648660
:raises CompletionError: if the completer or choices function this calls raises one.
649661
"""

0 commit comments

Comments
 (0)