mirror of
https://github.com/github/codeql.git
synced 2026-03-31 12:48:17 +02:00
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).
This commit is contained in:
@@ -2055,6 +2055,23 @@ module DuckTyping {
|
||||
hasMethod(cls, "__delete__")
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if `cls` directly assigns to an attribute named `name` in its class body.
|
||||
* This covers attribute assignments like `x = value`, but not method definitions.
|
||||
*/
|
||||
predicate declaresAttribute(Class cls, string name) { exists(getAnAttributeValue(cls, name)) }
|
||||
|
||||
/**
|
||||
* Gets the value expression assigned to attribute `name` directly in the class body of `cls`.
|
||||
*/
|
||||
Expr getAnAttributeValue(Class cls, string name) {
|
||||
exists(Assign a |
|
||||
a.getScope() = cls and
|
||||
a.getATarget().(Name).getId() = name and
|
||||
result = a.getValue()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if `cls` is callable, i.e. it declares `__call__`.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user