[Libre-soc-bugs] [Bug 906] change HDL code to not use type annotations even for dataclasses

bugzilla-daemon at libre-soc.org bugzilla-daemon at libre-soc.org
Fri Aug 12 08:11:42 BST 2022


https://bugs.libre-soc.org/show_bug.cgi?id=906

--- Comment #2 from Jacob Lifshay <programmerjake at gmail.com> ---
Finished implementing plain_data.

It's nicer to use than dataclass if you have a frozen instance and you write
your own __init__, because it still allows field writes until __init__ finishes
running:

@dataclass(frozen=True)
class MyData:
    a: int
    b: int

    def __init__(self, a, b):
        # insert custom logic here
        # we have to use object.__setattr__ to bypass the frozen-type logic
        object.__setattr__(self, "a", a)
        object.__setattr__(self, "b", b)

@plain_data(frozen=True)
class MyData:
    __slots__ = "a", "b"

    def __init__(self, a, b):
        # insert custom logic here
        self.a = a
        self.b = b

Converted prefix_sum.py to use plain_data instead of dataclass:
https://git.libre-soc.org/?p=nmutil.git;a=blob;f=src/nmutil/prefix_sum.py;h=65f2b33eaf079889590d5aa13ffb6fdb28f1628c;hb=fcbd2f816210cd48879f05ac6d342f93409e03b4#l13

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the libre-soc-bugs mailing list