|
| 1 | +/** Definitions for reasoning about the expected first argument names for methods. */ |
| 2 | + |
| 3 | +import python |
| 4 | +import semmle.python.ApiGraphs |
| 5 | + |
| 6 | +/** Holds if `f` is a method of the class `c`. */ |
| 7 | +private predicate methodOfClass(Function f, Class c) { f.getScope() = c } |
| 8 | + |
| 9 | +/** Holds if `c` is a metaclass. */ |
| 10 | +private predicate isMetaclass(Class c) { |
| 11 | + c.getABase() = API::builtin("type").getASubclass*().asSource().asExpr() |
| 12 | +} |
| 13 | + |
| 14 | +/** Holds if `f` is a class method. */ |
| 15 | +private predicate isClassMethod(Function f) { |
| 16 | + f.getADecorator() = API::builtin("classmethod").asSource().asExpr() |
| 17 | +} |
| 18 | + |
| 19 | +/** Holds if `f` is a static method. */ |
| 20 | +private predicate isStaticMethod(Function f) { |
| 21 | + f.getADecorator() = API::builtin("staticmethod").asSource().asExpr() |
| 22 | +} |
| 23 | + |
| 24 | +/** Holds if `c` is a Zope interface. */ |
| 25 | +private predicate isZopeInterface(Class c) { |
| 26 | + c.getABase() = |
| 27 | + API::moduleImport("zone") |
| 28 | + .getMember("interface") |
| 29 | + .getMember("interface") |
| 30 | + .getASubclass*() |
| 31 | + .asSource() |
| 32 | + .asExpr() |
| 33 | +} |
| 34 | + |
| 35 | +/** Holds if the first parameter of `f` should be named `self`. */ |
| 36 | +predicate shouldBeSelf(Function f, Class c) { |
| 37 | + methodOfClass(f, c) and |
| 38 | + not isStaticMethod(f) and |
| 39 | + not isClassMethod(f) and |
| 40 | + not f.getName() in ["__new__", "__init_subclass__", "__metaclass__", "__class_getitem__"] and |
| 41 | + isMetaclass(c) and |
| 42 | + not isZopeInterface(c) |
| 43 | +} |
| 44 | + |
| 45 | +/** Holds if the first parameter of `f` should be named `cls`. */ |
| 46 | +predicate shouldBeCls(Function f, Class c) { |
| 47 | + methodOfClass(f, c) and |
| 48 | + not isStaticMethod(f) and |
| 49 | + ( |
| 50 | + isClassMethod(f) |
| 51 | + or |
| 52 | + f.getName() in ["__new__", "__init_subclass__", "__metaclass__", "__class_getitem__"] |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +/** Holds if the first parameter of `f` is named `self`. */ |
| 57 | +predicate firstArgNamedSelf(Function f) { f.getArgName(0) = "self" } |
| 58 | + |
| 59 | +/** Holds if the first parameter of `f` is named `cls`. */ |
| 60 | +predicate firstArgNamedCls(Function f) { |
| 61 | + exists(string argname | argname = f.getArgName(0) | |
| 62 | + argname = "cls" |
| 63 | + or |
| 64 | + /* Not PEP8, but relatively common */ |
| 65 | + argname = "mcls" |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +/** Holds if the first parameter of `f` should be named `self`, but isn't. */ |
| 70 | +predicate firstArgShouldBeNamedSelfAndIsnt(Function f) { |
| 71 | + exists(Class c | shouldBeSelf(f, c)) and |
| 72 | + not firstArgNamedSelf(f) |
| 73 | +} |
| 74 | + |
| 75 | +/** Holds if the first parameter of `f` should be named `cls`, but isn't. */ |
| 76 | +predicate firstArgShouldBeNamedClsAndIsnt(Function f) { |
| 77 | + exists(Class c | shouldBeCls(f, c)) and |
| 78 | + not firstArgNamedCls(f) |
| 79 | +} |
0 commit comments