Skip to content

Commit 34f7a04

Browse files
authored
[ty] Handle Definitions in SemanticModel::scope (#21919)
1 parent c9fe4e2 commit 34f7a04

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

crates/ty_python_semantic/src/semantic_model.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,45 @@ impl<'db> SemanticModel<'db> {
239239
completions
240240
}
241241

242-
/// Get the scope of the given node (handles string annotations)
242+
/// Returns the scope in which `node` is defined (handles string annotations).
243243
pub fn scope(&self, node: ast::AnyNodeRef<'_>) -> Option<FileScopeId> {
244244
let index = semantic_index(self.db, self.file);
245245
match self.node_in_ast(node) {
246246
ast::AnyNodeRef::Identifier(identifier) => index.try_expression_scope_id(identifier),
247+
248+
// Nodes implementing `HasDefinition`
249+
ast::AnyNodeRef::StmtFunctionDef(function) => Some(
250+
function
251+
.definition(self)
252+
.scope(self.db)
253+
.file_scope_id(self.db),
254+
),
255+
ast::AnyNodeRef::StmtClassDef(class) => {
256+
Some(class.definition(self).scope(self.db).file_scope_id(self.db))
257+
}
258+
ast::AnyNodeRef::Parameter(parameter) => Some(
259+
parameter
260+
.definition(self)
261+
.scope(self.db)
262+
.file_scope_id(self.db),
263+
),
264+
ast::AnyNodeRef::ParameterWithDefault(parameter) => Some(
265+
parameter
266+
.definition(self)
267+
.scope(self.db)
268+
.file_scope_id(self.db),
269+
),
270+
ast::AnyNodeRef::ExceptHandlerExceptHandler(handler) => Some(
271+
handler
272+
.definition(self)
273+
.scope(self.db)
274+
.file_scope_id(self.db),
275+
),
276+
ast::AnyNodeRef::TypeParamTypeVar(var) => {
277+
Some(var.definition(self).scope(self.db).file_scope_id(self.db))
278+
}
279+
280+
// Fallback
247281
node => match node.as_expr_ref() {
248282
// If we couldn't identify a specific
249283
// expression that we're in, then just

0 commit comments

Comments
 (0)