Skip to content

Commit 9007cc7

Browse files
committed
Apply refactorings from ruff UP ruleset that are safe for Python 3.9+
1 parent 6111972 commit 9007cc7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+281
-425
lines changed

cmd2/ansi.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
"""
32
Support for ANSI escape sequences which are used for things like applying style to text,
43
setting the window title, and asynchronous alerts.
@@ -12,7 +11,6 @@
1211
from typing import (
1312
IO,
1413
Any,
15-
List,
1614
Optional,
1715
cast,
1816
)
@@ -993,10 +991,10 @@ def style(
993991
:return: the stylized string
994992
"""
995993
# List of strings that add style
996-
additions: List[AnsiSequence] = []
994+
additions: list[AnsiSequence] = []
997995

998996
# List of strings that remove style
999-
removals: List[AnsiSequence] = []
997+
removals: list[AnsiSequence] = []
1000998

1001999
# Process the style settings
10021000
if fg is not None:

cmd2/argparse_custom.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
"""
32
This module adds capabilities to argparse by patching a few of its functions.
43
It also defines a parser class called Cmd2ArgumentParser which improves error
@@ -230,24 +229,17 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
230229
ZERO_OR_MORE,
231230
ArgumentError,
232231
)
232+
from collections.abc import Callable, Iterable, Sequence
233233
from gettext import (
234234
gettext,
235235
)
236236
from typing import (
237237
IO,
238238
TYPE_CHECKING,
239239
Any,
240-
Callable,
241-
Dict,
242-
Iterable,
243-
List,
244240
NoReturn,
245241
Optional,
246242
Protocol,
247-
Sequence,
248-
Set,
249-
Tuple,
250-
Type,
251243
Union,
252244
cast,
253245
runtime_checkable,
@@ -325,7 +317,7 @@ class ChoicesProviderFuncBase(Protocol):
325317
Function that returns a list of choices in support of tab completion
326318
"""
327319

328-
def __call__(self) -> List[str]: ... # pragma: no cover
320+
def __call__(self) -> list[str]: ... # pragma: no cover
329321

330322

331323
@runtime_checkable
@@ -334,7 +326,7 @@ class ChoicesProviderFuncWithTokens(Protocol):
334326
Function that returns a list of choices in support of tab completion and accepts a dictionary of prior arguments.
335327
"""
336328

337-
def __call__(self, *, arg_tokens: Dict[str, List[str]] = {}) -> List[str]: ... # pragma: no cover
329+
def __call__(self, *, arg_tokens: dict[str, list[str]] = {}) -> list[str]: ... # pragma: no cover
338330

339331

340332
ChoicesProviderFunc = Union[ChoicesProviderFuncBase, ChoicesProviderFuncWithTokens]
@@ -352,7 +344,7 @@ def __call__(
352344
line: str,
353345
begidx: int,
354346
endidx: int,
355-
) -> List[str]: ... # pragma: no cover
347+
) -> list[str]: ... # pragma: no cover
356348

357349

358350
@runtime_checkable
@@ -369,8 +361,8 @@ def __call__(
369361
begidx: int,
370362
endidx: int,
371363
*,
372-
arg_tokens: Dict[str, List[str]] = {},
373-
) -> List[str]: ... # pragma: no cover
364+
arg_tokens: dict[str, list[str]] = {},
365+
) -> list[str]: ... # pragma: no cover
374366

375367

376368
CompleterFunc = Union[CompleterFuncBase, CompleterFuncWithTokens]
@@ -571,7 +563,7 @@ def _action_set_descriptive_header(self: argparse.Action, descriptive_header: Op
571563
############################################################################################################
572564
# Patch argparse.Action with accessors for nargs_range attribute
573565
############################################################################################################
574-
def _action_get_nargs_range(self: argparse.Action) -> Optional[Tuple[int, Union[int, float]]]:
566+
def _action_get_nargs_range(self: argparse.Action) -> Optional[tuple[int, Union[int, float]]]:
575567
"""
576568
Get the nargs_range attribute of an argparse Action.
577569
@@ -582,13 +574,13 @@ def _action_get_nargs_range(self: argparse.Action) -> Optional[Tuple[int, Union[
582574
:param self: argparse Action being queried
583575
:return: The value of nargs_range or None if attribute does not exist
584576
"""
585-
return cast("Optional[Tuple[int, Union[int, float]]]", getattr(self, ATTR_NARGS_RANGE, None))
577+
return cast("Optional[tuple[int, Union[int, float]]]", getattr(self, ATTR_NARGS_RANGE, None))
586578

587579

588580
setattr(argparse.Action, 'get_nargs_range', _action_get_nargs_range)
589581

590582

591-
def _action_set_nargs_range(self: argparse.Action, nargs_range: Optional[Tuple[int, Union[int, float]]]) -> None:
583+
def _action_set_nargs_range(self: argparse.Action, nargs_range: Optional[tuple[int, Union[int, float]]]) -> None:
592584
"""
593585
Set the nargs_range attribute of an argparse Action.
594586
@@ -646,11 +638,11 @@ def _action_set_suppress_tab_hint(self: argparse.Action, suppress_tab_hint: bool
646638
# Allow developers to add custom action attributes
647639
############################################################################################################
648640

649-
CUSTOM_ACTION_ATTRIBS: Set[str] = set()
641+
CUSTOM_ACTION_ATTRIBS: set[str] = set()
650642
_CUSTOM_ATTRIB_PFX = '_attr_'
651643

652644

653-
def register_argparse_argument_parameter(param_name: str, param_type: Optional[Type[Any]]) -> None:
645+
def register_argparse_argument_parameter(param_name: str, param_type: Optional[type[Any]]) -> None:
654646
"""
655647
Registers a custom argparse argument parameter.
656648
@@ -719,7 +711,7 @@ def _action_set_custom_parameter(self: argparse.Action, value: Any) -> None:
719711
def _add_argument_wrapper(
720712
self: argparse._ActionsContainer,
721713
*args: Any,
722-
nargs: Union[int, str, Tuple[int], Tuple[int, int], Tuple[int, float], None] = None,
714+
nargs: Union[int, str, tuple[int], tuple[int, int], tuple[int, float], None] = None,
723715
choices_provider: Optional[ChoicesProviderFunc] = None,
724716
completer: Optional[CompleterFunc] = None,
725717
suppress_tab_hint: bool = False,
@@ -770,7 +762,7 @@ def _add_argument_wrapper(
770762
nargs_range = None
771763

772764
if nargs is not None:
773-
nargs_adjusted: Union[int, str, Tuple[int], Tuple[int, int], Tuple[int, float], None]
765+
nargs_adjusted: Union[int, str, tuple[int], tuple[int, int], tuple[int, float], None]
774766
# Check if nargs was given as a range
775767
if isinstance(nargs, tuple):
776768
# Handle 1-item tuple by setting max to INFINITY
@@ -820,7 +812,7 @@ def _add_argument_wrapper(
820812
kwargs['nargs'] = nargs_adjusted
821813

822814
# Extract registered custom keyword arguments
823-
custom_attribs: Dict[str, Any] = {}
815+
custom_attribs: dict[str, Any] = {}
824816
for keyword, value in kwargs.items():
825817
if keyword in CUSTOM_ACTION_ATTRIBS:
826818
custom_attribs[keyword] = value
@@ -918,7 +910,7 @@ def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Acti
918910
ATTR_AP_COMPLETER_TYPE = 'ap_completer_type'
919911

920912

921-
def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> Optional[Type['ArgparseCompleter']]:
913+
def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> Optional[type['ArgparseCompleter']]:
922914
"""
923915
Get the ap_completer_type attribute of an argparse ArgumentParser.
924916
@@ -929,13 +921,13 @@ def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> Opti
929921
:param self: ArgumentParser being queried
930922
:return: An ArgparseCompleter-based class or None if attribute does not exist
931923
"""
932-
return cast("Optional[Type[ArgparseCompleter]]", getattr(self, ATTR_AP_COMPLETER_TYPE, None))
924+
return cast("Optional[type[ArgparseCompleter]]", getattr(self, ATTR_AP_COMPLETER_TYPE, None))
933925

934926

935927
setattr(argparse.ArgumentParser, 'get_ap_completer_type', _ArgumentParser_get_ap_completer_type)
936928

937929

938-
def _ArgumentParser_set_ap_completer_type(self: argparse.ArgumentParser, ap_completer_type: Type['ArgparseCompleter']) -> None:
930+
def _ArgumentParser_set_ap_completer_type(self: argparse.ArgumentParser, ap_completer_type: type['ArgparseCompleter']) -> None:
939931
"""
940932
Set the ap_completer_type attribute of an argparse ArgumentParser.
941933
@@ -1092,9 +1084,9 @@ def _format_usage(
10921084
# End cmd2 customization
10931085

10941086
# helper for wrapping lines
1095-
def get_lines(parts: List[str], indent: str, prefix: Optional[str] = None) -> List[str]:
1096-
lines: List[str] = []
1097-
line: List[str] = []
1087+
def get_lines(parts: list[str], indent: str, prefix: Optional[str] = None) -> list[str]:
1088+
lines: list[str] = []
1089+
line: list[str] = []
10981090
if prefix is not None:
10991091
line_len = len(prefix) - 1
11001092
else:
@@ -1155,7 +1147,7 @@ def _format_action_invocation(self, action: argparse.Action) -> str:
11551147
(metavar,) = self._metavar_formatter(action, default)(1)
11561148
return metavar
11571149

1158-
parts: List[str] = []
1150+
parts: list[str] = []
11591151

11601152
# if the Optional doesn't take a value, format is:
11611153
# -s, --long
@@ -1175,8 +1167,8 @@ def _format_action_invocation(self, action: argparse.Action) -> str:
11751167
def _determine_metavar(
11761168
self,
11771169
action: argparse.Action,
1178-
default_metavar: Union[str, Tuple[str, ...]],
1179-
) -> Union[str, Tuple[str, ...]]:
1170+
default_metavar: Union[str, tuple[str, ...]],
1171+
) -> Union[str, tuple[str, ...]]:
11801172
"""Custom method to determine what to use as the metavar value of an action"""
11811173
if action.metavar is not None:
11821174
result = action.metavar
@@ -1192,18 +1184,18 @@ def _determine_metavar(
11921184
def _metavar_formatter(
11931185
self,
11941186
action: argparse.Action,
1195-
default_metavar: Union[str, Tuple[str, ...]],
1196-
) -> Callable[[int], Tuple[str, ...]]:
1187+
default_metavar: Union[str, tuple[str, ...]],
1188+
) -> Callable[[int], tuple[str, ...]]:
11971189
metavar = self._determine_metavar(action, default_metavar)
11981190

1199-
def format_tuple(tuple_size: int) -> Tuple[str, ...]:
1191+
def format_tuple(tuple_size: int) -> tuple[str, ...]:
12001192
if isinstance(metavar, tuple):
12011193
return metavar
12021194
return (metavar,) * tuple_size
12031195

12041196
return format_tuple
12051197

1206-
def _format_args(self, action: argparse.Action, default_metavar: Union[str, Tuple[str, ...]]) -> str:
1198+
def _format_args(self, action: argparse.Action, default_metavar: Union[str, tuple[str, ...]]) -> str:
12071199
"""Customized to handle ranged nargs and make other output less verbose"""
12081200
metavar = self._determine_metavar(action, default_metavar)
12091201
metavar_formatter = self._metavar_formatter(action, default_metavar)
@@ -1241,7 +1233,7 @@ def __init__(
12411233
description: Optional[str] = None,
12421234
epilog: Optional[str] = None,
12431235
parents: Sequence[argparse.ArgumentParser] = (),
1244-
formatter_class: Type[argparse.HelpFormatter] = Cmd2HelpFormatter,
1236+
formatter_class: type[argparse.HelpFormatter] = Cmd2HelpFormatter,
12451237
prefix_chars: str = '-',
12461238
fromfile_prefix_chars: Optional[str] = None,
12471239
argument_default: Optional[str] = None,
@@ -1252,7 +1244,7 @@ def __init__(
12521244
suggest_on_error: bool = False,
12531245
color: bool = False,
12541246
*,
1255-
ap_completer_type: Optional[Type['ArgparseCompleter']] = None,
1247+
ap_completer_type: Optional[type['ArgparseCompleter']] = None,
12561248
) -> None:
12571249
"""
12581250
# Custom parameter added by cmd2
@@ -1410,10 +1402,10 @@ def set(self, new_val: Any) -> None:
14101402

14111403

14121404
# The default ArgumentParser class for a cmd2 app
1413-
DEFAULT_ARGUMENT_PARSER: Type[argparse.ArgumentParser] = Cmd2ArgumentParser
1405+
DEFAULT_ARGUMENT_PARSER: type[argparse.ArgumentParser] = Cmd2ArgumentParser
14141406

14151407

1416-
def set_default_argument_parser_type(parser_type: Type[argparse.ArgumentParser]) -> None:
1408+
def set_default_argument_parser_type(parser_type: type[argparse.ArgumentParser]) -> None:
14171409
"""
14181410
Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if
14191411
you want to override the parser for cmd2's built-in commands. See examples/override_parser.py.

cmd2/clipboard.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
"""
32
This module provides basic ability to copy from and paste to the clipboard/pastebuffer.
43
"""

0 commit comments

Comments
 (0)