Skip to content

Commit f46a2a1

Browse files
Exclude some decorators
1 parent 61d5a69 commit f46a2a1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

python/ql/src/Functions/MethodArgNames.qll

+11-1
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,24 @@ private predicate usedInInit(Function f, Class c) {
4343
)
4444
}
4545

46+
/**
47+
* Holds if `f` has no arguments, and also has a decorator.
48+
* We assume that the decorator affect the method in such a way that a `self` parameter is unneeded.
49+
*/
50+
private predicate noArgsWithDecorator(Function f) {
51+
not exists(f.getAnArg()) and
52+
exists(f.getADecorator())
53+
}
54+
4655
/** Holds if the first parameter of `f` should be named `self`. */
4756
predicate shouldBeSelf(Function f, Class c) {
4857
methodOfClass(f, c) and
4958
not isStaticmethod(f) and
5059
not isClassmethod(f) and
5160
not isMetaclass(c) and
5261
not isZopeInterface(c) and
53-
not usedInInit(f, c)
62+
not usedInInit(f, c) and
63+
not noArgsWithDecorator(f)
5464
}
5565

5666
/** Holds if the first parameter of `f` should be named `cls`. */

python/ql/test/query-tests/Functions/methodArgNames/parameter_names.py

+8
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ def meth(arg):
120120

121121
Z().meth(0)
122122

123+
def weird_decorator(f):
124+
def g(self):
125+
return f()
126+
return g
123127

128+
class B:
129+
@weird_decorator
130+
def f(): # allow no-arg functions with a decorator
131+
pass
124132

125133
# The `__init_subclass__` (introduced in Python 3.6)
126134
# and `__class_getitem__` (introduced in Python 3.7) methods are methods

0 commit comments

Comments
 (0)