Skip to content

Enable ruff RUF ruleset for Ruff-specific rules #1423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from .py_bridge import CommandResult
from .utils import CompletionMode, CustomCompletionSettings, Settable, categorize

__all__: list[str] = [
__all__: list[str] = [ # noqa: RUF022
'COMMAND_NAME',
'DEFAULT_SHORTCUTS',
# ANSI Exports
Expand Down
2 changes: 1 addition & 1 deletion cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def remove(self, command_method: CommandFunc) -> None:
class Cmd(cmd.Cmd):
"""An easy but powerful framework for writing line-oriented command interpreters.

Extends the Python Standard Librarys cmd package by adding a lot of useful features
Extends the Python Standard Library's cmd package by adding a lot of useful features
to the out of the box configuration.

Line-oriented command interpreters are often useful for test harnesses, internal tools, and rapid prototypes.
Expand Down
2 changes: 1 addition & 1 deletion cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ class ByteBuf:
"""Used by StdSim to write binary data and stores the actual bytes written."""

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

def __init__(self, std_sim_instance: StdSim) -> None:
self.byte_buf = bytearray()
Expand Down
6 changes: 3 additions & 3 deletions examples/cmd_as_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class CmdLineApp(cmd2.Cmd):

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

def __init__(self) -> None:
shortcuts = dict(cmd2.DEFAULT_SHORTCUTS)
Expand Down
6 changes: 3 additions & 3 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class CmdLineApp(cmd2.Cmd):

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

def __init__(self) -> None:
shortcuts = cmd2.DEFAULT_SHORTCUTS
Expand Down
2 changes: 1 addition & 1 deletion examples/help_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def wrapper(*args, **kwds):
class HelpCategories(cmd2.Cmd):
"""Example cmd2 application."""

START_TIMES = ['now', 'later', 'sometime', 'whenever']
START_TIMES = ('now', 'later', 'sometime', 'whenever')

# Command categories
CMD_CAT_CONNECTING = 'Connecting'
Expand Down
6 changes: 3 additions & 3 deletions examples/migrating.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
class CmdLineApp(cmd.Cmd):
"""Example cmd application."""

MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
MUMBLE_FIRST = ['so', 'like', 'well']
MUMBLE_LAST = ['right?']
MUMBLES = ('like', '...', 'um', 'er', 'hmmm', 'ahh')
MUMBLE_FIRST = ('so', 'like', 'well')
MUMBLE_LAST = ('right?',)

def do_exit(self, line) -> bool:
"""Exit the application."""
Expand Down
8 changes: 4 additions & 4 deletions examples/modular_commands/commandset_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
@with_default_category('Basic Completion')
class BasicCompletionCommandSet(CommandSet):
# List of strings used with completion functions
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
food_item_strs = ('Pizza', 'Ham', 'Ham Sandwich', 'Potato')
sport_item_strs = ('Bat', 'Basket', 'Basketball', 'Football', 'Space Ball')

# This data is used to demonstrate delimiter_complete
file_strs = [
file_strs = (
'/home/user/file.db',
'/home/user/file space.db',
'/home/user/another.db',
'/home/other user/maps.db',
'/home/other user/tests.db',
]
)

def do_flag_based(self, statement: Statement) -> None:
"""Tab completes arguments based on a preceding flag using flag_based_complete
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ select = [
"Q", # flake8-quotes (force double quotes)
"RET", # flake8-return (various warnings related to implicit vs explicit return statements)
"RSE", # flake8-raise (warn about unnecessary parentheses on raised exceptions)
# "RUF", # Ruff-specific rules (miscellaneous grab bag of lint checks specific to Ruff)
"RUF", # Ruff-specific rules (miscellaneous grab bag of lint checks specific to Ruff)
"S", # flake8-bandit (security oriented checks, but extremely pedantic - do not attempt to apply to unit test files)
"SIM", # flake8-simplify (rules to attempt to simplify code)
# "SLF", # flake8-self (warn when protected members are accessed outside of a class or file)
Expand Down
44 changes: 21 additions & 23 deletions tests/test_argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ def do_pos_and_flag(self, args: argparse.Namespace) -> None:
TUPLE_METAVAR = ('arg1', 'others')
CUSTOM_DESC_HEADER = "Custom Header"

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

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

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

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

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

nargs_parser = Cmd2ArgumentParser()

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

# Numbers will be sorted in ascending order and then converted to strings by ArgparseCompleter
if completions and all(isinstance(x, numbers.Number) for x in completions):
completions.sort()
completions = [str(x) for x in completions]
completions = [str(x) for x in sorted(completions)]
else:
completions.sort(key=ac_app.default_sort_key)
completions = sorted(completions, key=ac_app.default_sort_key)

assert ac_app.completion_matches == completions

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

# Numbers will be sorted in ascending order and then converted to strings by ArgparseCompleter
if completions and all(isinstance(x, numbers.Number) for x in completions):
completions.sort()
completions = [str(x) for x in completions]
completions = [str(x) for x in sorted(completions)]
else:
completions.sort(key=ac_app.default_sort_key)
completions = sorted(completions, key=ac_app.default_sort_key)

assert ac_app.completion_matches == completions

Expand Down
6 changes: 3 additions & 3 deletions tests/test_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@


class CmdLineApp(cmd2.Cmd):
MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
MUMBLE_FIRST = ['so', 'like', 'well']
MUMBLE_LAST = ['right?']
MUMBLES = ('like', '...', 'um', 'er', 'hmmm', 'ahh')
MUMBLE_FIRST = ('so', 'like', 'well')
MUMBLE_LAST = ('right?',)

def __init__(self, *args, **kwargs) -> None:
self.maxrepeats = 3
Expand Down
8 changes: 4 additions & 4 deletions tests_isolated/test_commandset/test_commandset.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ def test_static_subcommands(static_subcommands_app) -> None:
class SupportFuncProvider(cmd2.CommandSet):
"""CommandSet which provides a support function (complete_states) to other CommandSets"""

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

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

assert first_match == 'alabama'
assert command_sets_manual.completion_matches == SupportFuncProvider.states
assert command_sets_manual.completion_matches == list(SupportFuncProvider.states)

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

assert first_match == 'alabama'
assert command_sets_manual.completion_matches == SupportFuncProvider.states
assert command_sets_manual.completion_matches == list(SupportFuncProvider.states)

command_sets_manual.unregister_command_set(user_unrelated)
command_sets_manual.unregister_command_set(func_provider)
Expand All @@ -911,7 +911,7 @@ def test_cross_commandset_completer(command_sets_manual, capsys) -> None:
complete_states_expected_self = None

assert first_match == 'alabama'
assert command_sets_manual.completion_matches == SupportFuncProvider.states
assert command_sets_manual.completion_matches == list(SupportFuncProvider.states)

command_sets_manual.unregister_command_set(user_unrelated)
command_sets_manual.unregister_command_set(user_sub1)
Expand Down
Loading