Skip to content

Commit 10b72a0

Browse files
committed
Python: Fix scope of type parameters
This takes care of scoping for type parameters on functions, but not type aliases or classes. For classes, the _type parameters_ now have the correct `Class` as scope, but all their child nodes do not (e.g. the `Name` inside a `TypeParameter`). This has to do with how the `py_scopes` relation is emitted by the extractor, since `Name`s are expressions.
1 parent 3620110 commit 10b72a0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,12 @@ class TypeParameter extends TypeParameter_, AstNode {
229229
override AstNode getAChildNode() { none() }
230230

231231
override Scope getScope() {
232-
// `TypeParameter`s are children of `TypeParameterList`s which are children of `Function`s, `ClassExpr`s, and `TypeAlias`es.
232+
exists(Function f | this = f.getATypeParameter() and result = f)
233+
or
234+
exists(ClassExpr c | this = c.getATypeParameter() and result = c.getInnerScope())
235+
or
233236
// 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()
237+
exists(TypeAlias t | this = t.getATypeParameter() and result = t.getScope())
235238
}
236239

237240
/** Gets the location of this element */

0 commit comments

Comments
 (0)