Skip to content

Commit 2c85081

Browse files
committed
Python: Extend DuckTyping module
Adds `overridesMethod` and `isPropertyAccessor`.
1 parent 03a4fc5 commit 2c85081

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,4 +2111,27 @@ module DuckTyping {
21112111
)
21122112
)
21132113
}
2114+
2115+
/**
2116+
* Gets the `__init__` function that will be invoked when `cls` is constructed,
2117+
* resolved according to the MRO.
2118+
*/
2119+
Function getInit(Class cls) { result = invokedFunctionFromClassConstruction(cls, "__init__") }
2120+
2121+
/**
2122+
* Holds if `f` overrides a method in a superclass with the same name.
2123+
*/
2124+
predicate overridesMethod(Function f) {
2125+
exists(Class cls | f.getScope() = cls | hasMethod(getADirectSuperclass(cls), f.getName()))
2126+
}
2127+
2128+
/**
2129+
* Holds if `f` is a property accessor (decorated with `@property`, `@name.setter`,
2130+
* or `@name.deleter`).
2131+
*/
2132+
predicate isPropertyAccessor(Function f) {
2133+
exists(Attribute a | a = f.getADecorator() | a.getName() = "setter" or a.getName() = "deleter")
2134+
or
2135+
f.getADecorator().(Name).getId() = "property"
2136+
}
21142137
}

0 commit comments

Comments
 (0)