support class attributes #102

Closed
opened 2021-11-19 12:55:12 +08:00 by sb10q · 5 comments
Owner

e.g.

class Foo:
   MY_ATTR = 32
e.g. ```python class Foo: MY_ATTR = 32 ```
sb10q added the
low-priority
label 2021-11-19 12:55:12 +08:00
sb10q added the
UROP0622
label 2022-06-24 18:03:41 +08:00
Author
Owner

Needs to be supported in nac3artiq + nac3standalone

Need to find a way to annotate it in nac3artiq. How about something like

class Foo:
   MY_ATTR0 = 32 # host only
   MY_ATTR1: int32 = 32  # host only
   MY_ATTR2: KernelInvariant[int32] = 32  # accessible to the kernel
   MY_ATTR3: Kernel[int32] = 32  # do we want to support this? OK if such attributes are immutable
Needs to be supported in nac3artiq + nac3standalone Need to find a way to annotate it in nac3artiq. How about something like ```python class Foo: MY_ATTR0 = 32 # host only MY_ATTR1: int32 = 32 # host only MY_ATTR2: KernelInvariant[int32] = 32 # accessible to the kernel MY_ATTR3: Kernel[int32] = 32 # do we want to support this? OK if such attributes are immutable ```
Author
Owner

Example code (nac3artiq) that should work but does not:

from artiq.experiment import *
from artiq.coredevice.core import Core


@nac3
class XXX:
    TEST: KernelInvariant[int32] = 32

@nac3
class NAC3Devices(EnvExperiment):
    core: KernelInvariant[Core]

    YYY: KernelInvariant[int32] = 32

    def build(self):
        self.setattr_device("core")

    @kernel
    def run(self):
        print_rpc(XXX.TEST)
        print_rpc(self.YYY)
        print_rpc(NAC3Devices.YYY)
Example code (nac3artiq) that should work but does not: ```python from artiq.experiment import * from artiq.coredevice.core import Core @nac3 class XXX: TEST: KernelInvariant[int32] = 32 @nac3 class NAC3Devices(EnvExperiment): core: KernelInvariant[Core] YYY: KernelInvariant[int32] = 32 def build(self): self.setattr_device("core") @kernel def run(self): print_rpc(XXX.TEST) print_rpc(self.YYY) print_rpc(NAC3Devices.YYY) ```
sb10q removed the
UROP0622
label 2024-06-03 09:44:00 +08:00
abdul124 was assigned by sb10q 2024-06-03 09:57:14 +08:00
abdul124 added reference support_class_attribute_issue102 2024-06-07 17:29:12 +08:00
abdul124 changed reference from support_class_attribute_issue102 to issue-102 2024-06-19 18:05:39 +08:00
Collaborator

Realized by #425

Realized by #425
Author
Owner

Are all the various examples given in this Issue tested and working?

Are all the various examples given in this Issue tested and working?
Collaborator
from min_artiq import *
from numpy import int32

@nac3
class Demo:
    attr1: KernelInvariant[int32] = 2
    attr2: int32 = 4
    attr3: Kernel[int32]

    @kernel
    def __init__(self):
        self.attr3 = 8

@nac3
class NAC3Devices:
    core: KernelInvariant[Core]
    attr4: KernelInvariant[int32] = 16
    
    def __init__(self):
        self.core = Core()

    @kernel
    def run(self):
        Demo.attr1  # Supported
        # Demo.attr2  # Field not accessible on Kernel
        # Demo.attr3  # Only attributes can be accessed in this way
        # Demo.attr1 = 2  # Attributes are immutable

        self.attr4  # Attributes can be accessed within class

        obj = Demo()
        obj.attr1  # Attributes can be accessed by class objects

        NAC3Devices.attr4  # Attributes accessible for classes without __init__

if __name__ == "__main__":
    NAC3Devices().run()

This program shows the current behavior. The attributes are all treated as immutable (whether defined as Kernel or KernelInvariant).

``` from min_artiq import * from numpy import int32 @nac3 class Demo: attr1: KernelInvariant[int32] = 2 attr2: int32 = 4 attr3: Kernel[int32] @kernel def __init__(self): self.attr3 = 8 @nac3 class NAC3Devices: core: KernelInvariant[Core] attr4: KernelInvariant[int32] = 16 def __init__(self): self.core = Core() @kernel def run(self): Demo.attr1 # Supported # Demo.attr2 # Field not accessible on Kernel # Demo.attr3 # Only attributes can be accessed in this way # Demo.attr1 = 2 # Attributes are immutable self.attr4 # Attributes can be accessed within class obj = Demo() obj.attr1 # Attributes can be accessed by class objects NAC3Devices.attr4 # Attributes accessible for classes without __init__ if __name__ == "__main__": NAC3Devices().run() ``` This program shows the current behavior. The attributes are all treated as immutable (whether defined as Kernel or KernelInvariant).
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#102
No description provided.