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