|
16 | 16 | import python |
17 | 17 | import semmle.python.libraries.Zope |
18 | 18 |
|
19 | | -predicate first_arg_self(Function f) { |
20 | | - f.getArgName(0) = "self" |
| 19 | +predicate is_type_method(FunctionValue fv) { |
| 20 | + exists(ClassValue c | c.declaredAttribute(_) = fv and c.getASuperType() = ClassValue::type()) |
21 | 21 | } |
22 | 22 |
|
23 | | -predicate is_type_method(FunctionObject f) { |
24 | | - exists(ClassObject c | c.lookupAttribute(_) = f and c.getASuperType() = theTypeType()) |
25 | | -} |
26 | | - |
27 | | -predicate used_in_defining_scope(FunctionObject f) { |
28 | | - exists(Call c | |
29 | | - c.getScope() = f.getFunction().getScope() and |
30 | | - c.getFunc().refersTo(f) |
| 23 | +predicate used_in_defining_scope(FunctionValue fv) { |
| 24 | + exists(Call c | |
| 25 | + c.getScope() = fv.getScope().getScope() and c.getFunc().pointsTo(fv) |
31 | 26 | ) |
32 | 27 | } |
33 | 28 |
|
34 | | -from Function f, PyFunctionObject func, string message |
| 29 | +from Function f, FunctionValue fv, string message |
35 | 30 | where |
36 | | -exists(ClassObject cls, string name | |
37 | | - cls.declaredAttribute(name) = func and cls.isNewStyle() and |
| 31 | +exists(ClassValue cls, string name | |
| 32 | + cls.declaredAttribute(name) = fv and cls.isNewStyle() and |
38 | 33 | not name = "__new__" and |
39 | 34 | not name = "__metaclass__" and |
40 | 35 | /* declared in scope */ |
41 | | - f.getScope() = cls.getPyClass() |
| 36 | + f.getScope() = cls.getScope() |
42 | 37 | ) and |
43 | | -not first_arg_self(f) and not is_type_method(func) and |
44 | | -func.getFunction() = f and not f.getName() = "lambda" and |
45 | | -not used_in_defining_scope(func) and |
| 38 | +not f.getArgName(0) = "self" and |
| 39 | +not is_type_method(fv) and |
| 40 | +fv.getScope() = f and |
| 41 | +not f.getName() = "lambda" and |
| 42 | +not used_in_defining_scope(fv) and |
46 | 43 | ( |
47 | 44 | if exists(f.getArgName(0)) then |
48 | 45 | message = "Normal methods should have 'self', rather than '" + f.getArgName(0) + "', as their first parameter." |
49 | 46 | else |
50 | | - message = "Normal methods should have at least one parameter (the first of which should be 'self')." and not f.hasVarArg() |
| 47 | + message = "Normal methods should have at least one parameter (the first of which should be 'self')." and |
| 48 | + not f.hasVarArg() |
51 | 49 | ) and |
52 | | -not func instanceof ZopeInterfaceMethod |
| 50 | +not fv instanceof ZopeInterfaceMethodValue |
53 | 51 |
|
54 | 52 | select f, message |
0 commit comments