|
| 1 | +/** Definitions for reasoning about the expected first argument names for methods. */ |
| 2 | + |
| 3 | +import python |
| 4 | +import semmle.python.ApiGraphs |
| 5 | +import semmle.python.dataflow.new.internal.DataFlowDispatch |
| 6 | +import DataFlow |
| 7 | + |
| 8 | +/** Holds if `f` is a method of the class `c`. */ |
| 9 | +private predicate methodOfClass(Function f, Class c) { |
| 10 | + exists(FunctionDef d | d.getDefinedFunction() = f and d.getScope() = c) |
| 11 | +} |
| 12 | + |
| 13 | +/** Holds if `c` is a metaclass. */ |
| 14 | +private predicate isMetaclass(Class c) { |
| 15 | + c = API::builtin("type").getASubclass*().asSource().asExpr().(ClassExpr).getInnerScope() |
| 16 | +} |
| 17 | + |
| 18 | +/** Holds if `c` is a Zope interface. */ |
| 19 | +private predicate isZopeInterface(Class c) { |
| 20 | + c = |
| 21 | + API::moduleImport("zope") |
| 22 | + .getMember("interface") |
| 23 | + .getMember("Interface") |
| 24 | + .getASubclass*() |
| 25 | + .asSource() |
| 26 | + .asExpr() |
| 27 | + .(ClassExpr) |
| 28 | + .getInnerScope() |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Holds if `f` is used in the initialisation of `c`. |
| 33 | + * This means `f` isn't being used as a normal method. |
| 34 | + * Ideally it should be a `@staticmethod`; however this wasn't possible prior to Python 3.10. |
| 35 | + * We exclude this case from the `not-named-self` query. |
| 36 | + * However there is potential for a new query that specifically covers and alerts for this case. |
| 37 | + */ |
| 38 | +private predicate usedInInit(Function f, Class c) { |
| 39 | + methodOfClass(f, c) and |
| 40 | + exists(Call call | |
| 41 | + call.getScope() = c and |
| 42 | + DataFlow::localFlow(DataFlow::exprNode(f.getDefinition()), DataFlow::exprNode(call.getFunc())) |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 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 | + |
| 55 | +/** Holds if the first parameter of `f` should be named `self`. */ |
| 56 | +predicate shouldBeSelf(Function f, Class c) { |
| 57 | + methodOfClass(f, c) and |
| 58 | + not isStaticmethod(f) and |
| 59 | + not isClassmethod(f) and |
| 60 | + not isMetaclass(c) and |
| 61 | + not isZopeInterface(c) and |
| 62 | + not usedInInit(f, c) and |
| 63 | + not noArgsWithDecorator(f) |
| 64 | +} |
| 65 | + |
| 66 | +/** Holds if the first parameter of `f` should be named `cls`. */ |
| 67 | +predicate shouldBeCls(Function f, Class c) { |
| 68 | + methodOfClass(f, c) and |
| 69 | + not isStaticmethod(f) and |
| 70 | + ( |
| 71 | + isClassmethod(f) and not isMetaclass(c) |
| 72 | + or |
| 73 | + isMetaclass(c) and not isClassmethod(f) |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +/** Holds if the first parameter of `f` is named `self`. */ |
| 78 | +predicate firstArgNamedSelf(Function f) { f.getArgName(0) = "self" } |
| 79 | + |
| 80 | +/** Holds if the first parameter of `f` refers to the class - it is either named `cls`, or it is named `self` and is a method of a metaclass. */ |
| 81 | +predicate firstArgRefersToCls(Function f, Class c) { |
| 82 | + methodOfClass(f, c) and |
| 83 | + exists(string argname | argname = f.getArgName(0) | |
| 84 | + argname = "cls" |
| 85 | + or |
| 86 | + /* Not PEP8, but relatively common */ |
| 87 | + argname = "mcls" |
| 88 | + or |
| 89 | + /* If c is a metaclass, allow arguments named `self`. */ |
| 90 | + argname = "self" and |
| 91 | + isMetaclass(c) |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +/** Holds if the first parameter of `f` should be named `self`, but isn't. */ |
| 96 | +predicate firstArgShouldBeNamedSelfAndIsnt(Function f) { |
| 97 | + shouldBeSelf(f, _) and |
| 98 | + not firstArgNamedSelf(f) |
| 99 | +} |
| 100 | + |
| 101 | +/** Holds if the first parameter of `f` should be named `cls`, but isn't. */ |
| 102 | +predicate firstArgShouldReferToClsAndDoesnt(Function f) { |
| 103 | + exists(Class c | |
| 104 | + methodOfClass(f, c) and |
| 105 | + shouldBeCls(f, c) and |
| 106 | + not firstArgRefersToCls(f, c) |
| 107 | + ) |
| 108 | +} |
0 commit comments