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.
This commit is contained in:
Taus
2023-11-20 13:31:21 +00:00
parent 36201105b9
commit 10b72a0c39

View File

@@ -229,9 +229,12 @@ class TypeParameter extends TypeParameter_, AstNode {
override AstNode getAChildNode() { none() }
override Scope getScope() {
// `TypeParameter`s are children of `TypeParameterList`s which are children of `Function`s, `ClassExpr`s, and `TypeAlias`es.
exists(Function f | this = f.getATypeParameter() and result = f)
or
exists(ClassExpr c | this = c.getATypeParameter() and result = c.getInnerScope())
or
// 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
result = this.getParent().getParent().(AstNode).getScope()
exists(TypeAlias t | this = t.getATypeParameter() and result = t.getScope())
}
/** Gets the location of this element */