Skip to content

Commit b697356

Browse files
authored
Enable ruff ARG ruleset to warn about unused arguments (#1425)
1 parent e68746b commit b697356

20 files changed

+49
-41
lines changed

cmd2/argparse_custom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class CompletionItem(str): # noqa: SLOT000
280280
See header of this file for more information
281281
"""
282282

283-
def __new__(cls, value: object, *args: Any, **kwargs: Any) -> 'CompletionItem':
283+
def __new__(cls, value: object, *_args: Any, **_kwargs: Any) -> 'CompletionItem':
284284
return super().__new__(cls, value)
285285

286286
def __init__(self, value: object, description: str = '', *args: Any) -> None:
@@ -914,7 +914,7 @@ def _ArgumentParser_set_ap_completer_type(self: argparse.ArgumentParser, ap_comp
914914
############################################################################################################
915915
# Patch ArgumentParser._check_value to support CompletionItems as choices
916916
############################################################################################################
917-
def _ArgumentParser_check_value(self: argparse.ArgumentParser, action: argparse.Action, value: Any) -> None: # noqa: N802
917+
def _ArgumentParser_check_value(_self: argparse.ArgumentParser, action: argparse.Action, value: Any) -> None: # noqa: N802
918918
"""Custom override of ArgumentParser._check_value that supports CompletionItems as choices.
919919
When evaluating choices, input is compared to CompletionItem.orig_value instead of the
920920
CompletionItem instance.

cmd2/cmd2.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ def remove_settable(self, name: str) -> None:
11111111
def build_settables(self) -> None:
11121112
"""Create the dictionary of user-settable parameters."""
11131113

1114-
def get_allow_style_choices(cli_self: Cmd) -> list[str]:
1114+
def get_allow_style_choices(_cli_self: Cmd) -> list[str]:
11151115
"""Used to tab complete allow_style values."""
11161116
return [val.name.lower() for val in ansi.AllowStyle]
11171117

@@ -1419,9 +1419,9 @@ def tokens_for_completion(self, line: str, begidx: int, endidx: int) -> tuple[li
14191419
def basic_complete(
14201420
self,
14211421
text: str,
1422-
line: str,
1423-
begidx: int,
1424-
endidx: int,
1422+
line: str, # noqa: ARG002
1423+
begidx: int, # noqa: ARG002
1424+
endidx: int, # noqa: ARG002
14251425
match_against: Iterable[str],
14261426
) -> list[str]:
14271427
"""Basic tab completion function that matches against a list of strings without considering line contents
@@ -1602,7 +1602,13 @@ def index_based_complete(
16021602
return matches
16031603

16041604
def path_complete(
1605-
self, text: str, line: str, begidx: int, endidx: int, *, path_filter: Optional[Callable[[str], bool]] = None
1605+
self,
1606+
text: str,
1607+
line: str,
1608+
begidx: int, # noqa: ARG002
1609+
endidx: int,
1610+
*,
1611+
path_filter: Optional[Callable[[str], bool]] = None,
16061612
) -> list[str]:
16071613
"""Performs completion of local file system paths.
16081614
@@ -2346,7 +2352,7 @@ def get_help_topics(self) -> list[str]:
23462352
# Filter out hidden and disabled commands
23472353
return [topic for topic in all_topics if topic not in self.hidden_commands and topic not in self.disabled_commands]
23482354

2349-
def sigint_handler(self, signum: int, _: Optional[FrameType]) -> None:
2355+
def sigint_handler(self, signum: int, _: Optional[FrameType]) -> None: # noqa: ARG002
23502356
"""Signal handler for SIGINTs which typically come from Ctrl-C events.
23512357
23522358
If you need custom SIGINT behavior, then override this method.
@@ -2402,7 +2408,7 @@ def precmd(self, statement: Union[Statement, str]) -> Statement:
24022408
"""
24032409
return Statement(statement) if not isinstance(statement, Statement) else statement
24042410

2405-
def postcmd(self, stop: bool, statement: Union[Statement, str]) -> bool:
2411+
def postcmd(self, stop: bool, statement: Union[Statement, str]) -> bool: # noqa: ARG002
24062412
"""Hook method executed just after a command is executed by
24072413
[cmd2.Cmd.onecmd][].
24082414
@@ -3087,7 +3093,7 @@ def configure_readline() -> None:
30873093
# Disable completion
30883094
if completion_mode == utils.CompletionMode.NONE:
30893095

3090-
def complete_none(text: str, state: int) -> Optional[str]: # pragma: no cover
3096+
def complete_none(text: str, state: int) -> Optional[str]: # pragma: no cover # noqa: ARG001
30913097
return None
30923098

30933099
complete_func = complete_none
@@ -3773,7 +3779,7 @@ def do_help(self, args: argparse.Namespace) -> None:
37733779
self.perror(err_msg, apply_style=False)
37743780
self.last_result = False
37753781

3776-
def print_topics(self, header: str, cmds: Optional[list[str]], cmdlen: int, maxcol: int) -> None:
3782+
def print_topics(self, header: str, cmds: Optional[list[str]], cmdlen: int, maxcol: int) -> None: # noqa: ARG002
37773783
"""Print groups of commands and topics in columns and an optional header
37783784
Override of cmd's print_topics() to handle headers with newlines, ANSI style sequences, and wide characters.
37793785
@@ -5379,7 +5385,7 @@ def disable_command(self, command: str, message_to_print: str) -> None:
53795385
setattr(self, help_func_name, new_func)
53805386

53815387
# Set the completer to a function that returns a blank list
5382-
setattr(self, completer_func_name, lambda *args, **kwargs: [])
5388+
setattr(self, completer_func_name, lambda *_args, **_kwargs: [])
53835389

53845390
def disable_category(self, category: str, message_to_print: str) -> None:
53855391
"""Disable an entire category of commands.

cmd2/parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Statement(str): # type: ignore[override] # noqa: SLOT000
145145
# Used in JSON dictionaries
146146
_args_field = 'args'
147147

148-
def __new__(cls, value: object, *pos_args: Any, **kw_args: Any) -> 'Statement':
148+
def __new__(cls, value: object, *_pos_args: Any, **_kw_args: Any) -> 'Statement':
149149
"""Create a new instance of Statement.
150150
151151
We must override __new__ because we are subclassing `str` which is

examples/basic_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def do_raise_error(self, statement: cmd2.Statement) -> None:
8282
"""Demonstrates effect of raising CompletionError."""
8383
self.poutput(f"Args: {statement.args}")
8484

85-
def complete_raise_error(self, text, line, begidx, endidx) -> list[str]:
85+
def complete_raise_error(self, _text, _line, _begidx, _endidx) -> list[str]:
8686
"""CompletionErrors can be raised if an error occurs while tab completing.
8787
8888
Example use cases

examples/default_categories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ExampleApp(cmd2.Cmd):
6767
def __init__(self) -> None:
6868
super().__init__()
6969

70-
def do_something(self, arg) -> None:
70+
def do_something(self, _arg) -> None:
7171
self.poutput('this is the something command')
7272

7373

examples/dynamic_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self) -> None:
3434

3535
super().__init__(include_ipy=True)
3636

37-
def send_text(self, args: cmd2.Statement, *, text: str) -> None:
37+
def send_text(self, _args: cmd2.Statement, *, text: str) -> None:
3838
"""Simulate sending text to a server and printing the response."""
3939
self.poutput(text.capitalize())
4040

examples/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self) -> None:
1616
)
1717
self.add_settable(cmd2.Settable('sunny', bool, 'Is it sunny outside?', self))
1818

19-
def do_sunbathe(self, arg) -> None:
19+
def do_sunbathe(self, _arg) -> None:
2020
"""Attempt to sunbathe."""
2121
if self.degrees_c < 20:
2222
result = f"It's {self.degrees_c} C - are you a penguin?"
@@ -26,7 +26,7 @@ def do_sunbathe(self, arg) -> None:
2626
result = 'UV is bad for your skin.'
2727
self.poutput(result)
2828

29-
def _onchange_degrees_c(self, param_name, old, new) -> None:
29+
def _onchange_degrees_c(self, _param_name, _old, new) -> None:
3030
# if it's over 40C, it's gotta be sunny, right?
3131
if new > 40:
3232
self.sunny = True

examples/migrating.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CmdLineApp(cmd.Cmd):
1313
MUMBLE_FIRST = ('so', 'like', 'well')
1414
MUMBLE_LAST = ('right?',)
1515

16-
def do_exit(self, line) -> bool:
16+
def do_exit(self, _line) -> bool:
1717
"""Exit the application."""
1818
return True
1919

examples/modular_commands/commandset_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def do_raise_error(self, statement: Statement) -> None:
7373
"""Demonstrates effect of raising CompletionError."""
7474
self._cmd.poutput(f"Args: {statement.args}")
7575

76-
def complete_raise_error(self, text: str, line: str, begidx: int, endidx: int) -> list[str]:
76+
def complete_raise_error(self, _text: str, _line: str, _begidx: int, _endidx: int) -> list[str]:
7777
"""CompletionErrors can be raised if an error occurs while tab completing.
7878
7979
Example use cases
@@ -83,5 +83,5 @@ def complete_raise_error(self, text: str, line: str, begidx: int, endidx: int) -
8383
raise CompletionError("This is how a CompletionError behaves")
8484

8585
@with_category('Not Basic Completion')
86-
def do_custom_category(self, statement: Statement) -> None:
86+
def do_custom_category(self, _statement: Statement) -> None:
8787
self._cmd.poutput('Demonstrates a command that bypasses the default category')

examples/modular_commands/commandset_complex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
@cmd2.with_default_category('Fruits')
99
class CommandSetA(cmd2.CommandSet):
10-
def do_apple(self, statement: cmd2.Statement) -> None:
10+
def do_apple(self, _statement: cmd2.Statement) -> None:
1111
self._cmd.poutput('Apple!')
1212

13-
def do_banana(self, statement: cmd2.Statement) -> None:
13+
def do_banana(self, _statement: cmd2.Statement) -> None:
1414
"""Banana Command."""
1515
self._cmd.poutput('Banana!!')
1616

examples/modular_commands/commandset_custominit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def __init__(self, arg1, arg2) -> None:
1616
self._arg1 = arg1
1717
self._arg2 = arg2
1818

19-
def do_show_arg1(self, cmd: Cmd, _: Statement) -> None:
19+
def do_show_arg1(self, _cmd: Cmd, _: Statement) -> None:
2020
self._cmd.poutput('Arg1: ' + self._arg1)
2121

22-
def do_show_arg2(self, cmd: Cmd, _: Statement) -> None:
22+
def do_show_arg2(self, _cmd: Cmd, _: Statement) -> None:
2323
self._cmd.poutput('Arg2: ' + self._arg2)

examples/modular_commands_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ExampleApp(cmd2.Cmd):
2626
def __init__(self) -> None:
2727
super().__init__()
2828

29-
def do_something(self, arg) -> None:
29+
def do_something(self, _arg) -> None:
3030
self.poutput('this is the something command')
3131

3232

examples/pirate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def precmd(self, line):
4141
self.initial_gold = self.gold
4242
return line
4343

44-
def postcmd(self, stop, line):
44+
def postcmd(self, stop, _line):
4545
"""Runs right before a command is about to return."""
4646
if self.gold != self.initial_gold:
4747
self.poutput(f'Now we gots {self.gold} doubloons')
@@ -51,7 +51,7 @@ def postcmd(self, stop, line):
5151
stop = True
5252
return stop
5353

54-
def do_loot(self, arg) -> None:
54+
def do_loot(self, _arg) -> None:
5555
"""Seize booty from a passing ship."""
5656
self.gold += 1
5757

@@ -67,7 +67,7 @@ def do_drink(self, arg) -> None:
6767
self.poutput(f'''What's "{arg}"? I'll take rrrum.''')
6868
self.gold -= 1
6969

70-
def do_quit(self, arg) -> bool:
70+
def do_quit(self, _arg) -> bool:
7171
"""Quit the application gracefully."""
7272
self.poutput("Quiterrr!")
7373
return True

examples/python_scripting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _set_prompt(self) -> None:
4040
self.cwd = os.getcwd()
4141
self.prompt = ansi.style(f'{self.cwd} $ ', fg=ansi.Fg.CYAN)
4242

43-
def postcmd(self, stop: bool, line: str) -> bool:
43+
def postcmd(self, stop: bool, _line: str) -> bool:
4444
"""Hook method executed just after a command dispatch is finished.
4545
4646
:param stop: if True, the command has indicated the application should exit
@@ -96,7 +96,7 @@ def complete_cd(self, text, line, begidx, endidx):
9696
dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line")
9797

9898
@cmd2.with_argparser(dir_parser, with_unknown_args=True)
99-
def do_dir(self, args, unknown) -> None:
99+
def do_dir(self, _args, unknown) -> None:
100100
"""List contents of current directory."""
101101
# No arguments for this command
102102
if unknown:

plugins/ext_test/examples/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, *args, **kwargs):
1111
# gotta have this or neither the plugin or cmd2 will initialize
1212
super().__init__(*args, **kwargs)
1313

14-
def do_something(self, arg):
14+
def do_something(self, _arg):
1515
self.last_result = 5
1616
self.poutput('this is the something command')
1717

plugins/template/examples/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, *args, **kwargs) -> None:
1111
super().__init__(*args, **kwargs)
1212

1313
@cmd2_myplugin.empty_decorator
14-
def do_something(self, arg) -> None:
14+
def do_something(self, _arg) -> None:
1515
self.poutput('this is the something command')
1616

1717

plugins/template/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def pylint_tests(context) -> None:
9696

9797

9898
@invoke.task
99-
def build_clean(context) -> None:
99+
def build_clean(_context) -> None:
100100
"""Remove the build directory."""
101101
# pylint: disable=unused-argument
102102
rmrf(BUILDDIR)
@@ -106,7 +106,7 @@ def build_clean(context) -> None:
106106

107107

108108
@invoke.task
109-
def dist_clean(context) -> None:
109+
def dist_clean(_context) -> None:
110110
"""Remove the dist directory."""
111111
# pylint: disable=unused-argument
112112
rmrf(DISTDIR)
@@ -116,7 +116,7 @@ def dist_clean(context) -> None:
116116

117117

118118
@invoke.task
119-
def eggs_clean(context) -> None:
119+
def eggs_clean(_context) -> None:
120120
"""Remove egg directories."""
121121
# pylint: disable=unused-argument
122122
dirs = set()
@@ -133,7 +133,7 @@ def eggs_clean(context) -> None:
133133

134134

135135
@invoke.task
136-
def bytecode_clean(context) -> None:
136+
def bytecode_clean(_context) -> None:
137137
"""Remove __pycache__ directories and *.pyc files."""
138138
# pylint: disable=unused-argument
139139
dirs = set()

plugins/template/tests/test_myplugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, *args, **kwargs) -> None:
1818
super().__init__(*args, **kwargs)
1919

2020
@cmd2_myplugin.empty_decorator
21-
def do_empty(self, args) -> None:
21+
def do_empty(self, _args) -> None:
2222
self.poutput("running the empty command")
2323

2424

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ select = [
158158
# https://docs.astral.sh/ruff/rules
159159
"A", # flake8-builtins (variables or arguments shadowing built-ins)
160160
# "AIR", # Airflow specific warnings
161-
"ANN", # flake8-annotations (missing type annotations for arguments or return types)
162-
# "ARG", # flake8-unused-arguments (functions or methods with arguments that are never used)
161+
"ANN", # flake8-annotations (missing type annotations for arguments or return types)
162+
"ARG", # flake8-unused-arguments (functions or methods with arguments that are never used)
163163
"ASYNC", # flake8-async (async await bugs)
164164
# "B", # flake8-bugbear (various likely bugs and design issues)
165165
"BLE", # flake8-blind-except (force more specific exception types than just Exception)
@@ -279,6 +279,7 @@ per-file-ignores."plugins/*.py" = [
279279

280280
per-file-ignores."tests/*.py" = [
281281
"ANN", # Ignore all type annotation rules in test folders
282+
"ARG", # Ignore all unused argument warnings in test folders
282283
"D", # Ignore all pydocstyle rules in test folders
283284
"E501", # Line too long
284285
"S", # Ignore all Security rules in test folders
@@ -292,6 +293,7 @@ per-file-ignores."tests/pyscript/*.py" = [
292293

293294
per-file-ignores."tests_isolated/*.py" = [
294295
"ANN", # Ignore all type annotation rules in test folders
296+
"ARG", # Ignore all unused argument warnings in test folders
295297
"D", # Ignore all pydocstyle rules in test folders
296298
"S", # Ignore all Security rules in test folders
297299
"SLF", # Ignore all warnings about private or protected member access in test folders

tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def mypy_clean(context: Context) -> None:
125125

126126

127127
@invoke.task()
128-
def docs(context: Context, builder: str = 'html') -> None:
128+
def docs(context: Context) -> None:
129129
"""Build documentation using MkDocs."""
130130
with context.cd(TASK_ROOT_STR):
131131
context.run('mkdocs build', pty=True)

0 commit comments

Comments
 (0)