diff --git a/new_holders_flat.py b/new_holders_flat.py index 9555d4b0..b87bf3c8 100644 --- a/new_holders_flat.py +++ b/new_holders_flat.py @@ -37,8 +37,8 @@ class Diode2(Module): def bridge(self): return can_bridge_defined(self.anode, self.cathode) - def _init(self): - print("Called Diode post_init") + def __finit__(self): + print("Called Diode __finit__") # anonymous dynamic trait self.add( @@ -52,21 +52,21 @@ def _init(self): class LED2(Diode2): color: TBD[float] - def _init(self): - print("Called LED post_init") + def __finit__(self): + print("Called LED __finit__") class LED2_NOINT(LED2, init=False): - def _init(self): - print("Called LED_NOINT post_init") + def __finit__(self): + print("Called LED_NOINT __finit__") class LED2_WITHEXTRAT_IFS(LED2): extra: list[Electrical] = field(default_factory=lambda: times(2, Electrical)) extra2: list[Electrical] = if_list(Electrical, 2) - def _init(self): - print("Called LED_WITHEXTRAT_IFS post_init") + def __finit__(self): + print("Called LED_WITHEXTRAT_IFS __finit__") print("Diode init ----") diff --git a/src/faebryk/core/node.py b/src/faebryk/core/node.py index 99fb33c6..d09d1fa0 100644 --- a/src/faebryk/core/node.py +++ b/src/faebryk/core/node.py @@ -1,7 +1,7 @@ # This file is part of the faebryk project # SPDX-License-Identifier: MIT import logging -from dataclasses import field, fields, is_dataclass +from dataclasses import Field, field, fields, is_dataclass from itertools import chain from typing import TYPE_CHECKING, Any, Callable, Type @@ -58,13 +58,12 @@ def d_field(default_factory: Callable[[], Any], **kwargs): # ----------------------------------------------------------------------------- -@dataclass class Node(FaebrykLibObject): - runtime_anon: list["Node"] = field(default_factory=list) - runtime: dict[str, "Node"] = field(default_factory=dict) - specialized: list["Node"] = field(default_factory=list) + runtime_anon: list["Node"] + runtime: dict[str, "Node"] + specialized: list["Node"] - self_gif: GraphInterface = d_field(GraphInterfaceSelf) + self_gif: GraphInterface children: GraphInterfaceHierarchical = d_field( lambda: GraphInterfaceHierarchical(is_parent=True) ) @@ -72,6 +71,8 @@ class Node(FaebrykLibObject): lambda: GraphInterfaceHierarchical(is_parent=False) ) + _init: bool = False + def __hash__(self) -> int: raise NotImplementedError() @@ -105,37 +106,33 @@ def add( self._handle_add_node(name, obj) - _init: bool = False - def __init_subclass__(cls, *, init: bool = True) -> None: - print("Called Node __subclass__", "-" * 20) + print("Called Node __subclass__", "-" * 20, cls.__qualname__) super().__init_subclass__() - # cls_d = dataclass(init=False, kw_only=True)(cls) - # print(is_dataclass(cls_d)) + cls._init = init for name, obj in chain( - vars(cls).items(), [(f.name, f.type) for f in fields(cls)] if is_dataclass(cls) else [], - cls.__annotations__.items(), - [(name, f) for name, f in vars(cls).items() if isinstance(f, rt_field)], + *[ + base.__annotations__.items() + for base in cls.__mro__ + if hasattr(base, "__annotations__") + ], + [ + (name, f) + for name, f in vars(cls).items() + if isinstance(f, (rt_field, Field)) + ], ): if name.startswith("_"): continue print(f"{cls.__qualname__}.{name} = {obj}, {type(obj)}") - # node_fields = [ - # f - # for f in fields(cls) - # if not f.name.startswith("_") and issubclass(f.type, (Node, Node2)) - # ] - # for f in node_fields: - # print(f"{cls.__qualname__}.{f.name} = {f.type.__qualname__}") - # NOTES: # - first construct than call handle (for eliminating hazards) - def __post_init__(self) -> None: + def __init__(self) -> None: print("Called Node init", "-" * 20) if self._init: for base in reversed(type(self).mro()):