Skip to content

Commit 48e51cd

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 2fce1b3 commit 48e51cd

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
@@ -1999,6 +1999,23 @@ module DuckTyping {
19991999
hasMethod(cls, "__delete__")
20002000
}
20012001

2002+
/**
2003+
* Holds if `cls` directly assigns to an attribute named `name` in its class body.
2004+
* This covers attribute assignments like `x = value`, but not method definitions.
2005+
*/
2006+
predicate declaresAttribute(Class cls, string name) { exists(getAnAttributeValue(cls, name)) }
2007+
2008+
/**
2009+
* Gets the value expression assigned to attribute `name` directly in the class body of `cls`.
2010+
*/
2011+
Expr getAnAttributeValue(Class cls, string name) {
2012+
exists(Assign a |
2013+
a.getScope() = cls and
2014+
a.getATarget().(Name).getId() = name and
2015+
result = a.getValue()
2016+
)
2017+
}
2018+
20022019
/**
20032020
* Holds if `cls` is callable, i.e. it declares `__call__`.
20042021
*/

0 commit comments

Comments
 (0)