Skip to content

Commit cfdeb0e

Browse files
committed
Python: Define getScope and getAChildNode for new nodes
1 parent 75e6de8 commit cfdeb0e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

python/ql/lib/semmle/python/AstExtended.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ class TypeParameter extends TypeParameter_, AstNode {
228228

229229
override AstNode getAChildNode() { none() }
230230

231-
override Scope getScope() { none() }
231+
override Scope getScope() {
232+
// `TypeParameter`s are children of `TypeParameterList`s which are children of `Function`s, `ClassExpr`s, and `TypeAlias`es.
233+
// For `TypeAlias`, this is not quite right. Instead, `TypeAlias`es should define their own scopes, cf. https://docs.python.org/3.12/reference/executionmodel.html#annotation-scopes
234+
result = this.getParent().getParent().(AstNode).getScope()
235+
}
232236

233237
/** Gets the location of this element */
234238
override Location getLocation() { result = TypeParameter_.super.getLocation() }
@@ -249,14 +253,20 @@ class TypeAlias extends TypeAlias_, Stmt {
249253
/** A type variable, with an optional bound, such as `T1` and `T2` in `type T[T1, T2: T3] = T4`. */
250254
class TypeVar extends TypeVar_, TypeParameter {
251255
override Name getName() { result = super.getName() }
256+
257+
override Expr getAChildNode() { result in [this.getName(), this.getBound()] }
252258
}
253259

254260
/** A type var tuple parameter, such as `*T1` in `type T[*T1] = T2`. */
255261
class TypeVarTuple extends TypeVarTuple_, TypeParameter {
256262
override Name getName() { result = super.getName() }
263+
264+
override Expr getAChildNode() { result = this.getName() }
257265
}
258266

259267
/** A param spec parameter, such as `**T1` in `type T[**T1] = T2`. */
260268
class ParamSpec extends ParamSpec_, TypeParameter {
261269
override Name getName() { result = super.getName() }
270+
271+
override Expr getAChildNode() { result = this.getName() }
262272
}

0 commit comments

Comments
 (0)