Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Unable to declare a staticmethod func(cython.double v) -> cython.double in pure python #5159

Open
jtoledo1974 opened this issue Dec 1, 2022 · 2 comments

Comments

@jtoledo1974
Copy link

jtoledo1974 commented Dec 1, 2022

Describe the bug

I have the following function in a compilable .py file

@cython.cclass
class TestClass:

    @staticmethod
    @cython.cfunc
    def func(v: cython.double) -> cython.double:
        return v + 1.0

With a corresponding .pxd file having

cdef class TestClass:

    @staticmethod
    cdef double func(double v)

When I try to compile it I get the following error:

(venv311) C:\Users\Juan\cruji-enaire>python setup.py build_ext --inplace
Compiling src/crujisim/cythontests.py because it changed.
[1/1] Cythonizing src/crujisim/cythontests.py

Error compiling Cython file:
------------------------------------------------------------
...
    return a

@cython.cclass
class TestClass:

    @staticmethod
    ^
------------------------------------------------------------

src\crujisim\cythontests.py:515:4: Signature not compatible with previous declaration

Error compiling Cython file:
------------------------------------------------------------
...
cdef class TestClass:

    @staticmethod
    cdef double func(double v)                    ^
------------------------------------------------------------

src\crujisim\cythontests.pxd:4:20: Previous declaration is here
Traceback (most recent call last):
  File "C:\Users\Juan\cruji-enaire\setup.py", line 11, in <module>
    cythonized = cythonize(["src/crujisim/mathutil.py",
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Juan\venv311\Lib\site-packages\Cython\Build\Dependencies.py", line 1129, in cythonize
    cythonize_one(*args)
  File "C:\Users\Juan\venv311\Lib\site-packages\Cython\Build\Dependencies.py", line 1296, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: src/crujisim/cythontests.py

Yet, if I rename the file to pyx and rewrite the function as

cdef class TestClass:

    @staticmethod
    cdef double func(v: double):
        return v + 1.0

It does compile properly

Expected behaviour

I expected the function to compile correctly in pure python, since it is equivalent

Environment

OS: Windows
Python version [e.g. 3.11]
Cython version [e.g. 3.0.0a11]

Additional context

No response

@jtoledo1974
Copy link
Author

I'm also getting a similar thing with another method returning int.

This doesn't work

@cython.cclass
class TestClass:
    @cython.ccall
    def intfunc(self, v: cython.int) -> cython.int:
        return v + 1

With the corresponding pxd file

cdef class TestClass:
    cpdef int intfunc(self, int v)

But this does work with the same .pxd file

cdef class TestClass:
   
    cpdef int intfunc(self, int v):
        return v + 1

@0dminnimda
Copy link
Contributor

This looks like a wrong behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants