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:
Taus
2026-02-20 15:56:14 +00:00
parent cd92162920
commit b57e92164c

View File

@@ -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__`.
*/