From 6d56b1a61168f1d1cacdcf2ce42e20016e3184d9 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 15 Aug 2024 17:25:05 -0700 Subject: [PATCH] fix that, no dict support --- selfdrive/car/card.py | 7 +++++-- selfdrive/car/structs.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/selfdrive/car/card.py b/selfdrive/car/card.py index f567f29b6a..ad207a792b 100755 --- a/selfdrive/car/card.py +++ b/selfdrive/car/card.py @@ -3,6 +3,7 @@ import capnp import dataclasses import os import time +from typing import Any import cereal.messaging as messaging @@ -60,18 +61,20 @@ def can_comm_callbacks(logcan: messaging.SubSocket, sendcan: messaging.PubSocket return can_recv, can_send -def asdict(obj) -> dict[str, any]: +def asdict(obj) -> dict[str, Any]: """Note that this function returns references rather than copies where possible""" if not dataclasses.is_dataclass(obj): raise TypeError("asdict() should be called on dataclass instances") - def _asdict_inner(obj: any) -> dict[str, any]: + def _asdict_inner(obj): if dataclasses.is_dataclass(obj): ret = {} for f in dataclasses.fields(obj): ret[f.name] = _asdict_inner(getattr(obj, f.name)) return ret + elif isinstance(obj, (tuple, list)): + return type(obj)(_asdict_inner(v) for v in obj) else: return obj diff --git a/selfdrive/car/structs.py b/selfdrive/car/structs.py index c4d7f90c05..b9fca97200 100644 --- a/selfdrive/car/structs.py +++ b/selfdrive/car/structs.py @@ -18,7 +18,7 @@ def auto_dataclass(cls=None, /, **kwargs): origin_typ = get_origin(typ) or typ if isinstance(origin_typ, str): raise TypeError(f"Forward references are not supported for auto_field: '{origin_typ}'. Use a default_factory with lambda instead.") - elif origin_typ in (int, float, str, bytes, list, tuple, set, dict, bool) or is_dataclass(origin_typ): + elif origin_typ in (int, float, str, bytes, list, tuple, set, bool) or is_dataclass(origin_typ): setattr(cls, name, field(default_factory=origin_typ)) elif origin_typ is None: setattr(cls, name, field(default=origin_typ))