Skip to content

Commit 815a7ad

Browse files
committed
Python: Add declares/getAttribute API
These could arguably be moved to `Class` itself, but for now I'm choosing to limit the changes to the `DuckTyping` module (until we decide on a proper API).
1 parent 70a67aa commit 815a7ad

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,23 @@ module DuckTyping {
20342034
hasMethod(cls, "__delete__")
20352035
}
20362036

2037+
/**
2038+
* Holds if `cls` directly assigns to an attribute named `name` in its class body.
2039+
* This covers attribute assignments like `x = value`, but not method definitions.
2040+
*/
2041+
predicate declaresAttribute(Class cls, string name) { exists(getAnAttributeValue(cls, name)) }
2042+
2043+
/**
2044+
* Gets the value expression assigned to attribute `name` directly in the class body of `cls`.
2045+
*/
2046+
Expr getAnAttributeValue(Class cls, string name) {
2047+
exists(Assign a |
2048+
a.getScope() = cls and
2049+
a.getATarget().(Name).getId() = name and
2050+
result = a.getValue()
2051+
)
2052+
}
2053+
20372054
/**
20382055
* Holds if `cls` is callable, i.e. it declares `__call__`.
20392056
*/

0 commit comments

Comments
 (0)