Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
No dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Aug 22, 2024
1 parent d5e41ca commit 76be49b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
16 changes: 8 additions & 8 deletions new_holders_flat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 ----")
Expand Down
43 changes: 20 additions & 23 deletions src/faebryk/core/node.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -58,20 +58,21 @@ 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)
)
parent: GraphInterfaceHierarchical = d_field(
lambda: GraphInterfaceHierarchical(is_parent=False)
)

_init: bool = False

def __hash__(self) -> int:
raise NotImplementedError()

Expand Down Expand Up @@ -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()):
Expand Down

0 comments on commit 76be49b

Please sign in to comment.