Skip to content

@staticmethod with implicit object argument type is handled wrongly #3174

@navytux

Description

@navytux

Hello up there. Please consider the following example:

---- 8< ---- moda.pxd

cdef class MyClass:
    @staticmethod
    cdef static_func(x)

---- 8< ---- moda.pyx

cdef class MyClass:
    @staticmethod
    cdef static_func(x):
        return x+1

---- 8< ---- modb.pyx

from moda cimport MyClass

def test():
    x = MyClass.static_func(2)
    print(x)

When compiled and run it fails with:

$ python -c 'import modb; modb.test()'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "modb.pyx", line 4, in modb.test
    x = MyClass.static_func(2)
TypeError: Cannot convert int to moda.MyClass

If I add explicit object to x argument in moda.pxd:

--- a/moda.pxd
+++ b/moda.pxd
@@ -1,3 +1,3 @@
 cdef class MyClass:
     @staticmethod
-    cdef static_func(x)
+    cdef static_func(object x)

It works:

$ python -c 'import modb; modb.test()'
3

Thanks beforehand,
Kirill

/cc @robertwb

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions