Skip to content

Commit

Permalink
Add More Types for mypy (#23268)
Browse files Browse the repository at this point in the history
* mypy passes

* a few more

* a few in manager

* more types, will lint

* more

* simple types

* events type

* Update selfdrive/thermald/thermald.py

* Apply suggestions from code review

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
  • Loading branch information
geohot and adeebshihadeh authored Dec 28, 2021
1 parent 5387806 commit dc96d4b
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 70 deletions.
2 changes: 1 addition & 1 deletion common/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def set_realtime_priority(level: int) -> None:

def set_core_affinity(core: int) -> None:
if not PC:
os.sched_setaffinity(0, [core,])
os.sched_setaffinity(0, [core,]) # type: ignore[attr-defined]


def config_realtime_process(core: int, priority: int) -> None:
Expand Down
2 changes: 1 addition & 1 deletion common/spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def update(self, spinner_text: str):
except BrokenPipeError:
pass

def update_progress(self, cur: int, total: int):
def update_progress(self, cur: float, total: float):
self.update(str(round(100 * cur / total)))

def close(self):
Expand Down
28 changes: 14 additions & 14 deletions selfdrive/controls/lib/events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import IntEnum
from typing import Dict, Union, Callable
from typing import Dict, Union, Callable, List, Optional

from cereal import log, car
import cereal.messaging as messaging
Expand Down Expand Up @@ -42,33 +42,33 @@ class ET:

class Events:
def __init__(self):
self.events = []
self.static_events = []
self.events: List[int] = []
self.static_events: List[int] = []
self.events_prev = dict.fromkeys(EVENTS.keys(), 0)

@property
def names(self):
def names(self) -> List[int]:
return self.events

def __len__(self):
def __len__(self) -> int:
return len(self.events)

def add(self, event_name, static=False):
def add(self, event_name: int, static: bool=False) -> None:
if static:
self.static_events.append(event_name)
self.events.append(event_name)

def clear(self):
def clear(self) -> None:
self.events_prev = {k: (v + 1 if k in self.events else 0) for k, v in self.events_prev.items()}
self.events = self.static_events.copy()

def any(self, event_type):
def any(self, event_type: str) -> bool:
for e in self.events:
if event_type in EVENTS.get(e, {}).keys():
return True
return False

def create_alerts(self, event_types, callback_args=None):
def create_alerts(self, event_types: List[str], callback_args=None):
if callback_args is None:
callback_args = []

Expand Down Expand Up @@ -129,7 +129,7 @@ def __init__(self,
self.creation_delay = creation_delay

self.alert_type = ""
self.event_type = None
self.event_type: Optional[str] = None

def __str__(self) -> str:
return f"{self.alert_text_1}/{self.alert_text_2} {self.priority} {self.visual_alert} {self.audible_alert}"
Expand All @@ -139,14 +139,14 @@ def __gt__(self, alert2) -> bool:


class NoEntryAlert(Alert):
def __init__(self, alert_text_2, visual_alert=VisualAlert.none):
def __init__(self, alert_text_2: str, visual_alert: car.CarControl.HUDControl.VisualAlert=VisualAlert.none):
super().__init__("openpilot Unavailable", alert_text_2, AlertStatus.normal,
AlertSize.mid, Priority.LOW, visual_alert,
AudibleAlert.refuse, 3.)


class SoftDisableAlert(Alert):
def __init__(self, alert_text_2):
def __init__(self, alert_text_2: str):
super().__init__("TAKE CONTROL IMMEDIATELY", alert_text_2,
AlertStatus.userPrompt, AlertSize.full,
Priority.MID, VisualAlert.steerRequired,
Expand All @@ -155,13 +155,13 @@ def __init__(self, alert_text_2):

# less harsh version of SoftDisable, where the condition is user-triggered
class UserSoftDisableAlert(SoftDisableAlert):
def __init__(self, alert_text_2):
def __init__(self, alert_text_2: str):
super().__init__(alert_text_2),
self.alert_text_1 = "openpilot will disengage"


class ImmediateDisableAlert(Alert):
def __init__(self, alert_text_2):
def __init__(self, alert_text_2: str):
super().__init__("TAKE CONTROL IMMEDIATELY", alert_text_2,
AlertStatus.critical, AlertSize.full,
Priority.HIGHEST, VisualAlert.steerRequired,
Expand Down
3 changes: 2 additions & 1 deletion selfdrive/hardware/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from abc import abstractmethod, ABC
from collections import namedtuple
from typing import Dict

ThermalConfig = namedtuple('ThermalConfig', ['cpu', 'gpu', 'mem', 'bat', 'ambient', 'pmic'])

class HardwareBase(ABC):
@staticmethod
def get_cmdline():
def get_cmdline() -> Dict[str, str]:
with open('/proc/cmdline') as f:
cmdline = f.read()
return {kv[0]: kv[1] for kv in [s.split('=') for s in cmdline.split(' ')] if len(kv) == 2}
Expand Down
7 changes: 4 additions & 3 deletions selfdrive/locationd/calibrationd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
import copy
from typing import NoReturn
import numpy as np
import cereal.messaging as messaging
from cereal import log
Expand Down Expand Up @@ -183,11 +184,11 @@ def get_msg(self):
msg.liveCalibration.rpyCalibSpread = [float(x) for x in self.calib_spread]
return msg

def send_data(self, pm):
def send_data(self, pm) -> None:
pm.send('liveCalibration', self.get_msg())


def calibrationd_thread(sm=None, pm=None):
def calibrationd_thread(sm=None, pm=None) -> NoReturn:
if sm is None:
sm = messaging.SubMaster(['cameraOdometry', 'carState'], poll=['cameraOdometry'])

Expand Down Expand Up @@ -215,7 +216,7 @@ def calibrationd_thread(sm=None, pm=None):
calibrator.send_data(pm)


def main(sm=None, pm=None):
def main(sm=None, pm=None) -> NoReturn:
calibrationd_thread(sm, pm)


Expand Down
3 changes: 2 additions & 1 deletion selfdrive/manager/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
PREBUILT = os.path.exists(os.path.join(BASEDIR, 'prebuilt'))


def build(spinner, dirty=False):
def build(spinner: Spinner, dirty: bool = False) -> None:
env = os.environ.copy()
env['SCONS_PROGRESS'] = "1"
nproc = os.cpu_count()
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)
assert scons.stderr is not None

compile_output = []

Expand Down
2 changes: 1 addition & 1 deletion selfdrive/manager/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import signal


def unblock_stdout():
def unblock_stdout() -> None:
# get a non-blocking stdout
child_pid, child_pty = os.forkpty()
if child_pid != 0: # parent
Expand Down
18 changes: 9 additions & 9 deletions selfdrive/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import sys
import traceback
from typing import List, Tuple, Union

import cereal.messaging as messaging
import selfdrive.crash as crash
Expand All @@ -25,7 +26,7 @@
sys.path.append(os.path.join(BASEDIR, "pyextra"))


def manager_init():
def manager_init() -> None:
# update system time from panda
set_time(cloudlog)

Expand All @@ -35,7 +36,7 @@ def manager_init():
params = Params()
params.clear_all(ParamKeyType.CLEAR_ON_MANAGER_START)

default_params = [
default_params: List[Tuple[str, Union[str, bytes]]] = [
("CompletedTrainingVersion", "0"),
("HasAcceptedTerms", "0"),
("OpenpilotEnabledToggle", "1"),
Expand All @@ -56,7 +57,7 @@ def manager_init():

# is this dashcam?
if os.getenv("PASSIVE") is not None:
params.put_bool("Passive", bool(int(os.getenv("PASSIVE"))))
params.put_bool("Passive", bool(int(os.getenv("PASSIVE", "0"))))

if params.get("Passive") is None:
raise Exception("Passive must be set to continue")
Expand Down Expand Up @@ -99,12 +100,12 @@ def manager_init():
device=HARDWARE.get_device_type())


def manager_prepare():
def manager_prepare() -> None:
for p in managed_processes.values():
p.prepare()


def manager_cleanup():
def manager_cleanup() -> None:
# send signals to kill all procs
for p in managed_processes.values():
p.stop(block=False)
Expand All @@ -116,7 +117,7 @@ def manager_cleanup():
cloudlog.info("everything is dead")


def manager_thread():
def manager_thread() -> None:
cloudlog.bind(daemon="manager")
cloudlog.info("manager start")
cloudlog.info({"environ": os.environ})
Expand All @@ -128,8 +129,7 @@ def manager_thread():
ignore += ["manage_athenad", "uploader"]
if os.getenv("NOBOARD") is not None:
ignore.append("pandad")
if os.getenv("BLOCK") is not None:
ignore += os.getenv("BLOCK").split(",")
ignore += [x for x in os.getenv("BLOCK", "").split(",") if len(x) > 0]

ensure_running(managed_processes.values(), started=False, not_run=ignore)

Expand Down Expand Up @@ -176,7 +176,7 @@ def manager_thread():
break


def main():
def main() -> None:
prepare_only = os.getenv("PREPAREONLY") is not None

manager_init()
Expand Down
Loading

0 comments on commit dc96d4b

Please sign in to comment.