Repository: numba/numba

Issue:
Jitclass not differentiated by different field types 
As reported at https://github.com/numba/numba/pull/1433#issuecomment-168996586



Discussion:

Resolving patch:
diff --git a/numba/types.py b/numba/types.py
--- a/numba/types.py
+++ b/numba/types.py
@@ -1417,16 +1417,9 @@ class ClassInstanceType(Type):
     mutable = True
     name_prefix = "instance"
 
-    def __init__(self, class_type, struct, jitmethods, jitprops):
+    def __init__(self, class_type):
         self.class_type = class_type
-        self.struct = struct
-        parameters = tuple((k, v) for k, v in self.struct.items())
-        self.jitmethods = jitmethods
-        self.jitprops = jitprops
-        self.methods = dict((k, v.py_func) for k, v in self.jitmethods.items())
-        name = "{0}.{1}!{2}".format(self.name_prefix,
-                                    self.class_type.name,
-                                    parameters)
+        name = "{0}.{1}".format(self.name_prefix, self.class_type.name)
         super(ClassInstanceType, self).__init__(name)
 
     def get_data_type(self):
@@ -1439,6 +1432,22 @@ def get_reference_type(self):
     def classname(self):
         return self.class_type.class_def.__name__
 
+    @property
+    def jitprops(self):
+        return self.class_type.jitprops
+
+    @property
+    def jitmethods(self):
+        return self.class_type.jitmethods
+
+    @property
+    def struct(self):
+        return self.class_type.struct
+
+    @property
+    def methods(self):
+        return self.class_type.methods
+
 
 class ClassType(Callable, Opaque):
     """
@@ -1453,11 +1462,15 @@ def __init__(self, class_def, ctor_template_cls, struct, jitmethods,
                  jitprops):
         self.class_def = class_def
         self.ctor_template = self._specialize_template(ctor_template_cls)
-        name = "{0}.{1}#{2}".format(self.name_prefix, class_def.__name__,
-                                    id(class_def))
+        self.jitmethods = jitmethods
+        self.jitprops = jitprops
+        self.struct = struct
+        self.methods = dict((k, v.py_func) for k, v in self.jitmethods.items())
+        fielddesc = ','.join("{0}:{1}".format(k, v) for k, v in struct.items())
+        name = "{0}.{1}#{2:x}<{3}>".format(self.name_prefix, class_def.__name__,
+                                           id(class_def), fielddesc)
         super(ClassType, self).__init__(name)
-        self.instance_type = self.instance_type_class(self, struct, jitmethods,
-                                                      jitprops)
+        self.instance_type = self.instance_type_class(self)
 
     def get_call_type(self, context, args, kws):
         return self.ctor_template(context).apply(args, kws)


Test patch:
diff --git a/numba/tests/test_jitclasses.py b/numba/tests/test_jitclasses.py
--- a/numba/tests/test_jitclasses.py
+++ b/numba/tests/test_jitclasses.py
@@ -3,7 +3,7 @@
 import numpy as np
 from numba import (float32, float64, int16, int32, boolean, deferred_type,
                    optional)
-from numba import njit
+from numba import njit, typeof
 from numba import unittest_support as unittest
 from numba import jitclass
 from numba.utils import OrderedDict
@@ -342,6 +342,27 @@ def my_method(self):
             jitclass([('my_method', int32)])(Foo)
         self.assertEqual(str(raises.exception), 'name shadowing: my_method')
 
+    def test_parameterized(self):
+        class MyClass(object):
+            def __init__(self, value):
+                self.value = value
+
+        def create_my_class(value):
+            cls = jitclass([('value', typeof(value))])(MyClass)
+            return cls(value)
+
+        a = create_my_class(123)
+        self.assertEqual(a.value, 123)
+
+        b = create_my_class(12.3)
+        self.assertEqual(b.value, 12.3)
+
+        c = create_my_class(np.array([123]))
+        np.testing.assert_equal(c.value, [123])
+
+        d = create_my_class(np.array([12.3]))
+        np.testing.assert_equal(d.value, [12.3])
+
 
 if __name__ == '__main__':
     unittest.main()