Skip to content

Commit

Permalink
Adding flake8-pytest-style to ruff (#1520)
Browse files Browse the repository at this point in the history
* Alphabetized ruff select and added flake8-pytest-style

* Added PT019 to disables and alphabetizes them too

* All fixes for flake8-pytest-style
  • Loading branch information
jamesbraza authored Apr 25, 2023
1 parent 4332562 commit b6384d0
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 36 deletions.
6 changes: 4 additions & 2 deletions examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 12 additions & 10 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ 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", #
"SIM201", #
"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"
Expand Down
6 changes: 3 additions & 3 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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]),
Expand Down
4 changes: 2 additions & 2 deletions test/test_client_multidrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 4 additions & 2 deletions test/test_example_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion test/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions test/test_framers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
2 changes: 1 addition & 1 deletion test/test_server_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions test/test_server_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit b6384d0

Please sign in to comment.