From b6384d02d3ff0a5a066eb39c9ce894aa1b3fb325 Mon Sep 17 00:00:00 2001 From: James Braza Date: Tue, 25 Apr 2023 07:38:03 -0700 Subject: [PATCH] Adding `flake8-pytest-style` to `ruff` (#1520) * Alphabetized ruff select and added flake8-pytest-style * Added PT019 to disables and alphabetizes them too * All fixes for flake8-pytest-style --- examples/client_calls.py | 6 ++++-- ruff.toml | 22 ++++++++++++---------- test/test_client.py | 6 +++--- test/test_client_multidrop.py | 4 ++-- test/test_example_client_server.py | 6 ++++-- test/test_exceptions.py | 6 +----- test/test_factory.py | 2 +- test/test_framers.py | 8 ++++---- test/test_logging.py | 2 +- test/test_server_asyncio.py | 2 +- test/test_server_task.py | 8 ++++---- test/test_simulator.py | 2 +- 12 files changed, 38 insertions(+), 36 deletions(-) diff --git a/examples/client_calls.py b/examples/client_calls.py index f1d49007e..7a2ac0a33 100755 --- a/examples/client_calls.py +++ b/examples/client_calls.py @@ -210,12 +210,14 @@ async def _execute_information_requests(client): rr = _check_call( await client.execute(req_other.GetCommEventCounterRequest(slave=SLAVE)) ) - assert rr.status and not rr.count + assert rr.status + assert not rr.count rr = _check_call( await client.execute(req_other.GetCommEventLogRequest(slave=SLAVE)) ) - assert rr.status and not (rr.event_count + rr.message_count + len(rr.events)) + assert rr.status + assert not (rr.event_count + rr.message_count + len(rr.events)) async def _execute_diagnostic_requests(client): diff --git a/ruff.toml b/ruff.toml index 26106c025..2e8e9fa00 100644 --- a/ruff.toml +++ b/ruff.toml @@ -7,20 +7,26 @@ exclude = [ "build", ] ignore = [ - "D202", # No blank lines allowed after function docstring (to work with black) - "E501", # line too long - "E731", # lambda expressions - "D400" # docstrings ending in period + "D202", # No blank lines allowed after function docstring (to work with black) + "D400", # docstrings ending in period + "E501", # line too long + "E731", # lambda expressions + "PT019", # Bug: https://github.com/m-burst/flake8-pytest-style/issues/202 ] line-length = 120 select = [ + # "B", # bandit "B007", # Loop control variable {name} not used within loop body "B014", # Exception handler with duplicate exception "C", # complexity "D", # docstrings "E", # pycodestyle errors - "W", # pycodestyle warnings "F", # pyflakes + "I", # isort + "PGH", # pygrep-hooks + "PLC", # pylint + "PT", # flake8-pytest-style + "RUF", # ruff builtins "SIM105", # flake8-simplify "SIM117", # "SIM118", # @@ -28,14 +34,10 @@ select = [ "SIM212", # "SIM300", # "SIM401", # - "RUF", # ruff builtins - "I", # isort "UP", # pyupgrade - "PLC", # pylint - "PGH", # pygrep-hooks + "W", # pycodestyle warnings # "TRY", # tryceratops "TRY004", # Prefer TypeError exception for invalid type - # "B", # bandit ] [pydocstyle] convention = "pep257" diff --git a/test/test_client.py b/test/test_client.py index 293ae0ad6..02c8eddb1 100755 --- a/test/test_client.py +++ b/test/test_client.py @@ -38,7 +38,7 @@ ], ) @pytest.mark.parametrize( - "method, arg, pdu_request", + ("method", "arg", "pdu_request"), [ ("read_coils", 1, pdu_bit_read.ReadCoilsRequest), ("read_discrete_inputs", 1, pdu_bit_read.ReadDiscreteInputsRequest), @@ -206,7 +206,7 @@ def fake_execute(_self, request): ], ) @pytest.mark.parametrize( - "type_args, clientclass", + ("type_args", "clientclass"), [ # TBD ("serial", lib_client.AsyncModbusSerialClient), # TBD ("serial", lib_client.ModbusSerialClient), @@ -488,7 +488,7 @@ def test_client_tls_connect(): @pytest.mark.parametrize( - "datatype,value,registers", + ("datatype", "value", "registers"), [ (ModbusClientMixin.DATATYPE.STRING, "abcd", [0x6162, 0x6364]), (ModbusClientMixin.DATATYPE.STRING, "a", [0x6100]), diff --git a/test/test_client_multidrop.py b/test/test_client_multidrop.py index bc145d096..a13b76747 100644 --- a/test/test_client_multidrop.py +++ b/test/test_client_multidrop.py @@ -15,12 +15,12 @@ class TestMultidrop: good_frame = b"\x02\x03\x00\x01\x00}\xd4\x18" @pytest.fixture(name="framer") - def _framer(self): + def fixture_framer(self): """Prepare framer.""" return ModbusRtuFramer(ServerDecoder()) @pytest.fixture(name="callback") - def _callback(self): + def fixture_callback(self): """Prepare dummy callback.""" return mock.Mock() diff --git a/test/test_example_client_server.py b/test/test_example_client_server.py index 7919f162f..e80cd9436 100755 --- a/test/test_example_client_server.py +++ b/test/test_example_client_server.py @@ -74,7 +74,7 @@ def test_get_commandline(): @pytest.mark.xdist_group(name="server_serialize") -@pytest.mark.parametrize("test_comm, test_framer, test_port", TEST_COMMS_FRAMER) +@pytest.mark.parametrize(("test_comm", "test_framer", "test_port"), TEST_COMMS_FRAMER) async def test_exp_async_server_client( test_comm, test_framer, @@ -102,7 +102,9 @@ async def test_exp_async_server_client( @pytest.mark.xdist_group(name="server_serialize") -@pytest.mark.parametrize("test_comm, test_framer, test_port", [TEST_COMMS_FRAMER[0]]) +@pytest.mark.parametrize( + ("test_comm", "test_framer", "test_port"), [TEST_COMMS_FRAMER[0]] +) def test_exp_sync_server_client( test_comm, test_framer, diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 8e5e481a8..369ad40fd 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -24,9 +24,5 @@ class TestExceptions: # pylint: disable=too-few-public-methods def test_exceptions(self): """Test all module exceptions""" for exc in self.exceptions: - try: + with pytest.raises(ModbusException, match="Modbus Error:"): raise exc - except ModbusException as exc: - assert "Modbus Error:" in str(exc) - return - pytest.fail("Excepted a ModbusExceptions") diff --git a/test/test_factory.py b/test/test_factory.py index 87a965e2c..ea2d4902c 100644 --- a/test/test_factory.py +++ b/test/test_factory.py @@ -96,7 +96,7 @@ class TestFactory: ) @pytest.fixture(autouse=True) - def setup(self): + def _setup(self): """Do common setup function.""" self.client = ClientDecoder() self.server = ServerDecoder() diff --git a/test/test_framers.py b/test/test_framers.py index b7df1fff9..cf3efbdcb 100644 --- a/test/test_framers.py +++ b/test/test_framers.py @@ -16,14 +16,14 @@ TEST_MESSAGE = b"\x00\x01\x00\x01\x00\n\xec\x1c" -@pytest.fixture -def rtu_framer(): +@pytest.fixture(name="rtu_framer") +def fixture_rtu_framer(): """RTU framer.""" return ModbusRtuFramer(ClientDecoder()) -@pytest.fixture -def ascii_framer(): +@pytest.fixture(name="ascii_framer") +def fixture_ascii_framer(): """Ascii framer.""" return ModbusAsciiFramer(ClientDecoder()) diff --git a/test/test_logging.py b/test/test_logging.py index 4c7e1cff4..7c12f015a 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -28,7 +28,7 @@ def test_log_simple(self): assert log_txt == txt @pytest.mark.parametrize( - "txt, result, params", + ("txt", "result", "params"), [ ("string {} {} {}", "string 101 102 103", (101, 102, 103)), ("string {}", "string 0x41 0x42 0x43 0x44", (b"ABCD", ":hex")), diff --git a/test/test_server_asyncio.py b/test/test_server_asyncio.py index f8f6bd640..413e765b9 100755 --- a/test/test_server_asyncio.py +++ b/test/test_server_asyncio.py @@ -105,7 +105,7 @@ class TestAsyncioServer: # pylint: disable=too-many-public-methods identity = None @pytest.fixture(autouse=True) - async def setup_teardown(self): + async def _setup_teardown(self): """Initialize the test environment by setting up a dummy store and context.""" self.loop = asyncio.get_running_loop() self.store = ModbusSlaveContext( diff --git a/test/test_server_task.py b/test/test_server_task.py index 3fac6b9a8..d008d5fa9 100755 --- a/test/test_server_task.py +++ b/test/test_server_task.py @@ -149,7 +149,7 @@ async def test_async_task_no_server(comm): try: await client.connect() except Exception as exc: # pylint: disable=broad-except - assert False, f"unexpected exception: {exc}" + raise AssertionError(f"unexpected exception: {exc}") from exc await asyncio.sleep(0.1) with pytest.raises((asyncio.exceptions.TimeoutError, ConnectionException)): await client.read_coils(1, 1, slave=0x01) @@ -247,7 +247,7 @@ async def test_async_task_server_stop(comm): await asyncio.sleep(0.1) timer_allowed -= 1 if not timer_allowed: - assert False, "client do not reconnect" + pytest.fail("client do not reconnect") assert client.transport on_reconnect_callback.assert_called() @@ -270,7 +270,7 @@ def test_sync_task_no_server(comm): try: client.connect() except Exception as exc: # pylint: disable=broad-except - assert False, f"unexpected exception: {exc}" + raise AssertionError(f"unexpected exception: {exc}") from exc sleep(0.1) if comm == "udp": rr = client.read_coils(1, 1, slave=0x01) @@ -345,7 +345,7 @@ def test_sync_task_server_stop(comm): sleep(0.1) timer_allowed -= 1 if not timer_allowed: - assert False, "client do not reconnect" + pytest.fail("client do not reconnect") assert client.socket rr = client.read_coils(1, 1, slave=0x01) diff --git a/test/test_simulator.py b/test/test_simulator.py index 03281dce5..ead7bfc9e 100644 --- a/test/test_simulator.py +++ b/test/test_simulator.py @@ -404,7 +404,7 @@ def test_simulator_get_text(self): assert cell.count_write == str(reg.count_write), f"at register {test_reg}" @pytest.mark.parametrize( - "func,addr", + ("func", "addr"), [ (FX_READ_BIT, 12), (FX_READ_REG, 16),