# Numba in a class: TypingError: non-precise type pyobject

**URL:** <https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833>\
**Category:** Support: How do I do ...?\
**Created:** [December 4, 2024, 10:35pm UTC](https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833 "2024-12-04T22:35:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul18fr](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@paul18fr](https://numba.discourse.group/u/paul18fr)\
**Post date:** [December 4, 2024, 10:35pm UTC](https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833/1 "2024-12-04T22:35:29Z")

</div>

Hi,

I’m trying to run the following snippet using numba; some aspects might be clumsy but I do not understand how to specify a type to numba. What am i missing?

Thanks

paul

```auto
from numba import jit, prange
import numpy as np

class test:
    
    def __init__ (self, M: np.ndarray = np.array([])) -> np.ndarray:
        
        self.M = M
        
    @jit(nopython=True, parallel=True)
    def eigenValues(self):
        
        Matrix = self.M
        
        r, c = np.shape(Matrix)
        M_princ = np.zeros( (r, 3), dtype = np.float64)
        
        
        for i in prange(r):  
            A = np.array( [[Matrix[i, 0], Matrix[i, 1], Matrix[i, 2]],
                           [Matrix[i, 1], Matrix[i, 3], Matrix[i, 4]],
                           [Matrix[i, 2], Matrix[i, 4], Matrix[i, 3]]])
            
            val, vect = np.linalg.eig(A)
            index = val.argsort()[::-1]
            M_princ[i, :] = val[index]
        
        return(M_princ)
    
    
    
if __name__ == " __main__":
    
    n = 10
    tensor = 10.25*np.random.rand(n,6)
    
    obj = test(M = tensor)
    eigen_val = obj.eigenValues()

```

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [December 5, 2024, 4:24am UTC](https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833/2 "2024-12-05T04:24:02Z")

</div>

Normal python classes don’t work with numba, can you reformulate the problem without a class?

Or try jitclass/structref. But plain functions are simplest.

---

<div class="post-metadata">

**Author:** ![paul18fr](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@paul18fr](https://numba.discourse.group/u/paul18fr)\
**Post date:** [December 5, 2024, 5:15am UTC](https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833/3 "2024-12-05T05:15:05Z")

</div>

Hi

Thanks for the answer.

No i can’t. The code mimic a current dev using oop.

After digging in internet, i found the following solution; some topic remain unclear for me for the moment, but it seems to work: is it correct? something to improve?

Thanks

Paul

```auto
import numba as nb
from numba.experimental import jitclass
import numpy as np
import time

# https://stackoverflow.com/questions/67458749/how-to-create-a-list-of-numpy-arrays-in-jitclass

float_array = nb.types.Array(dtype = nb.float64, ndim = 2, layout= 'A')
spec = [('M', float_array)]

@jitclass(spec)
class test:
    
    def __init__ (self, M):
        
        self.M = M
        
    def eigenValues(self):
        
        Matrix = self.M
        
        r, c = np.shape(Matrix)
        M_princ = np.zeros( (r, 3), dtype = np.float64)
        
        
        for i in range(r):  
            A = np.array( [[Matrix[i, 0], Matrix[i, 1], Matrix[i, 2]],
                           [Matrix[i, 1], Matrix[i, 3], Matrix[i, 4]],
                           [Matrix[i, 2], Matrix[i, 4], Matrix[i, 3]]])
            
            val, vect = np.linalg.eig(A)
            index = val.argsort()[::-1]
            M_princ[i, :] = val[index]
        
        return(M_princ)
    
    
    
if __name__ == " __main__":
    
    n = 100_000
    tensor = 10.25*np.random.rand(n,6)
    
    obj = test(M = tensor)
    t0 = time.time()
    eigen_val = obj.eigenValues()
    t1 = time.time()
    print(f"Duration = {t1 - t0}")

```

---

<div class="post-metadata">

**Author:** ![paul18fr](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@paul18fr](https://numba.discourse.group/u/paul18fr)\
**Post date:** [December 7, 2024, 10:33pm UTC](https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833/4 "2024-12-07T22:33:22Z")

</div>

Imagine now that i want to pass arguments not using the constructor, but through a method, where can i found informations?

Thanks

```auto
import numba as nb
from numba import int64
from numba.experimental import jitclass
import numpy as np

# https://stackoverflow.com/questions/67458749/how-to-create-a-list-of-numpy-arrays-in-jitclass

float_array = nb.types.Array(dtype = nb.float64, ndim = 2, layout= 'A')
spec = [('M', float_array),
        ('row', int64),
        ('col', int64),]

@jitclass(spec)
class test(object):
    
    # constructor
    def __init__ (self):
        pass
      

    # method    
    def CreateMatrix(self, row = 0, col = 0):
        
        self.M = 10.25*np.random.rand(row, col)

    # method    
    def eigenValues(self):
        
        Matrix = self.M
        
        r, c = np.shape(Matrix)
        M_princ = np.zeros( (r, 3), dtype = np.float64)
        
        
        for i in range(r):  
            A = np.array( [[Matrix[i, 0], Matrix[i, 1], Matrix[i, 2]],
                           [Matrix[i, 1], Matrix[i, 3], Matrix[i, 4]],
                           [Matrix[i, 2], Matrix[i, 4], Matrix[i, 3]]])
            
            val, vect = np.linalg.eig(A)
            index = val.argsort()[::-1]
            M_princ[i, :] = val[index]
        
        return(M_princ)

```

---

<div class="post-metadata">

**Author:** ![DannyWeitekamp](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/dannyweitekamp/32/144_2.png) [@DannyWeitekamp](https://numba.discourse.group/u/DannyWeitekamp)\
**Post date:** [December 8, 2024, 1:39am UTC](https://numba.discourse.group/t/numba-in-a-class-typingerror-non-precise-type-pyobject/2833/5 "2024-12-08T01:39:34Z")

</div>

> [@Exposing StructRef method to Python side](https://numba.discourse.group/t/exposing-structref-method-to-python-side/1893/4):
>
> What you have looks pretty reasonable to me. @DannyWeitekamp’s [CRE](https://github.com/DannyWeitekamp/Cognitive-Rule-Engine) contains an absolute trove of great examples, including a structref generator that I used as a starting point for my own generator.

Perhaps the trick in this thread (4th post) of allocating the object with “new” instead of using the constructor, and then setting each attribute of the object might work. Or you could just call the constructor directly in your method. Note 1) that the linked example uses structref instead of jitclass, I don’t recall if there is a different special new() for jitclass, you can check the jitclass source 2) if you don’t set every attribute when you do this approach then it is possible to have uninitialized values in the object that have undefined behavior.
