Skip to content

Commit 0f102d5

Browse files
authored
Enable ruff RUF ruleset for Ruff-specific rules (#1423)
Mostly this involved replacing mutable class variables with immutable class variables - i.e. class-level member lists were replaced with tuples.
1 parent 56fe9ec commit 0f102d5

File tree

12 files changed

+46
-48
lines changed

12 files changed

+46
-48
lines changed

cmd2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from .py_bridge import CommandResult
5353
from .utils import CompletionMode, CustomCompletionSettings, Settable, categorize
5454

55-
__all__: list[str] = [
55+
__all__: list[str] = [ # noqa: RUF022
5656
'COMMAND_NAME',
5757
'DEFAULT_SHORTCUTS',
5858
# ANSI Exports

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def remove(self, command_method: CommandFunc) -> None:
281281
class Cmd(cmd.Cmd):
282282
"""An easy but powerful framework for writing line-oriented command interpreters.
283283
284-
Extends the Python Standard Librarys cmd package by adding a lot of useful features
284+
Extends the Python Standard Library's cmd package by adding a lot of useful features
285285
to the out of the box configuration.
286286
287287
Line-oriented command interpreters are often useful for test harnesses, internal tools, and rapid prototypes.

cmd2/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ class ByteBuf:
531531
"""Used by StdSim to write binary data and stores the actual bytes written."""
532532

533533
# Used to know when to flush the StdSim
534-
NEWLINES = [b'\n', b'\r']
534+
NEWLINES = (b'\n', b'\r')
535535

536536
def __init__(self, std_sim_instance: StdSim) -> None:
537537
self.byte_buf = bytearray()

examples/cmd_as_argument.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class CmdLineApp(cmd2.Cmd):
2121

2222
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
2323
# default_to_shell = True # noqa: ERA001
24-
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
25-
MUMBLE_FIRST = ['so', 'like', 'well']
26-
MUMBLE_LAST = ['right?']
24+
MUMBLES = ('like', '...', 'um', 'er', 'hmmm', 'ahh')
25+
MUMBLE_FIRST = ('so', 'like', 'well')
26+
MUMBLE_LAST = ('right?',)
2727

2828
def __init__(self) -> None:
2929
shortcuts = dict(cmd2.DEFAULT_SHORTCUTS)

examples/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class CmdLineApp(cmd2.Cmd):
1919

2020
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
2121
# default_to_shell = True # noqa: ERA001
22-
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
23-
MUMBLE_FIRST = ['so', 'like', 'well']
24-
MUMBLE_LAST = ['right?']
22+
MUMBLES = ('like', '...', 'um', 'er', 'hmmm', 'ahh')
23+
MUMBLE_FIRST = ('so', 'like', 'well')
24+
MUMBLE_LAST = ('right?',)
2525

2626
def __init__(self) -> None:
2727
shortcuts = cmd2.DEFAULT_SHORTCUTS

examples/help_categories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def wrapper(*args, **kwds):
2525
class HelpCategories(cmd2.Cmd):
2626
"""Example cmd2 application."""
2727

28-
START_TIMES = ['now', 'later', 'sometime', 'whenever']
28+
START_TIMES = ('now', 'later', 'sometime', 'whenever')
2929

3030
# Command categories
3131
CMD_CAT_CONNECTING = 'Connecting'

examples/migrating.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
class CmdLineApp(cmd.Cmd):
1010
"""Example cmd application."""
1111

12-
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
13-
MUMBLE_FIRST = ['so', 'like', 'well']
14-
MUMBLE_LAST = ['right?']
12+
MUMBLES = ('like', '...', 'um', 'er', 'hmmm', 'ahh')
13+
MUMBLE_FIRST = ('so', 'like', 'well')
14+
MUMBLE_LAST = ('right?',)
1515

1616
def do_exit(self, line) -> bool:
1717
"""Exit the application."""

examples/modular_commands/commandset_basic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
@with_default_category('Basic Completion')
1313
class BasicCompletionCommandSet(CommandSet):
1414
# List of strings used with completion functions
15-
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
16-
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
15+
food_item_strs = ('Pizza', 'Ham', 'Ham Sandwich', 'Potato')
16+
sport_item_strs = ('Bat', 'Basket', 'Basketball', 'Football', 'Space Ball')
1717

1818
# This data is used to demonstrate delimiter_complete
19-
file_strs = [
19+
file_strs = (
2020
'/home/user/file.db',
2121
'/home/user/file space.db',
2222
'/home/user/another.db',
2323
'/home/other user/maps.db',
2424
'/home/other user/tests.db',
25-
]
25+
)
2626

2727
def do_flag_based(self, statement: Statement) -> None:
2828
"""Tab completes arguments based on a preceding flag using flag_based_complete

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ select = [
205205
"Q", # flake8-quotes (force double quotes)
206206
"RET", # flake8-return (various warnings related to implicit vs explicit return statements)
207207
"RSE", # flake8-raise (warn about unnecessary parentheses on raised exceptions)
208-
# "RUF", # Ruff-specific rules (miscellaneous grab bag of lint checks specific to Ruff)
208+
"RUF", # Ruff-specific rules (miscellaneous grab bag of lint checks specific to Ruff)
209209
"S", # flake8-bandit (security oriented checks, but extremely pedantic - do not attempt to apply to unit test files)
210210
"SIM", # flake8-simplify (rules to attempt to simplify code)
211211
# "SLF", # flake8-self (warn when protected members are accessed outside of a class or file)

tests/test_argparse_completer.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ def do_pos_and_flag(self, args: argparse.Namespace) -> None:
107107
TUPLE_METAVAR = ('arg1', 'others')
108108
CUSTOM_DESC_HEADER = "Custom Header"
109109

110-
# lists used in our tests (there is a mix of sorted and unsorted on purpose)
111-
non_negative_num_choices = [1, 2, 3, 0.5, 22]
112-
num_choices = [-1, 1, -2, 2.5, 0, -12]
113-
static_choices_list = ['static', 'choices', 'stop', 'here']
114-
choices_from_provider = ['choices', 'provider', 'probably', 'improved']
115-
completion_item_choices = [CompletionItem('choice_1', 'A description'), CompletionItem('choice_2', 'Another description')]
110+
# tuples (for sake of immutability) used in our tests (there is a mix of sorted and unsorted on purpose)
111+
non_negative_num_choices = (1, 2, 3, 0.5, 22)
112+
num_choices = (-1, 1, -2, 2.5, 0, -12)
113+
static_choices_list = ('static', 'choices', 'stop', 'here')
114+
choices_from_provider = ('choices', 'provider', 'probably', 'improved')
115+
completion_item_choices = (CompletionItem('choice_1', 'A description'), CompletionItem('choice_2', 'Another description'))
116116

117117
# This tests that CompletionItems created with numerical values are sorted as numbers.
118-
num_completion_items = [CompletionItem(5, "Five"), CompletionItem(1.5, "One.Five"), CompletionItem(2, "Five")]
118+
num_completion_items = (CompletionItem(5, "Five"), CompletionItem(1.5, "One.Five"), CompletionItem(2, "Five"))
119119

120-
def choices_provider(self) -> list[str]:
120+
def choices_provider(self) -> tuple[str]:
121121
"""Method that provides choices"""
122122
return self.choices_from_provider
123123

@@ -179,9 +179,9 @@ def do_choices(self, args: argparse.Namespace) -> None:
179179
############################################################################################################
180180
# Begin code related to testing completer parameter
181181
############################################################################################################
182-
completions_for_flag = ['completions', 'flag', 'fairly', 'complete']
183-
completions_for_pos_1 = ['completions', 'positional_1', 'probably', 'missed', 'spot']
184-
completions_for_pos_2 = ['completions', 'positional_2', 'probably', 'missed', 'me']
182+
completions_for_flag = ('completions', 'flag', 'fairly', 'complete')
183+
completions_for_pos_1 = ('completions', 'positional_1', 'probably', 'missed', 'spot')
184+
completions_for_pos_2 = ('completions', 'positional_2', 'probably', 'missed', 'me')
185185

186186
def flag_completer(self, text: str, line: str, begidx: int, endidx: int) -> list[str]:
187187
return self.basic_complete(text, line, begidx, endidx, self.completions_for_flag)
@@ -208,12 +208,12 @@ def do_completer(self, args: argparse.Namespace) -> None:
208208
############################################################################################################
209209
# Begin code related to nargs
210210
############################################################################################################
211-
set_value_choices = ['set', 'value', 'choices']
212-
one_or_more_choices = ['one', 'or', 'more', 'choices']
213-
optional_choices = ['a', 'few', 'optional', 'choices']
214-
range_choices = ['some', 'range', 'choices']
215-
remainder_choices = ['remainder', 'choices']
216-
positional_choices = ['the', 'positional', 'choices']
211+
set_value_choices = ('set', 'value', 'choices')
212+
one_or_more_choices = ('one', 'or', 'more', 'choices')
213+
optional_choices = ('a', 'few', 'optional', 'choices')
214+
range_choices = ('some', 'range', 'choices')
215+
remainder_choices = ('remainder', 'choices')
216+
positional_choices = ('the', 'positional', 'choices')
217217

218218
nargs_parser = Cmd2ArgumentParser()
219219

@@ -572,10 +572,9 @@ def test_autocomp_flag_choices_completion(ac_app, flag, text, completions) -> No
572572

573573
# Numbers will be sorted in ascending order and then converted to strings by ArgparseCompleter
574574
if completions and all(isinstance(x, numbers.Number) for x in completions):
575-
completions.sort()
576-
completions = [str(x) for x in completions]
575+
completions = [str(x) for x in sorted(completions)]
577576
else:
578-
completions.sort(key=ac_app.default_sort_key)
577+
completions = sorted(completions, key=ac_app.default_sort_key)
579578

580579
assert ac_app.completion_matches == completions
581580

@@ -606,10 +605,9 @@ def test_autocomp_positional_choices_completion(ac_app, pos, text, completions)
606605

607606
# Numbers will be sorted in ascending order and then converted to strings by ArgparseCompleter
608607
if completions and all(isinstance(x, numbers.Number) for x in completions):
609-
completions.sort()
610-
completions = [str(x) for x in completions]
608+
completions = [str(x) for x in sorted(completions)]
611609
else:
612-
completions.sort(key=ac_app.default_sort_key)
610+
completions = sorted(completions, key=ac_app.default_sort_key)
613611

614612
assert ac_app.completion_matches == completions
615613

tests/test_transcript.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929

3030
class CmdLineApp(cmd2.Cmd):
31-
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
32-
MUMBLE_FIRST = ['so', 'like', 'well']
33-
MUMBLE_LAST = ['right?']
31+
MUMBLES = ('like', '...', 'um', 'er', 'hmmm', 'ahh')
32+
MUMBLE_FIRST = ('so', 'like', 'well')
33+
MUMBLE_LAST = ('right?',)
3434

3535
def __init__(self, *args, **kwargs) -> None:
3636
self.maxrepeats = 3

tests_isolated/test_commandset/test_commandset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ def test_static_subcommands(static_subcommands_app) -> None:
783783
class SupportFuncProvider(cmd2.CommandSet):
784784
"""CommandSet which provides a support function (complete_states) to other CommandSets"""
785785

786-
states = ['alabama', 'alaska', 'arizona', 'arkansas', 'california', 'colorado', 'connecticut', 'delaware']
786+
states = ('alabama', 'alaska', 'arizona', 'arkansas', 'california', 'colorado', 'connecticut', 'delaware')
787787

788788
def __init__(self, dummy) -> None:
789789
"""Dummy variable prevents this from being autoloaded in other tests"""
@@ -862,7 +862,7 @@ def test_cross_commandset_completer(command_sets_manual, capsys) -> None:
862862
complete_states_expected_self = None
863863

864864
assert first_match == 'alabama'
865-
assert command_sets_manual.completion_matches == SupportFuncProvider.states
865+
assert command_sets_manual.completion_matches == list(SupportFuncProvider.states)
866866

867867
assert (
868868
getattr(command_sets_manual.cmd_func('user_sub1').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY) == 'With Completer'
@@ -888,7 +888,7 @@ def test_cross_commandset_completer(command_sets_manual, capsys) -> None:
888888
complete_states_expected_self = None
889889

890890
assert first_match == 'alabama'
891-
assert command_sets_manual.completion_matches == SupportFuncProvider.states
891+
assert command_sets_manual.completion_matches == list(SupportFuncProvider.states)
892892

893893
command_sets_manual.unregister_command_set(user_unrelated)
894894
command_sets_manual.unregister_command_set(func_provider)
@@ -911,7 +911,7 @@ def test_cross_commandset_completer(command_sets_manual, capsys) -> None:
911911
complete_states_expected_self = None
912912

913913
assert first_match == 'alabama'
914-
assert command_sets_manual.completion_matches == SupportFuncProvider.states
914+
assert command_sets_manual.completion_matches == list(SupportFuncProvider.states)
915915

916916
command_sets_manual.unregister_command_set(user_unrelated)
917917
command_sets_manual.unregister_command_set(user_sub1)

0 commit comments

Comments
 (0)