Skip to content

Commit b15ceeb

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 2df8973 commit b15ceeb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,23 @@ module DuckTyping {
20552055
hasMethod(cls, "__delete__")
20562056
}
20572057

2058+
/**
2059+
* Holds if `cls` directly assigns to an attribute named `name` in its class body.
2060+
* This covers attribute assignments like `x = value`, but not method definitions.
2061+
*/
2062+
predicate declaresAttribute(Class cls, string name) { exists(getAnAttributeValue(cls, name)) }
2063+
2064+
/**
2065+
* Gets the value expression assigned to attribute `name` directly in the class body of `cls`.
2066+
*/
2067+
Expr getAnAttributeValue(Class cls, string name) {
2068+
exists(Assign a |
2069+
a.getScope() = cls and
2070+
a.getATarget().(Name).getId() = name and
2071+
result = a.getValue()
2072+
)
2073+
}
2074+
20582075
/**
20592076
* Holds if `cls` is callable, i.e. it declares `__call__`.
20602077
*/

0 commit comments

Comments
 (0)