Skip to content

Commit 3d75fea

Browse files
committed
Make a few changes suggested by ruff in examples and tests
1 parent 369663c commit 3d75fea

6 files changed

Lines changed: 11 additions & 10 deletions

File tree

examples/argparse_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, color: str) -> None:
3434
"cmd2 has awesome decorators to make it easy to use Argparse to parse command arguments", style=color
3535
)
3636

37-
## ------ Basic examples of using argparse for command argument parsing -----
37+
# ------ Basic examples of using argparse for command argument parsing -----
3838

3939
# do_fsize parser
4040
fsize_parser = cmd2.Cmd2ArgumentParser(description="Obtain the size of a file")
@@ -79,7 +79,7 @@ def do_pow(self, args: argparse.Namespace) -> None:
7979
"""
8080
self.poutput(f"{args.base} ** {args.exponent} == {args.base**args.exponent}")
8181

82-
## ------ Examples displaying how argparse arguments are passed to commands by printing them out -----
82+
# ------ Examples displaying how argparse arguments are passed to commands by printing them out -----
8383

8484
argprint_parser = cmd2.Cmd2ArgumentParser()
8585
argprint_parser.add_argument("-p", "--piglatin", action="store_true", help="atinLay")
@@ -104,7 +104,7 @@ def do_print_unknown(self, args: argparse.Namespace, unknown: list[str]) -> None
104104
"""Print the arpgarse argument list this command was called with, including unknown arguments."""
105105
self.poutput(f"print_unknown was called with the following arguments\n\tknown: {args!r}\n\tunknown: {unknown}")
106106

107-
## ------ Examples demonstrating how to use argparse subcommands -----
107+
# ------ Examples demonstrating how to use argparse subcommands -----
108108

109109
# create the top-level parser for the base command
110110
calculate_parser = cmd2.Cmd2ArgumentParser(description="Perform simple mathematical calculations.")

examples/paged_output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""A simple example demonstrating the using paged output via the ppaged() method."""
33

44
import os
5+
import pathlib
56

67
import cmd2
78

@@ -16,8 +17,7 @@ def page_file(self, file_path: str, chop: bool = False) -> None:
1617
"""Helper method to prevent having too much duplicated code."""
1718
filename = os.path.expanduser(file_path)
1819
try:
19-
with open(filename) as f:
20-
text = f.read()
20+
text = pathlib.Path(filename).read_text()
2121
self.ppaged(text, chop=chop)
2222
except OSError as ex:
2323
self.pexcept(f"Error reading {filename!r}: {ex}")

tests/test_argparse_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def test_register_argparse_argument_parameter() -> None:
322322
with pytest.raises(KeyError, match=expected_err):
323323
register_argparse_argument_parameter("colliding_param")
324324
finally:
325-
delattr(argparse.Action, "get_colliding_param")
325+
del argparse.Action.get_colliding_param
326326

327327
# Test collision with internal attribute
328328
try:

tests/test_commandset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def test_load_commandset_errors(manual_command_sets_app, capsys) -> None:
348348
assert "Fruits" not in cmds_cats
349349
assert not manual_command_sets_app._installed_command_sets
350350

351-
delattr(manual_command_sets_app, "do_durian")
351+
del manual_command_sets_app.do_durian
352352

353353
# pre-create intentionally conflicting macro and alias names
354354
manual_command_sets_app.app_cmd("macro create apple run_pyscript")

tests/test_run_pyscript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Unit/functional testing for run_pytest in cmd2"""
22

33
import os
4+
import pathlib
45
from unittest import (
56
mock,
67
)
@@ -245,8 +246,7 @@ def test_run_pyscript_print_redirection(base_app, request, tmp_path, capsys) ->
245246
out, err = capsys.readouterr()
246247

247248
# Verify the output file contains what we expect from print()
248-
with open(out_file) as f:
249-
content = f.read()
249+
content = pathlib.Path(out_file).read_text()
250250

251251
# Look for everything written to self.stdout
252252
assert len(content.splitlines()) == 4

tests/test_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Unit testing for cmd2/utils.py module."""
22

3+
import math
34
import os
45
import signal
56
import sys
@@ -281,7 +282,7 @@ def test_to_bool_int() -> None:
281282
def test_to_bool_float() -> None:
282283
assert cu.to_bool(2.35)
283284
assert cu.to_bool(0.25)
284-
assert cu.to_bool(-3.1415)
285+
assert cu.to_bool(-math.pi)
285286
assert not cu.to_bool(0)
286287

287288

0 commit comments

Comments
 (0)