From b8b15d5cf1d2507c89bbe0e3b1de1ff043d9078e Mon Sep 17 00:00:00 2001 From: grekiki <96022003+GregorKikelj@users.noreply.github.com> Date: Thu, 21 Apr 2022 12:27:45 -0400 Subject: [PATCH] mypy: enable --warn-unreachable (#24282) * Optional solves mypy assuming None is always None * oops * spaces * Update registration.py * fix local error Co-authored-by: Willem Melching --- .pre-commit-config.yaml | 2 ++ selfdrive/athena/registration.py | 4 +++- selfdrive/debug/internal/measure_modeld_packet_drop.py | 3 ++- selfdrive/debug/live_cpu_and_temp.py | 9 +++++---- selfdrive/hardware/base.py | 2 +- selfdrive/manager/build.py | 2 +- selfdrive/manager/process.py | 2 +- tools/replay/can_replay.py | 2 +- tools/sim/bridge.py | 1 - 9 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3fc5596568..dd53a4200c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,6 +22,8 @@ repos: exclude: '^(pyextra/)|(cereal/)|(rednose/)|(panda/)|(laika/)|(opendbc/)|(laika_repo/)|(rednose_repo/)/' additional_dependencies: ['git+https://github.com/numpy/numpy-stubs', 'types-requests', 'types-atomicwrites', 'types-pycurl'] + args: + - --warn-unreachable - repo: https://github.com/PyCQA/flake8 rev: 4.0.1 hooks: diff --git a/selfdrive/athena/registration.py b/selfdrive/athena/registration.py index 10f5d46f77..cc4bd7e647 100755 --- a/selfdrive/athena/registration.py +++ b/selfdrive/athena/registration.py @@ -3,6 +3,7 @@ import time import json import jwt from pathlib import Path +from typing import Optional from datetime import datetime, timedelta from common.api import api_get @@ -48,7 +49,8 @@ def register(show_spinner=False) -> str: # Block until we get the imei serial = HARDWARE.get_serial() start_time = time.monotonic() - imei1, imei2 = None, None + imei1: Optional[str] = None + imei2: Optional[str] = None while imei1 is None and imei2 is None: try: imei1, imei2 = HARDWARE.get_imei(0), HARDWARE.get_imei(1) diff --git a/selfdrive/debug/internal/measure_modeld_packet_drop.py b/selfdrive/debug/internal/measure_modeld_packet_drop.py index d5f65d06b1..6b7f7dbd13 100755 --- a/selfdrive/debug/internal/measure_modeld_packet_drop.py +++ b/selfdrive/debug/internal/measure_modeld_packet_drop.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 import cereal.messaging as messaging +from typing import Optional if __name__ == "__main__": modeld_sock = messaging.sub_sock("modelV2") last_frame_id = None - start_t = None + start_t: Optional[int] = None frame_cnt = 0 dropped = 0 diff --git a/selfdrive/debug/live_cpu_and_temp.py b/selfdrive/debug/live_cpu_and_temp.py index e46c0b0a1f..b244f7cb0e 100755 --- a/selfdrive/debug/live_cpu_and_temp.py +++ b/selfdrive/debug/live_cpu_and_temp.py @@ -1,9 +1,10 @@ #!/usr/bin/env python3 import argparse +import capnp from cereal.messaging import SubMaster from common.numpy_fast import mean - +from typing import Optional def cputime_total(ct): return ct.user + ct.nice + ct.system + ct.idle + ct.iowait + ct.irq + ct.softirq @@ -40,8 +41,8 @@ if __name__ == "__main__": total_times = [0.]*8 busy_times = [0.]*8 - prev_proclog = None - prev_proclog_t = None + prev_proclog: Optional[capnp._DynamicStructReader] = None + prev_proclog_t: Optional[int] = None while True: sm.update() @@ -73,7 +74,7 @@ if __name__ == "__main__": print(f"CPU {100.0 * mean(cores):.2f}% - RAM: {last_mem:.2f}% - Temp {last_temp:.2f}C") - if args.cpu and prev_proclog is not None: + if args.cpu and prev_proclog is not None and prev_proclog_t is not None: procs = {} dt = (sm.logMonoTime['procLog'] - prev_proclog_t) / 1e9 for proc in m.procs: diff --git a/selfdrive/hardware/base.py b/selfdrive/hardware/base.py index c05c75bbb2..2f251d3364 100644 --- a/selfdrive/hardware/base.py +++ b/selfdrive/hardware/base.py @@ -44,7 +44,7 @@ class HardwareBase(ABC): pass @abstractmethod - def get_imei(self, slot): + def get_imei(self, slot) -> str: pass @abstractmethod diff --git a/selfdrive/manager/build.py b/selfdrive/manager/build.py index 4b97855f34..806aa58bb7 100755 --- a/selfdrive/manager/build.py +++ b/selfdrive/manager/build.py @@ -29,7 +29,7 @@ def build(spinner: Spinner, dirty: bool = False) -> None: j_flag = "" if nproc is None else f"-j{nproc - 1}" for retry in [True, False]: - scons = subprocess.Popen(["scons", j_flag, "--cache-populate"], cwd=BASEDIR, env=env, stderr=subprocess.PIPE) + scons: subprocess.Popen = subprocess.Popen(["scons", j_flag, "--cache-populate"], cwd=BASEDIR, env=env, stderr=subprocess.PIPE) assert scons.stderr is not None compile_output = [] diff --git a/selfdrive/manager/process.py b/selfdrive/manager/process.py index 2b5ef7b85d..8bb160961f 100644 --- a/selfdrive/manager/process.py +++ b/selfdrive/manager/process.py @@ -78,7 +78,7 @@ class ManagerProcess(ABC): name = "" last_watchdog_time = 0 - watchdog_max_dt = None + watchdog_max_dt: Optional[int] = None watchdog_seen = False shutting_down = False diff --git a/tools/replay/can_replay.py b/tools/replay/can_replay.py index 6d68e75fe7..571a13e960 100755 --- a/tools/replay/can_replay.py +++ b/tools/replay/can_replay.py @@ -83,7 +83,7 @@ def connect(): if __name__ == "__main__": if PandaJungle is None: - print("\33[31m", "WARNING: cannot connect to jungles. Clone the jungle library to enable support:", "\033[0m") + print("\33[31m", "WARNING: cannot connect to jungles. Clone the jungle library to enable support:", "\033[0m") # type: ignore print("\033[34m", f"cd {BASEDIR} && git clone https://github.com/commaai/panda_jungle", "\033[0m") print("Loading log...") diff --git a/tools/sim/bridge.py b/tools/sim/bridge.py index 3777b39696..71d7a20489 100755 --- a/tools/sim/bridge.py +++ b/tools/sim/bridge.py @@ -489,7 +489,6 @@ if __name__ == "__main__": # start input poll for joystick from lib.manual_ctrl import wheel_poll_thread wheel_poll_thread(q) - p.join() else: # start input poll for keyboard from lib.keyboard_ctrl import keyboard_poll_thread