I am trying to return a NumPy array from an NDSP

def test_returnN(self):
    return numpy.array([0,1,2,3])

to a kernel

@kernel
def run(self):
    ...
    #print(self.ixon.test_returnN())    # type mismatch: cannot serialize array([0, 1, 2, 3]) as None (drv annot)
    print(self.ixon_test_returnN())    # proxy: type mismatch: cannot serialize array([0, 1, 2, 3]) as list

via a proxy with a suitable type annotation

def ixon_test_returnN(self) -> TList(TInt32):      # https://m-labs.hk/artiq/manual/compiler.html#remote-procedure-calls
    val = self.ixon.test_returnN()                 # https://m-labs.hk/artiq/sipyco-manual/#module-sipyco.pyon
    print("ixon.returnN returns", type(val), val)       # ixon.returnN returns <class 'numpy.ndarray'> [0 1 2 3]
    print("           base type", type(val[0]), val[0]) # base type <class 'numpy.int32'> 0
    return val

but I get the error message:

ixon.returnN returns <class 'numpy.ndarray'> [0 1 2 3]
           base type <class 'numpy.int32'> 0
Traceback (most recent call last):
...
artiq.coredevice.comm_kernel.RPCReturnValueError: type mismatch: cannot serialize array([0, 1, 2, 3]) as list (<function test_return.ixon_test_returnN at 0x000002985D8C10D8> has returned [0 1 2 3])

I think the NumPy array is supported by sipyco as mentioned in the manual but I'm making a wrong compiler annotation or some other kind of error. Can this error be fixed with some other annotation from the list in the manual? Grateful for any advice.

Thanks

I think RPC from kernels of methods returning NumPy arrays is not implemented. @dpn ?

    They are implemented, but the annotation to use is TArray(element_type, number_of_dimensions), not TList.

      I've just added it to the manual as well. (On second glance, TTuple also appears to be missing.)

      Sorry I can't get it working. I fear the problem might be we are still using ARTIQ 5, but the error is name 'TArray' is not defined and I'm afraid I would get the same error with ARTIQ 6, i.e. I suspect I'm still doing something else wrong.
      I have reduced the example to this simpler snippet

      from artiq.experiment import *
      import numpy
      
      class test_return(EnvExperiment):
          """ARTIQ 5 - NumPy return test"""
          
          def ret_L(self) -> TList(TInt32):
              L = [0,1,2,3]
              print("ret_L returns", type(L), L)
              print("    base type", type(L[0]), L[0])
              return L
      
          #def ret_N(self) -> TList(TInt32):      #           cannot serialize array([0, 1, 2, 3]) as list
          #def ret_N(self) -> TArray(TInt32):     # ARTIQ 5 ? File "test_return_2.py", line 26, in test_return: NameError: name 'TArray' is not defined
          #def ret_N(self) -> TArray(TInt32,1):   # ARTIQ 6 ? File "test_return_2.py", line 26, in test_return: NameError: name 'TArray' is not defined
          def ret_N(self) -> TList(TInt32):
              N = numpy.array([0,1,2,3])                  # When annotated as TList(TInt32):
              print("ret_N returns", type(N), N)          #   ret_N returns <class 'numpy.ndarray'> [0 1 2 3]
              print("    base type", type(N[0]), N[0])    #      base type <class 'numpy.int32'> 0
              return N
      
      
          def build(self):
              self.setattr_device("core")        
              
          @kernel
          def run(self):                                      # artiq.coredevice.comm_kernel.RPCReturnValueError: type mismatch: 
              print(self.ret_L())                             # ret_L returns <class 'list'> [0, 1, 2, 3] // base type <class 'int'> 0 // [0, 1, 2, 3]
              print(self.ret_N())

      The moment I change TList(TInt32) to TArray(blah) I get the TArray not defined error. Looking at ...\envs\artiq[5|6]\Lib\site-packages\artiq\compiler\builtins.py I see

      class TArray(types.TMono):
          def __init__(self, elt=None):

      and

      class TArray(types.TMono):
          def __init__(self, elt=None, num_dims=1):

      respectively, so maybe I'm doing something wrong (other than using ARTIQ 5) to get the TArray not defined error. Is the error reproducible? Must I install ARTIQ 6 to be able to annotate returned NumPy values with TArray?
      Thanks for your patience

      ARTIQ-5 does not support NumPy arrays in kernels. They were introduced in 6.

      (Strictly speaking, there was some rudimentary support for NumPy arrays in ARTIQ 5, but with different behaviour that didn't match host Python – they were treated the same as lists –, and which is not forward-compatible to the actual implementation of them in ARTIQ 6+, where array operations, math functions, etc. are supported.)