MCPcopy Index your code
hub / github.com/RustPython/RustPython / found_in_outer_scope

Method found_in_outer_scope

crates/codegen/src/symboltable.rs:710–800  ·  view source on GitHub ↗
(&mut self, name: &str, st_typ: CompilerScope)

Source from the content-addressed store, hash-verified

708 }
709
710 fn found_in_outer_scope(&mut self, name: &str, st_typ: CompilerScope) -> Option<SymbolScope> {
711 let mut decl_depth = None;
712 for (i, (symbols, typ)) in self.tables.iter().rev().enumerate() {
713 if matches!(typ, CompilerScope::Module)
714 || matches!(typ, CompilerScope::Class if name != "__class__" && name != "__classdict__" && name != "__conditional_annotations__")
715 {
716 continue;
717 }
718
719 // PEP 649: Annotation scope is conceptually a sibling of the function,
720 // not a child. Skip the immediate parent function scope when looking
721 // for outer variables from annotation scope.
722 if st_typ == CompilerScope::Annotation
723 && i == 0
724 && matches!(
725 typ,
726 CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda
727 )
728 {
729 continue;
730 }
731
732 // __class__ and __classdict__ are implicitly declared in class scope
733 // This handles the case where nested scopes reference them
734 if (name == "__class__" || name == "__classdict__")
735 && matches!(typ, CompilerScope::Class)
736 {
737 decl_depth = Some(i);
738 break;
739 }
740
741 // __conditional_annotations__ is implicitly declared in class scope
742 // for classes with conditional annotations
743 if name == "__conditional_annotations__" && matches!(typ, CompilerScope::Class) {
744 decl_depth = Some(i);
745 break;
746 }
747
748 if let Some(sym) = symbols.get(name) {
749 match sym.scope {
750 SymbolScope::GlobalExplicit => return Some(SymbolScope::GlobalExplicit),
751 SymbolScope::GlobalImplicit => {}
752 _ => {
753 if sym.is_bound() {
754 decl_depth = Some(i);
755 break;
756 }
757 }
758 }
759 }
760 }
761
762 if let Some(decl_depth) = decl_depth {
763 // decl_depth is the number of tables between the current one and
764 // the one that declared the cell var
765 // For implicit class scope variables (__classdict__, __conditional_annotations__),
766 // only propagate free to annotation/type-param scopes, not regular functions.
767 // Regular method functions don't need these in their freevars.

Callers 1

analyze_symbolMethod · 0.80

Calls 13

newFunction · 0.85
is_boundMethod · 0.80
get_mutMethod · 0.80
contains_keyMethod · 0.80
SomeClass · 0.50
revMethod · 0.45
iterMethod · 0.45
getMethod · 0.45
takeMethod · 0.45
iter_mutMethod · 0.45
insertMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected