Skip to content

Commit a09ca98

Browse files
committed
Accepted a few recommended refactorings from ruff PLR ruleset
1 parent e1b2cd8 commit a09ca98

File tree

4 files changed

+53
-60
lines changed

4 files changed

+53
-60
lines changed

cmd2/argparse_custom.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,12 @@ def __init__(
400400
raise ValueError(
401401
'With is_completer set to true, to_call must be either CompleterFunc, CompleterFuncWithTokens'
402402
)
403-
else:
404-
if not isinstance(to_call, (ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens)): # pragma: no cover
405-
# runtime checking of Protocols do not currently check the parameters of a function.
406-
raise ValueError(
407-
'With is_completer set to false, to_call must be either: '
408-
'ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens'
409-
)
403+
elif not isinstance(to_call, (ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens)): # pragma: no cover
404+
# runtime checking of Protocols do not currently check the parameters of a function.
405+
raise ValueError(
406+
'With is_completer set to false, to_call must be either: '
407+
'ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens'
408+
)
410409
self.to_call = to_call
411410

412411
@property

cmd2/cmd2.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,8 +1944,7 @@ def _display_matches_gnu_readline(
19441944

19451945
for cur_match in matches_to_display:
19461946
cur_length = ansi.style_aware_wcswidth(cur_match)
1947-
if cur_length > longest_match_length:
1948-
longest_match_length = cur_length
1947+
longest_match_length = max(longest_match_length, cur_length)
19491948
else:
19501949
matches_to_display = matches
19511950

@@ -2106,12 +2105,11 @@ def _perform_completion(
21062105
completer_func = self.completedefault # type: ignore[assignment]
21072106

21082107
# Not a recognized macro or command
2108+
# Check if this command should be run as a shell command
2109+
elif self.default_to_shell and command in utils.get_exes_in_path(command):
2110+
completer_func = self.path_complete
21092111
else:
2110-
# Check if this command should be run as a shell command
2111-
if self.default_to_shell and command in utils.get_exes_in_path(command):
2112-
completer_func = self.path_complete
2113-
else:
2114-
completer_func = self.completedefault # type: ignore[assignment]
2112+
completer_func = self.completedefault # type: ignore[assignment]
21152113

21162114
# Otherwise we are completing the command token or performing custom completion
21172115
else:
@@ -3232,25 +3230,24 @@ def restore_readline() -> None:
32323230
sys.stdout.write(f'{prompt}{line}\n')
32333231

32343232
# Otherwise read from self.stdin
3233+
elif self.stdin.isatty():
3234+
# on a tty, print the prompt first, then read the line
3235+
self.poutput(prompt, end='')
3236+
self.stdout.flush()
3237+
line = self.stdin.readline()
3238+
if len(line) == 0:
3239+
line = 'eof'
32353240
else:
3236-
if self.stdin.isatty():
3237-
# on a tty, print the prompt first, then read the line
3238-
self.poutput(prompt, end='')
3239-
self.stdout.flush()
3240-
line = self.stdin.readline()
3241-
if len(line) == 0:
3242-
line = 'eof'
3241+
# we are reading from a pipe, read the line to see if there is
3242+
# anything there, if so, then decide whether to print the
3243+
# prompt or not
3244+
line = self.stdin.readline()
3245+
if len(line):
3246+
# we read something, output the prompt and the something
3247+
if self.echo:
3248+
self.poutput(f'{prompt}{line}')
32433249
else:
3244-
# we are reading from a pipe, read the line to see if there is
3245-
# anything there, if so, then decide whether to print the
3246-
# prompt or not
3247-
line = self.stdin.readline()
3248-
if len(line):
3249-
# we read something, output the prompt and the something
3250-
if self.echo:
3251-
self.poutput(f'{prompt}{line}')
3252-
else:
3253-
line = 'eof'
3250+
line = 'eof'
32543251

32553252
return line.rstrip('\r\n')
32563253

@@ -3660,8 +3657,7 @@ def _macro_create(self, args: argparse.Namespace) -> None:
36603657
return
36613658

36623659
arg_nums.add(cur_num)
3663-
if cur_num > max_arg_num:
3664-
max_arg_num = cur_num
3660+
max_arg_num = max(max_arg_num, cur_num)
36653661

36663662
arg_list.append(MacroArg(start_index=cur_match.start(), number_str=cur_num_str, is_escaped=False))
36673663

cmd2/transcript.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,28 +204,27 @@ def _escaped_find(regex: str, s: str, start: int, in_regex: bool) -> Tuple[str,
204204
# slash at the beginning of the string, so it can't be
205205
# escaped. We found it.
206206
break
207-
else:
208-
# check if the slash is preceded by a backslash
209-
if s[pos - 1 : pos] == '\\':
210-
# it is.
211-
if in_regex:
212-
# add everything up to the backslash as a
213-
# regular expression
214-
regex += s[start : pos - 1]
215-
# skip the backslash, and add the slash
216-
regex += s[pos]
217-
else:
218-
# add everything up to the backslash as escaped
219-
# plain text
220-
regex += re.escape(s[start : pos - 1])
221-
# and then add the slash as escaped
222-
# plain text
223-
regex += re.escape(s[pos])
224-
# update start to show we have handled everything
225-
# before it
226-
start = pos + 1
227-
# and continue to look
207+
# check if the slash is preceded by a backslash
208+
elif s[pos - 1 : pos] == '\\':
209+
# it is.
210+
if in_regex:
211+
# add everything up to the backslash as a
212+
# regular expression
213+
regex += s[start : pos - 1]
214+
# skip the backslash, and add the slash
215+
regex += s[pos]
228216
else:
229-
# slash is not escaped, this is what we are looking for
230-
break
217+
# add everything up to the backslash as escaped
218+
# plain text
219+
regex += re.escape(s[start : pos - 1])
220+
# and then add the slash as escaped
221+
# plain text
222+
regex += re.escape(s[pos])
223+
# update start to show we have handled everything
224+
# before it
225+
start = pos + 1
226+
# and continue to look
227+
else:
228+
# slash is not escaped, this is what we are looking for
229+
break
231230
return regex, pos, start

cmd2/utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,11 +1168,10 @@ def do_echo(self, arglist):
11681168
if isinstance(func, Iterable):
11691169
for item in func:
11701170
setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category)
1171+
elif inspect.ismethod(func):
1172+
setattr(func.__func__, constants.CMD_ATTR_HELP_CATEGORY, category) # type: ignore[attr-defined]
11711173
else:
1172-
if inspect.ismethod(func):
1173-
setattr(func.__func__, constants.CMD_ATTR_HELP_CATEGORY, category) # type: ignore[attr-defined]
1174-
else:
1175-
setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)
1174+
setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)
11761175

11771176

11781177
def get_defining_class(meth: Callable[..., Any]) -> Optional[Type[Any]]:

0 commit comments

Comments
 (0)