From 7cd23af9b15e0bac1c9c723155e6be9383eee121 Mon Sep 17 00:00:00 2001 From: grekiki <96022003+GregorKikelj@users.noreply.github.com> Date: Thu, 12 May 2022 14:08:11 +0200 Subject: [PATCH] Mypy fixes for --check-untyped-defs (#24372) more type fixes old-commit-hash: 17e33978cdd9322918e9d5d388f7fa47aa30e9b7 --- common/realtime.py | 2 +- selfdrive/athena/athenad.py | 2 +- selfdrive/car/honda/carcontroller.py | 8 ++++---- selfdrive/controls/controlsd.py | 6 ++++-- selfdrive/locationd/test/test_locationd.py | 2 +- selfdrive/loggerd/uploader.py | 4 ++-- selfdrive/loggerd/xattr_cache.py | 2 +- selfdrive/modeld/thneed/lib.py | 4 ++-- selfdrive/test/process_replay/model_replay.py | 4 ++-- selfdrive/test/process_replay/test_processes.py | 4 ++-- tools/lib/auth.py | 4 ++-- tools/replay/unlog_ci_segment.py | 4 ++-- tools/sim/bridge.py | 8 ++++---- 13 files changed, 28 insertions(+), 26 deletions(-) diff --git a/common/realtime.py b/common/realtime.py index 6ef27ed3a3..c77bab0519 100644 --- a/common/realtime.py +++ b/common/realtime.py @@ -52,7 +52,7 @@ def config_realtime_process(cores: Union[int, List[int]], priority: int) -> None class Ratekeeper: - def __init__(self, rate: int, print_delay_threshold: Optional[float] = 0.0) -> None: + def __init__(self, rate: float, print_delay_threshold: Optional[float] = 0.0) -> None: """Rate in Hz for ratekeeping. print_delay_threshold must be nonnegative.""" self._interval = 1. / rate self._next_frame_time = sec_since_boot() + self._interval diff --git a/selfdrive/athena/athenad.py b/selfdrive/athena/athenad.py index b9c9acb465..f91752479a 100755 --- a/selfdrive/athena/athenad.py +++ b/selfdrive/athena/athenad.py @@ -439,7 +439,7 @@ def startLocalProxy(global_end_event, remote_ws_uri, local_port): ssock, csock = socket.socketpair() local_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) local_sock.connect(('127.0.0.1', local_port)) - local_sock.setblocking(0) + local_sock.setblocking(False) proxy_end_event = threading.Event() threads = [ diff --git a/selfdrive/car/honda/carcontroller.py b/selfdrive/car/honda/carcontroller.py index dc7648ce58..f803e9db47 100644 --- a/selfdrive/car/honda/carcontroller.py +++ b/selfdrive/car/honda/carcontroller.py @@ -112,10 +112,10 @@ class CarController: self.apply_brake_last = 0 self.last_pump_ts = 0. - self.accel = 0 - self.speed = 0 - self.gas = 0 - self.brake = 0 + self.accel = 0.0 + self.speed = 0.0 + self.gas = 0.0 + self.brake = 0.0 def update(self, CC, CS): actuators = CC.actuators diff --git a/selfdrive/controls/controlsd.py b/selfdrive/controls/controlsd.py index 498218ab46..cdf9f38e15 100755 --- a/selfdrive/controls/controlsd.py +++ b/selfdrive/controls/controlsd.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import os import math -from numbers import Number +from typing import SupportsFloat from cereal import car, log from common.numpy_fast import clip @@ -17,6 +17,7 @@ from selfdrive.car.car_helpers import get_car, get_startup_event, get_one_can from selfdrive.controls.lib.lane_planner import CAMERA_OFFSET from selfdrive.controls.lib.drive_helpers import update_v_cruise, initialize_v_cruise from selfdrive.controls.lib.drive_helpers import get_lag_adjusted_curvature +from selfdrive.controls.lib.latcontrol import LatControl from selfdrive.controls.lib.longcontrol import LongControl from selfdrive.controls.lib.latcontrol_pid import LatControlPID from selfdrive.controls.lib.latcontrol_indi import LatControlINDI @@ -137,6 +138,7 @@ class Controls: self.LoC = LongControl(self.CP) self.VM = VehicleModel(self.CP) + self.LaC: LatControl if self.CP.steerControlType == car.CarParams.SteerControlType.angle: self.LaC = LatControlAngle(self.CP, self.CI) elif self.CP.lateralTuning.which() == 'pid': @@ -607,7 +609,7 @@ class Controls: # Ensure no NaNs/Infs for p in ACTUATOR_FIELDS: attr = getattr(actuators, p) - if not isinstance(attr, Number): + if not isinstance(attr, SupportsFloat): continue if not math.isfinite(attr): diff --git a/selfdrive/locationd/test/test_locationd.py b/selfdrive/locationd/test/test_locationd.py index 5c0143bdbf..7f5d752100 100755 --- a/selfdrive/locationd/test/test_locationd.py +++ b/selfdrive/locationd/test/test_locationd.py @@ -108,7 +108,7 @@ void localizer_handle_msg_bytes(Localizer_t localizer, const char *data, size_t class TestLocationdProc(unittest.TestCase): MAX_WAITS = 1000 - LLD_MSGS = {'gpsLocationExternal', 'cameraOdometry', 'carState', 'sensorEvents', 'liveCalibration'} + LLD_MSGS = ['gpsLocationExternal', 'cameraOdometry', 'carState', 'sensorEvents', 'liveCalibration'] def setUp(self): random.seed(123489234) diff --git a/selfdrive/loggerd/uploader.py b/selfdrive/loggerd/uploader.py index 74d45f01c9..baa56356ba 100644 --- a/selfdrive/loggerd/uploader.py +++ b/selfdrive/loggerd/uploader.py @@ -69,8 +69,8 @@ class Uploader(): self.immediate_count = 0 # stats for last successfully uploaded file - self.last_time = 0 - self.last_speed = 0 + self.last_time = 0.0 + self.last_speed = 0.0 self.last_filename = "" self.immediate_folders = ["crash/", "boot/"] diff --git a/selfdrive/loggerd/xattr_cache.py b/selfdrive/loggerd/xattr_cache.py index e721692500..d01bf1aa8a 100644 --- a/selfdrive/loggerd/xattr_cache.py +++ b/selfdrive/loggerd/xattr_cache.py @@ -4,7 +4,7 @@ from common.xattr import getxattr as getattr1 from common.xattr import setxattr as setattr1 cached_attributes: Dict[Tuple, bytes] = {} -def getxattr(path: str, attr_name: bytes) -> bytes: +def getxattr(path: str, attr_name: str) -> bytes: if (path, attr_name) not in cached_attributes: response = getattr1(path, attr_name) cached_attributes[(path, attr_name)] = response diff --git a/selfdrive/modeld/thneed/lib.py b/selfdrive/modeld/thneed/lib.py index dccdfb10ac..38ef3f4262 100644 --- a/selfdrive/modeld/thneed/lib.py +++ b/selfdrive/modeld/thneed/lib.py @@ -23,9 +23,9 @@ def save_thneed(jdat, fn): if 'data' in o: new_weights.append(o['data']) del o['data'] - new_weights = b''.join(new_weights) + new_weights_bytes = b''.join(new_weights) with open(fn, "wb") as f: j = json.dumps(jdat, ensure_ascii=False).encode('latin_1') f.write(struct.pack("I", len(j))) f.write(j) - f.write(new_weights) + f.write(new_weights_bytes) diff --git a/selfdrive/test/process_replay/model_replay.py b/selfdrive/test/process_replay/model_replay.py index 1befb69aa4..ebbf5f72af 100755 --- a/selfdrive/test/process_replay/model_replay.py +++ b/selfdrive/test/process_replay/model_replay.py @@ -61,8 +61,8 @@ def model_replay(lr, frs): log_msgs = [] last_desire = None - recv_cnt = defaultdict(lambda: 0) - frame_idxs = defaultdict(lambda: 0) + recv_cnt = defaultdict(int) + frame_idxs = defaultdict(int) # init modeld with valid calibration cal_msgs = [msg for msg in lr if msg.which() == "liveCalibration"] diff --git a/selfdrive/test/process_replay/test_processes.py b/selfdrive/test/process_replay/test_processes.py index 0a8fb9b454..9d7feac477 100755 --- a/selfdrive/test/process_replay/test_processes.py +++ b/selfdrive/test/process_replay/test_processes.py @@ -2,7 +2,7 @@ import argparse import os import sys -from typing import Any +from typing import Any, Dict from selfdrive.car.car_helpers import interface_names from selfdrive.test.openpilotci import get_url, upload_file @@ -94,7 +94,7 @@ def format_diff(results, ref_commit): diff1 += f"\t\t{diff}\n" failed = True elif len(diff): - cnt = {} + cnt: Dict[str, int] = {} for d in diff: diff2 += f"\t{str(d)}\n" diff --git a/tools/lib/auth.py b/tools/lib/auth.py index 54fb81cc22..086027968f 100755 --- a/tools/lib/auth.py +++ b/tools/lib/auth.py @@ -46,8 +46,8 @@ class ClientRedirectHandler(BaseHTTPRequestHandler): return query = self.path.split('?', 1)[-1] - query = parse_qs(query, keep_blank_values=True) - self.server.query_params = query + query_parsed = parse_qs(query, keep_blank_values=True) + self.server.query_params = query_parsed self.send_response(200) self.send_header('Content-type', 'text/plain') diff --git a/tools/replay/unlog_ci_segment.py b/tools/replay/unlog_ci_segment.py index 5bceef875e..c008a7aeff 100755 --- a/tools/replay/unlog_ci_segment.py +++ b/tools/replay/unlog_ci_segment.py @@ -35,7 +35,7 @@ def replay(route, segment, loop): frame_idx = {m.roadEncodeIdx.frameId: m.roadEncodeIdx.segmentId for m in msgs if m.which() == 'roadEncodeIdx' and m.roadEncodeIdx.type == 'fullHEVC'} socks = {} - lag = 0 + lag = 0.0 i = 0 max_i = len(msgs) - 2 @@ -66,7 +66,7 @@ def replay(route, segment, loop): lag += (next_msg.logMonoTime - msg.logMonoTime) / 1e9 lag -= time.time() - start_time - dt = max(lag, 0) + dt = max(lag, 0.0) lag -= dt time.sleep(dt) diff --git a/tools/sim/bridge.py b/tools/sim/bridge.py index 368e18fd92..8729bd04d8 100755 --- a/tools/sim/bridge.py +++ b/tools/sim/bridge.py @@ -44,8 +44,8 @@ def parse_args(add_args=None): class VehicleState: def __init__(self): - self.speed = 0 - self.angle = 0 + self.speed = 0.0 + self.angle = 0.0 self.bearing_deg = 0.0 self.vel = carla.Vector3D() self.cruise_button = 0 @@ -111,7 +111,7 @@ class Camerad: rgb_cl = cl_array.to_device(self.queue, rgb) yuv_cl = cl_array.empty_like(rgb_cl) self.krnl(self.queue, (np.int32(self.Wdiv4), np.int32(self.Hdiv4)), None, rgb_cl.data, yuv_cl.data).wait() - yuv = np.resize(yuv_cl.get(), np.int32(rgb.size / 2)) + yuv = np.resize(yuv_cl.get(), rgb.size // 2) eof = int(frame_id * 0.05 * 1e9) # TODO: remove RGB send once the last RGB vipc subscriber is removed @@ -397,7 +397,7 @@ class CarlaBridge: cruise_button = 0 throttle_out = steer_out = brake_out = 0.0 - throttle_op = steer_op = brake_op = 0 + throttle_op = steer_op = brake_op = 0.0 throttle_manual = steer_manual = brake_manual = 0.0 # --------------Step 1-------------------------------