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):
            print(s)
            return method(self)
        return wrapped
    return wrapper

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

    @decorator("global!")
    def global_f(self):
        print("in global")

    def decorator(self, s):
        def wrapper(method):
            def wrapped():
                print(s)
                return method()
            return wrapped
        return wrapper
>>> a = A()
>>> a.instance_f()
method!
in instance
>>> a.global_f()
global!
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.

Cheers,
Guillaume

Thank you for your response!

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