From c9af31fd3a359b727ee3302a4c4fa3bafc07aa04 Mon Sep 17 00:00:00 2001 From: Connor Hanley <105237973+hanleyc01@users.noreply.github.com> Date: Sun, 9 Feb 2025 14:03:43 -0500 Subject: [PATCH] Throw attribute error NIRNode constructor (#122) Fix for https://github.com/neuromorphs/NIR/issues/120 Throws `AttributeError` on call `NIRNode.__init__`. --- nir/ir/node.py | 6 +++++- tests/test_ir.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/nir/ir/node.py b/nir/ir/node.py index dc591bd..edb804f 100644 --- a/nir/ir/node.py +++ b/nir/ir/node.py @@ -1,9 +1,10 @@ +from abc import ABC from dataclasses import asdict, dataclass from typing import Any, Dict @dataclass(eq=False) -class NIRNode: +class NIRNode(ABC): """Base superclass of Neural Intermediate Representation Unit (NIR). All NIR primitives inherit from this class, but NIRNodes should never be @@ -16,6 +17,9 @@ class NIRNode: # output_type: Dict[str, np.ndarray] = field(init=False, kw_only=True) # metadata: Dict[str, Any] = field(init=True, default_factory=dict) + def __init__(self) -> None: + raise AttributeError("NIRNode does not have a default constructor.") + def __eq__(self, other): return self is other diff --git a/tests/test_ir.py b/tests/test_ir.py index 1bf2b42..da89dc7 100644 --- a/tests/test_ir.py +++ b/tests/test_ir.py @@ -582,3 +582,13 @@ def test_conv_type_inference(): raise AssertionError(f"type check failed for: {name}") graph.infer_types() assert graph._check_types(), f"type inference failed for: {name}" + + +def test_node(): + try: + node = nir.ir.NIRNode() + assert ( + node is None + ), f"test failed, we should not be able to construct an NIRNode: {node}" + except AttributeError: + pass