diff --git a/board/jungle/scripts/can_printer.py b/board/jungle/scripts/can_printer.py index 3e6480634..675fc508a 100755 --- a/board/jungle/scripts/can_printer.py +++ b/board/jungle/scripts/can_printer.py @@ -26,7 +26,7 @@ def can_printer(): if sec_since_boot() - lp > 0.1: dd = chr(27) + "[2J" dd += "%5.2f\n" % (sec_since_boot() - start) - for k,v in sorted(zip(list(msgs.keys()), [binascii.hexlify(x[-1]) for x in list(msgs.values())])): + for k,v in sorted(zip(list(msgs.keys()), [binascii.hexlify(x[-1]) for x in list(msgs.values())], strict=True)): dd += "%s(%6d) %s\n" % ("%04X(%4d)" % (k,k),len(msgs[k]), v) print(dd) lp = sec_since_boot() diff --git a/board/jungle/scripts/echo_loopback_test.py b/board/jungle/scripts/echo_loopback_test.py index d68fa4f3e..78b65b534 100755 --- a/board/jungle/scripts/echo_loopback_test.py +++ b/board/jungle/scripts/echo_loopback_test.py @@ -43,7 +43,7 @@ def test_loopback(): break if not found: cprint("\nFAILED", "red") - assert False + raise AssertionError ################################################################# ############################# MAIN ############################## diff --git a/pyproject.toml b/pyproject.toml index 0a5a4e5ca..53f2b5aba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ # https://beta.ruff.rs/docs/configuration/#using-pyprojecttoml [tool.ruff] -select = ["E", "F", "W"] +select = ["E", "F", "W", "B"] ignore = ["W292", "E741"] line-length = 160 target-version="py311" diff --git a/python/isotp.py b/python/isotp.py index 45acb6aba..3334deb8e 100644 --- a/python/isotp.py +++ b/python/isotp.py @@ -6,10 +6,8 @@ DEBUG = False def msg(x): if DEBUG: print("S:", binascii.hexlify(x)) - if len(x) <= 7: - ret = bytes([len(x)]) + x - else: - assert False + assert len(x) <= 7 + ret = bytes([len(x)]) + x return ret.ljust(8, b"\x00") kmsgs = [] @@ -56,7 +54,7 @@ def isotp_recv_subaddr(panda, addr, bus, sendaddr, subaddr): dat = msg[2:] else: print(binascii.hexlify(msg)) - assert False + raise AssertionError return dat[0:tlen] @@ -133,7 +131,7 @@ def isotp_recv(panda, addr, bus=0, sendaddr=None, subaddr=None): tlen = msg[0] & 0xf dat = msg[1:] else: - assert False + raise AssertionError dat = dat[0:tlen] if DEBUG: diff --git a/python/spi.py b/python/spi.py index ad0225b1e..0be28f49c 100644 --- a/python/spi.py +++ b/python/spi.py @@ -314,7 +314,7 @@ class STBootloaderSPIHandle(BaseSTBootloaderHandle): self._mcu_type = MCU_TYPE_BY_IDCODE[self.get_chip_id()] except PandaSpiException: - raise PandaSpiException("failed to connect to panda") # pylint: disable=W0707 + raise PandaSpiException("failed to connect to panda") from None def _get_ack(self, spi, timeout=1.0): data = 0x00 diff --git a/tests/black_loopback_test.py b/tests/black_loopback_test.py deleted file mode 100755 index bcfcaefed..000000000 --- a/tests/black_loopback_test.py +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/env python3 - -# Loopback test between two black pandas (+ harness and power) -# Tests all buses, including OBD CAN, which is on the same bus as CAN0 in this test. -# To be sure, the test should be run with both harness orientations - - -import os -import sys -import time -import random -import argparse - -sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) -from panda import Panda # noqa: E402 - -def get_test_string(): - return b"test" + os.urandom(10) - -def run_test(sleep_duration): - pandas = Panda.list() - print(pandas) - - # make sure two pandas are connected - if len(pandas) != 2: - print("Connect white/grey and black panda to run this test!") - assert False - - # connect - pandas[0] = Panda(pandas[0]) - pandas[1] = Panda(pandas[1]) - - # find out the hardware types - if not pandas[0].is_black() or not pandas[1].is_black(): - print("Connect two black pandas to run this test!") - assert False - - for panda in pandas: - # disable safety modes - panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) - - # test health packet - print("panda health", panda.health()) - - # setup test array (send bus, sender obd, reciever obd, expected busses) - test_array = [ - (0, False, False, [0]), - (1, False, False, [1]), - (2, False, False, [2]), - (0, False, True, [0, 1]), - (1, False, True, []), - (2, False, True, [2]), - (0, True, False, [0]), - (1, True, False, [0]), - (2, True, False, [2]), - (0, True, True, [0, 1]), - (1, True, True, [0, 1]), - (2, True, True, [2]) - ] - - # test both orientations - print("***************** TESTING (0 --> 1) *****************") - test_buses(pandas[0], pandas[1], test_array, sleep_duration) - print("***************** TESTING (1 --> 0) *****************") - test_buses(pandas[1], pandas[0], test_array, sleep_duration) - - -def test_buses(send_panda, recv_panda, test_array, sleep_duration): - for send_bus, send_obd, recv_obd, recv_buses in test_array: - send_panda.send_heartbeat() - recv_panda.send_heartbeat() - print("\nSend bus:", send_bus, " Send OBD:", send_obd, " Recv OBD:", recv_obd) - - # set OBD on pandas - send_panda.set_gmlan(True if send_obd else None) - recv_panda.set_gmlan(True if recv_obd else None) - - # clear and flush - send_panda.can_clear(send_bus) - for recv_bus in recv_buses: - recv_panda.can_clear(recv_bus) - send_panda.can_recv() - recv_panda.can_recv() - - # send the characters - at = random.randint(1, 2000) - st = get_test_string()[0:8] - send_panda.can_send(at, st, send_bus) - time.sleep(0.1) - - # check for receive - _ = send_panda.can_recv() # cans echo - cans_loop = recv_panda.can_recv() - - loop_buses = [] - for loop in cans_loop: - print(" Loop on bus", str(loop[3])) - loop_buses.append(loop[3]) - if len(cans_loop) == 0: - print(" No loop") - - # test loop buses - recv_buses.sort() - loop_buses.sort() - assert recv_buses == loop_buses - print(" TEST PASSED") - - time.sleep(sleep_duration) - print("\n") - -if __name__ == "__main__": - parser = argparse.ArgumentParser() - parser.add_argument("-n", type=int, help="Number of test iterations to run") - parser.add_argument("-sleep", type=int, help="Sleep time between tests", default=0) - args = parser.parse_args() - - if args.n is None: - while True: - run_test(sleep_duration=args.sleep) - else: - for i in range(args.n): - run_test(sleep_duration=args.sleep) diff --git a/tests/black_white_loopback_test.py b/tests/black_white_loopback_test.py index f5c6170fe..8914430a1 100755 --- a/tests/black_white_loopback_test.py +++ b/tests/black_white_loopback_test.py @@ -30,8 +30,7 @@ def run_test(sleep_duration): # make sure two pandas are connected if len(pandas) != 2: - print("Connect white/grey and black panda to run this test!") - assert False + raise Exception("Connect white/grey and black panda to run this test!") # connect pandas[0] = Panda(pandas[0]) @@ -48,8 +47,7 @@ def run_test(sleep_duration): black_panda = pandas[1] other_panda = pandas[0] else: - print("Connect white/grey and black panda to run this test!") - assert False + raise Exception("Connect white/grey and black panda to run this test!") # disable safety modes black_panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) @@ -130,8 +128,7 @@ def test_buses(black_panda, other_panda, direction, test_array, sleep_duration): loop_buses.append(loop[3]) if len(cans_loop) == 0: print(" No loop") - if not os.getenv("NOASSERT"): - assert False + assert not os.getenv("NOASSERT") # test loop buses recv_buses.sort() @@ -141,8 +138,7 @@ def test_buses(black_panda, other_panda, direction, test_array, sleep_duration): zero_bus_errors += 1 else: nonzero_bus_errors += 1 - if not os.getenv("NOASSERT"): - assert False + assert not os.getenv("NOASSERT") else: print(" TEST PASSED") @@ -159,5 +155,5 @@ if __name__ == "__main__": while True: run_test(sleep_duration=args.sleep) else: - for i in range(args.n): + for _ in range(args.n): run_test(sleep_duration=args.sleep) diff --git a/tests/black_white_relay_endurance.py b/tests/black_white_relay_endurance.py index f44eafd20..cfdaeb330 100755 --- a/tests/black_white_relay_endurance.py +++ b/tests/black_white_relay_endurance.py @@ -30,8 +30,7 @@ def run_test(sleep_duration): # make sure two pandas are connected if len(pandas) != 2: - print("Connect white/grey and black panda to run this test!") - assert False + raise Exception("Connect white/grey and black panda to run this test!") # connect pandas[0] = Panda(pandas[0]) @@ -48,8 +47,7 @@ def run_test(sleep_duration): black_panda = pandas[1] other_panda = pandas[0] else: - print("Connect white/grey and black panda to run this test!") - assert False + raise Exception("Connect white/grey and black panda to run this test!") # disable safety modes black_panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) @@ -137,8 +135,7 @@ def test_buses(black_panda, other_panda, direction, test_array, sleep_duration): loop_buses.append(loop[3]) if len(cans_loop) == 0: print(" No loop") - if not os.getenv("NOASSERT"): - assert False + assert os.getenv("NOASSERT") # test loop buses recv_buses.sort() @@ -148,8 +145,7 @@ def test_buses(black_panda, other_panda, direction, test_array, sleep_duration): zero_bus_errors += 1 else: nonzero_bus_errors += 1 - if not os.getenv("NOASSERT"): - assert False + assert os.getenv("NOASSERT") else: print(" TEST PASSED") @@ -166,5 +162,5 @@ if __name__ == "__main__": while True: run_test(sleep_duration=args.sleep) else: - for i in range(args.n): + for _ in range(args.n): run_test(sleep_duration=args.sleep) diff --git a/tests/black_white_relay_test.py b/tests/black_white_relay_test.py index 4de02ec84..4a3f58f7f 100755 --- a/tests/black_white_relay_test.py +++ b/tests/black_white_relay_test.py @@ -29,8 +29,7 @@ def run_test(sleep_duration): # make sure two pandas are connected if len(pandas) != 2: - print("Connect white/grey and black panda to run this test!") - assert False + raise Exception("Connect white/grey and black panda to run this test!") # connect pandas[0] = Panda(pandas[0]) @@ -50,8 +49,7 @@ def run_test(sleep_duration): black_panda = pandas[1] other_panda = pandas[0] else: - print("Connect white/grey and black panda to run this test!") - assert False + raise Exception("Connect white/grey and black panda to run this test!") # disable safety modes black_panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) @@ -69,8 +67,7 @@ def run_test(sleep_duration): if not test_buses(black_panda, other_panda, (0, False, [0])): open_errors += 1 - print("Open error") - assert False + raise Exception("Open error") # Switch off relay black_panda.set_safety_mode(Panda.SAFETY_SILENT) @@ -78,8 +75,7 @@ def run_test(sleep_duration): if not test_buses(black_panda, other_panda, (0, False, [0, 2])): closed_errors += 1 - print("Close error") - assert False + raise Exception("Close error") counter += 1 print("Number of cycles:", counter, "Open errors:", open_errors, "Closed errors:", closed_errors, "Content errors:", content_errors) @@ -137,5 +133,5 @@ if __name__ == "__main__": while True: run_test(sleep_duration=args.sleep) else: - for i in range(args.n): + for _ in range(args.n): run_test(sleep_duration=args.sleep) diff --git a/tests/can_printer.py b/tests/can_printer.py index af0ed0e2e..15ce89f68 100755 --- a/tests/can_printer.py +++ b/tests/can_printer.py @@ -1,13 +1,10 @@ #!/usr/bin/env python3 - import os -import sys import time from collections import defaultdict import binascii -sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) -from panda import Panda # noqa: E402 +from panda import Panda # fake def sec_since_boot(): @@ -30,7 +27,7 @@ def can_printer(): if sec_since_boot() - lp > 0.1: dd = chr(27) + "[2J" dd += "%5.2f\n" % (sec_since_boot() - start) - for k, v in sorted(zip(list(msgs.keys()), [binascii.hexlify(x[-1]) for x in list(msgs.values())])): + for k, v in sorted(zip(list(msgs.keys()), [binascii.hexlify(x[-1]) for x in list(msgs.values())], strict=True)): dd += "%s(%6d) %s\n" % ("%04X(%4d)" % (k, k), len(msgs[k]), v) print(dd) lp = sec_since_boot() diff --git a/tests/hitl/helpers.py b/tests/hitl/helpers.py index ee0478404..aafad72db 100644 --- a/tests/hitl/helpers.py +++ b/tests/hitl/helpers.py @@ -64,5 +64,4 @@ def clear_can_buffers(panda): r = panda.can_recv() time.sleep(0.05) if (time.monotonic() - st) > 10: - print("Unable to clear can buffers for panda ", panda.get_serial()) - assert False + raise Exception("Unable to clear can buffers for panda ", panda.get_serial()) diff --git a/tests/loopback_test.py b/tests/loopback_test.py index 84925952a..630c55215 100755 --- a/tests/loopback_test.py +++ b/tests/loopback_test.py @@ -20,8 +20,7 @@ def run_test(sleep_duration): print(pandas) if len(pandas) < 2: - print("Minimum two pandas are needed for test") - assert False + raise Exception("Minimum two pandas are needed for test") run_test_w_pandas(pandas, sleep_duration) @@ -116,5 +115,5 @@ if __name__ == "__main__": while True: run_test(sleep_duration=args.sleep) else: - for i in range(args.n): + for _ in range(args.n): run_test(sleep_duration=args.sleep) diff --git a/tests/safety/common.py b/tests/safety/common.py index afce6c187..0bec019fb 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -884,7 +884,8 @@ class PandaSafetyTest(PandaSafetyTestBase): test = importlib.import_module("panda.tests.safety."+tf[:-3]) for attr in dir(test): if attr.startswith("Test") and attr != current_test: - tx = getattr(getattr(test, attr), "TX_MSGS") + tc = getattr(test, attr) + tx = tc.TX_MSGS if tx is not None and not attr.endswith('Base'): # No point in comparing different Tesla safety modes if 'Tesla' in attr and 'Tesla' in current_test: diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index f8e17c24e..11b1c623e 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -132,7 +132,7 @@ class HondaButtonEnableBase(common.PandaSafetyTest): self.assertFalse(self.safety.get_controls_allowed()) # restore counters for future tests with a couple of good messages - for i in range(2): + for _ in range(2): self.safety.set_controls_allowed(1) self._rx(self._button_msg(Btn.SET)) self._rx(self._speed_msg(0)) diff --git a/tests/standalone_test.py b/tests/standalone_test.py index 6580b06d1..dfd2c9a7a 100755 --- a/tests/standalone_test.py +++ b/tests/standalone_test.py @@ -13,7 +13,7 @@ if __name__ == "__main__": print(p.health()) t1 = time.time() - for i in range(100): + for _ in range(100): p.get_serial() t2 = time.time() print("100 requests took %.2f ms" % ((t2 - t1) * 1000)) diff --git a/tests/tucan_loopback.py b/tests/tucan_loopback.py index ff037f3e3..0780ffee0 100755 --- a/tests/tucan_loopback.py +++ b/tests/tucan_loopback.py @@ -20,8 +20,7 @@ def run_test(sleep_duration): print(pandas) if len(pandas) < 2: - print("Two pandas are needed for test") - assert False + raise Exception("Two pandas are needed for test") run_test_w_pandas(pandas, sleep_duration) @@ -114,5 +113,5 @@ if __name__ == "__main__": while True: run_test(sleep_duration=args.sleep) else: - for i in range(args.n): + for _ in range(args.n): run_test(sleep_duration=args.sleep)