Exclude some decorators

This commit is contained in:
Joe Farebrother
2025-02-12 09:40:45 +00:00
parent 61d5a692fb
commit f46a2a1773
2 changed files with 19 additions and 1 deletions

View File

@@ -43,6 +43,15 @@ private predicate usedInInit(Function f, Class c) {
)
}
/**
* Holds if `f` has no arguments, and also has a decorator.
* We assume that the decorator affect the method in such a way that a `self` parameter is unneeded.
*/
private predicate noArgsWithDecorator(Function f) {
not exists(f.getAnArg()) and
exists(f.getADecorator())
}
/** Holds if the first parameter of `f` should be named `self`. */
predicate shouldBeSelf(Function f, Class c) {
methodOfClass(f, c) and
@@ -50,7 +59,8 @@ predicate shouldBeSelf(Function f, Class c) {
not isClassmethod(f) and
not isMetaclass(c) and
not isZopeInterface(c) and
not usedInInit(f, c)
not usedInInit(f, c) and
not noArgsWithDecorator(f)
}
/** Holds if the first parameter of `f` should be named `cls`. */

View File

@@ -120,7 +120,15 @@ class Z(zope.interface.Interface):
Z().meth(0)
def weird_decorator(f):
def g(self):
return f()
return g
class B:
@weird_decorator
def f(): # allow no-arg functions with a decorator
pass
# The `__init_subclass__` (introduced in Python 3.6)
# and `__class_getitem__` (introduced in Python 3.7) methods are methods