question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Combining @staticmethod with other decorators is broken

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
linuxhenhaocommented, Apr 4, 2020

For peoplo who facing the same issue here, I got a method to work around this problem. convert a

class A:
    @staticmethod
    @decorator
    def test():
          pass

to

class A:
    def test():
          pass
    test = staticmethod(decorator(test))

A simple implementation of this kind of conversion using ast and astunparse is:

import ast, astunparse


class RewriteFuncdef(ast.NodeTransformer):
    def visit_ClassDef(self, node):
        for index, sub_node in enumerate(node.body):
            if not isinstance(sub_node, ast.FunctionDef):
                continue
                
            decorators = sub_node.decorator_list
            if self.need_rewrite(decorators):
                new_funcdef, assign = self.convert_func_def(sub_node)
                node.body.pop(index)
                node.body.insert(index ,assign)
                node.body.insert(index, new_funcdef)
        return node
                
    def need_rewrite(self, decorators) -> bool:
        for decorator in decorators:
            if isinstance(decorator, ast.Name) and decorator.id == "staticmethod":
                return True
        return False
    
    def convert_func_def(self, func_def):
        new_funcd = ast.FunctionDef(name=func_def.name, body=func_def.body, args=func_def.args, returns=func_def.returns, decorator_list=[])
        decorators = func_def.decorator_list
        a_name = ast.Name(id=func_def.name)
        a_value = self.gen_assign_value(a_name, decorators)
        assign = ast.Assign(targets=[a_name], value=a_value)
        return new_funcd, assign
    
    def gen_assign_value(self, func_name, decorators):
        args = [func_name]
        for decorator in reversed(decorators):
            args = [ast.Call(func=decorator, args=args, keywords=[])]
        return args[0]

module = ast.parse(open("static.py").read())
transformer = RewriteFuncdef()
transformer.visit(module)
generated_content = astunparse.unparse(module)

with open("new_file.py", "w") as f:
      f.write(generated_content)

Hope this can help somebody.

1reaction
da-woodscommented, Aug 31, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Why can @decorator not decorate a staticmethod or a ...
classmethod and staticmethod return descriptor objects, not functions. Most decorators are not designed to accept descriptors.
Read more >
no-staticmethod-decorator / R0203 - Pylint 2.16.0-dev ...
Consider using a decorator instead of calling staticmethod. Description: Used when a static method is defined without using the decorator syntax.
Read more >
Python @staticmethod | How @staticmethod Decorator works
It is a method with no right to access or modify class state. The @staticmethod is a built-in function and simply returns a...
Read more >
@classmethod and @staticmethod Method in Python - STechies
We hope this article has given you a clear idea between static and class methods and how they are different from each other....
Read more >
Define Static Method using @staticmethod Decorator in Python
The @staticmethod is a built-in decorator that defines a static method in the class in Python. A static method doesn't receive any reference...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found