Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/ty_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ impl<'db> SemanticIndex<'db> {
self.scopes_by_node.get(&node.node_key()).copied()
}

/// Returns the id of the scope that the node identified by `key` creates.
///
/// This is useful when you have a [`NodeWithScopeKey`] constructed from an
/// [`AstNodeRef`](crate::ast_node_ref::AstNodeRef) and want to avoid loading
/// the parsed module just to look up the scope.
pub(crate) fn node_scope_by_key(&self, key: NodeWithScopeKey) -> FileScopeId {
self.scopes_by_node[&key]
}

/// Checks if there is an import of `__future__.annotations` in the global scope, which affects
/// the logic for type inference.
pub(super) fn has_future_annotations(&self) -> bool {
Expand Down
11 changes: 7 additions & 4 deletions crates/ty_python_semantic/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use ruff_python_ast as ast;
use ruff_python_ast::name::Name;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::node_key::NodeKey;
use crate::semantic_index::definition::{Definition, DefinitionKind};
use crate::semantic_index::scope::{FileScopeId, NodeWithScopeKind, NodeWithScopeRef, ScopeId};
use crate::semantic_index::scope::{FileScopeId, NodeWithScopeKey, NodeWithScopeKind, ScopeId};
use crate::semantic_index::{SemanticIndex, semantic_index};
use crate::types::class::ClassType;
use crate::types::class_base::ClassBase;
Expand All @@ -30,7 +31,6 @@ use crate::types::{
UnionType, declaration_type, walk_type_var_bounds,
};
use crate::{Db, FxOrderMap, FxOrderSet};
use ruff_db::parsed::parsed_module;

/// Returns an iterator of any generic context introduced by the given scope or any enclosing
/// scope.
Expand Down Expand Up @@ -167,8 +167,11 @@ pub(crate) fn typing_self<'db>(
let DefinitionKind::Function(func_ref) = def.kind(db) else {
return None;
};
let module = parsed_module(db, file).load(db);
Some(index.node_scope(NodeWithScopeRef::Function(func_ref.node(&module))))
Some(
index.node_scope_by_key(NodeWithScopeKey::Function(NodeKey::from_node_ref(
func_ref,
))),
)
})
.unwrap_or_else(|| scope_id.file_scope_id(db));

Expand Down