Skip to content

Instantly share code, notes, and snippets.

@juliusHuelsmann
Created September 1, 2021 14:39
Show Gist options
  • Save juliusHuelsmann/01558ee5d9d4cfd97e39cc79a9f37c18 to your computer and use it in GitHub Desktop.
Save juliusHuelsmann/01558ee5d9d4cfd97e39cc79a9f37c18 to your computer and use it in GitHub Desktop.
Cython decorator ordering bug workaround
# noinspection PyPep8Naming
class SM(staticmethod):
"""
Quick workaround that allows us to use Cython and multiple decorators.
"""
def __init__(self, *args, **kwargs):
super(staticmethod, self).__init__(*args, **kwargs)
self.__inds_static_hack__ = args[0]
__static = globals()['__builtins__'].staticmethod = SM
# This has to be done for every decorator
def deco(_fun):
fun = getattr(_fun, '__inds_static_hack__', _fun)
def f(*args, **kwargs):
return fun(*args, **kwargs)
f.__name__ = fun.__name__ # etc...
return f
class C:
@staticmethod
@deco
def f():
return 'f_return_value'
@deco
@staticmethod
def g():
return 'g_return_value'
print(f'both is fine : {C.f.__name__} returns {C.f()}, \n'
f' {C.g.__name__} returns {C.g()}. \n'
f'This can also be used in a different file.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment