SonarQube incorrectly "hoisting" method declarations

version: Enterprise Edition Version 9.4 (build 54424)

The @decorator above global_f is flagged as a bug and points to the declaration of the method decorator instead of the global function decorator. A screenshot of the message is attached.

This is a false positive since @decorator uses the global function. In my head I envision SonarQube “hoisting” the decorator method, but python does not do that.

def decorator(s):
    def wrapper(method):
        def wrapped(self):
            return method(self)
        return wrapped
    return wrapper

class A:
    def __init__(self):
        self.instance_f = self.decorator("method!")(lambda: print("in instance"))

    def global_f(self):
        print("in global")

    def decorator(self, s):
        def wrapper(method):
            def wrapped():
                return method()
            return wrapped
        return wrapper
>>> a = A()
>>> a.instance_f()
in instance
>>> a.global_f()
in global

Hello @spagh-eddie,

Thanks for the clear reproducer. I believe your assumption is correct and the method is incorrectly hoisted, which leads to the wrong resolution. I created the following ticket to fix this.


Thank you for your response!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.