cython: Combining @staticmethod with other decorators is broken
Consider
def debug(fun):
print(fun)
return fun
class A(object):
@staticmethod
@debug
def foo(self):
return self
In Python, this correctly prints <function foo at 0x7f4d305df410>.
In Cython, this wrongly prints <staticmethod object at 0x7f4d30e5e718>.
When doing the same with a Cython cdef class, the decorators are somehow applied twice: the output becomes
<staticmethod object at 0x7f4d30e5eb40>
<staticmethod object at 0x7f4d305e52f0>
Migrated from http://trac.cython.org/ticket/880
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 7
- Comments: 15 (7 by maintainers)
For peoplo who facing the same issue here, I got a method to work around this problem. convert a
to
A simple implementation of this kind of conversion using ast and astunparse is:
Hope this can help somebody.
Looks like Cython handles the order of multiple decorators incorrectly.
Python can tell the difference between
A.aandA.bbut Cython can’t.This would cause much trouble since we used to use
functools.wrapsin every decorator to preserve the original signature and docstring butwrapscan only be applied on function object but not staticmethod object.FWIW there is a PR that should fix this issue https://github.com/cython/cython/pull/3966 - testing that PR and letting me know whether it works or not for your would be useful. We don’t really need more comments saying “I hope this is fixed soon” though.
Using the deprecated
abstractstaticmethodis a temp fix for now. Hoping the bug can be fixed soon.Another quick and dirty workaround if you have access to the decorator implementation (and not to the code that uses the decorators):
I override staticmethod, store the information I require and am subsequently able to retrieve them if the decorator order is incorrect
I’m unsure if what I do is advisable, but it works for now 😃