Why does cython infer float types as objects inside cdef classes?

10 views
Skip to first unread message

trevo...@ups.com

unread,
Jan 10, 2023, 1:15:09 AM1/10/23
to cython-users
According to the docs, int long and float types are interpreted as c types (rather than their python equivalents).

Inside test.pxd:
import cython
cdef class Test:
     cdef float attr
     cdef float test(self)

and insert test.pyx:
import cython

cdef class Test:
    @cython.infer_types.verbose(True)
     cdef float test(self):
         attr = self.attr
         return attr

Capture.PNG
Cython infers attr to be a Python object and performs a PyFloat_FromDouble on attr (implying that cython really thinks self.attr is a double instead of a float as specified inside test.pxd).
Why can't cython infer self.attr to be a float? If I do "cdef float attr = self.attr"  it works just fine. And, if I change the type everywhere to be double instead of float, the type inference works fine.

Currently on Cython version 3.0.0a11
Best, Trevor
Reply all
Reply to author
Forward
0 new messages