@@ -947,6 +947,20 @@ impl SymbolTableBuilder {
947947 } ) => {
948948 self . scan_decorators ( decorator_list, ExpressionContext :: Load ) ?;
949949 self . register_ident ( name, SymbolUsage :: Assigned ) ?;
950+
951+ // When in class scope, save the class's annotation_block before scanning
952+ // function annotations, so method annotations don't interfere with class annotations
953+ let parent_is_class = self
954+ . tables
955+ . last ( )
956+ . map ( |t| t. typ == CompilerScope :: Class )
957+ . unwrap_or ( false ) ;
958+ let saved_annotation_block = if parent_is_class {
959+ self . tables . last_mut ( ) . unwrap ( ) . annotation_block . take ( )
960+ } else {
961+ None
962+ } ;
963+
950964 let has_return_annotation = if let Some ( expression) = returns {
951965 self . scan_annotation ( expression) ?;
952966 true
@@ -971,6 +985,11 @@ impl SymbolTableBuilder {
971985 if type_params. is_some ( ) {
972986 self . leave_scope ( ) ;
973987 }
988+
989+ // Restore class's annotation_block after processing the function
990+ if let Some ( block) = saved_annotation_block {
991+ self . tables . last_mut ( ) . unwrap ( ) . annotation_block = Some ( block) ;
992+ }
974993 }
975994 Stmt :: ClassDef ( StmtClassDef {
976995 name,
@@ -1912,8 +1931,9 @@ impl SymbolTableBuilder {
19121931
19131932 let has_any_annotations = has_param_annotations || has_return_annotation;
19141933
1915- // Only take annotation_block if this function has any annotations
1916- // Otherwise, leave the parent's annotation_block for module-level annotations
1934+ // Take annotation_block if this function has any annotations.
1935+ // When in class scope, the class's annotation_block was saved before scanning
1936+ // function annotations, so the current annotation_block belongs to this function.
19171937 let annotation_block = if has_any_annotations {
19181938 self . tables . last_mut ( ) . unwrap ( ) . annotation_block . take ( )
19191939 } else {
0 commit comments