Skip to content

Commit 1b9b84c

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 1b9b84c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,25 @@ 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) {
2007+
exists(getAnAttributeValue(cls, name))
2008+
}
2009+
2010+
/**
2011+
* Gets the value expression assigned to attribute `name` directly in the class body of `cls`.
2012+
*/
2013+
Expr getAnAttributeValue(Class cls, string name) {
2014+
exists(Assign a |
2015+
a.getScope() = cls and
2016+
a.getATarget().(Name).getId() = name and
2017+
result = a.getValue()
2018+
)
2019+
}
2020+
20022021
/**
20032022
* Holds if `cls` is callable, i.e. it declares `__call__`.
20042023
*/

0 commit comments

Comments
 (0)