Skip to content

Commit e8adef1

Browse files
Update to inline expectations + fixes
1 parent aa2c84e commit e8adef1

File tree

8 files changed

+44
-25
lines changed

8 files changed

+44
-25
lines changed

python/ql/src/Functions/MethodArgNames.qll

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private predicate methodOfClass(Function f, Class c) { f.getScope() = c }
99

1010
/** Holds if `c` is a metaclass. */
1111
private predicate isMetaclass(Class c) {
12-
c.getABase() = API::builtin("type").getASubclass*().asSource().asExpr()
12+
c = API::builtin("type").getASubclass*().asSource().asExpr().(ClassExpr).getInnerScope()
1313
}
1414

1515
/** Holds if `f` is a class method. */
@@ -26,13 +26,15 @@ private predicate isStaticMethod(Function f) {
2626

2727
/** Holds if `c` is a Zope interface. */
2828
private predicate isZopeInterface(Class c) {
29-
c.getABase() =
30-
API::moduleImport("zone")
31-
.getMember("interface")
29+
c =
30+
API::moduleImport("zope")
3231
.getMember("interface")
32+
.getMember("Interface")
3333
.getASubclass*()
3434
.asSource()
3535
.asExpr()
36+
.(ClassExpr)
37+
.getInnerScope()
3638
}
3739

3840
/**

python/ql/test/query-tests/Functions/methodArgNames/NonCls.expected

-3
This file was deleted.

python/ql/test/query-tests/Functions/methodArgNames/NonCls.qlref

-1
This file was deleted.

python/ql/test/query-tests/Functions/methodArgNames/NonSelf.expected

-4
This file was deleted.

python/ql/test/query-tests/Functions/methodArgNames/NonSelf.qlref

-1
This file was deleted.

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,47 @@ def n_smethod(ok):
1414

1515
# not ok
1616
@classmethod
17-
def n_cmethod(self):
17+
def n_cmethod(self): # $shouldBeCls
1818
pass
1919

2020
# not ok
2121
@classmethod
22-
def n_cmethod2():
22+
def n_cmethod2(): # $shouldBeCls
2323
pass
2424

25-
# this is allowed because it has a decorator other than @classmethod
2625
@classmethod
2726
@id
28-
def n_suppress(any_name):
27+
def n_dec(any_name): # $shouldBeCls
2928
pass
3029

3130

31+
# Metaclass - normal methods should be named cls, though self is also accepted
3232
class Class(type):
3333

3434
def __init__(cls):
3535
pass
3636

37-
def c_method(y):
37+
def c_method(y): # $shouldBeCls
3838
pass
3939

4040
def c_ok(cls):
4141
pass
4242

43-
@id
44-
def c_suppress(any_name):
43+
# technically we could alert on mixing self for metaclasses with cls for metaclasses in the same codebase,
44+
# but it's probably not too significant.
45+
def c_self_ok(self):
4546
pass
4647

4748

4849
class NonSelf(object):
4950

50-
def __init__(x):
51+
def __init__(x): # $shouldBeSelf
5152
pass
5253

53-
def s_method(y):
54+
def s_method(y): # $shouldBeSelf
5455
pass
5556

56-
def s_method2():
57+
def s_method2(): # $shouldBeSelf
5758
pass
5859

5960
def s_ok(self):
@@ -67,11 +68,12 @@ def s_smethod(ok):
6768
def s_cmethod(cls):
6869
pass
6970

70-
def s_smethod2(ok):
71+
# we allow methods that are used in class initialization, but only detect this case when they are called.
72+
def s_smethod2(ok): # $ SPURIOUS: shouldBeSelf
7173
pass
7274
s_smethod2 = staticmethod(s_smethod2)
7375

74-
def s_cmethod2(cls):
76+
def s_cmethod2(cls): # $ SPURIOUS: shouldBeSelf
7577
pass
7678
s_cmethod2 = classmethod(s_cmethod2)
7779

python/ql/test/query-tests/Functions/methodArgNames/test.expected

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import python
2+
import Functions.MethodArgNames
3+
import utils.test.InlineExpectationsTest
4+
5+
module MethodArgTest implements TestSig {
6+
string getARelevantTag() { result = ["shouldBeSelf", "shouldBeCls"] }
7+
8+
predicate hasActualResult(Location location, string element, string tag, string value) {
9+
exists(Function f |
10+
element = f.toString() and
11+
location = f.getLocation() and
12+
value = "" and
13+
(
14+
firstArgShouldBeNamedSelfAndIsnt(f) and
15+
tag = "shouldBeSelf"
16+
or
17+
firstArgShouldBeNamedClsAndIsnt(f) and
18+
tag = "shouldBeCls"
19+
)
20+
)
21+
}
22+
}
23+
24+
import MakeTest<MethodArgTest>

0 commit comments

Comments
 (0)