1414from .annotation import (
1515 is_non_standalone_annotation ,
1616 prepend_annotations_to_formatted_line ,
17+ is_abstract_annotation_for_statement ,
1718)
1819
1920
@@ -26,12 +27,20 @@ def format_block(
2627 previous_statement_name = None
2728 formatted_lines = [] # type: FormattedLines
2829 previously_processed_line_number = context .previously_processed_line_number
29- for statement in statements :
30- if is_non_standalone_annotation (statement ):
30+
31+ for i , statement in enumerate (statements ):
32+ # Check if this is an abstract annotation followed by an abstract function or class_name
33+ next_statement = statements [i + 1 ] if i + 1 < len (statements ) else None
34+ is_abstract_for_statement = (
35+ next_statement is not None
36+ and is_abstract_annotation_for_statement (statement , next_statement )
37+ )
38+
39+ if is_non_standalone_annotation (statement ) or is_abstract_for_statement :
3140 context .annotations .append (statement )
32- is_first_annotation = len (context .annotations ) == 1
33- if not is_first_annotation :
41+ if len (context .annotations ) > 1 :
3442 continue
43+
3544 blank_lines = reconstruct_blank_lines_in_range (
3645 previously_processed_line_number , get_line (statement ), context
3746 )
@@ -46,29 +55,35 @@ def format_block(
4655 blank_lines = _add_extra_blanks_due_to_next_statement (
4756 blank_lines , statement .data , surrounding_empty_lines_table
4857 )
49- is_first_annotation = len (context .annotations ) == 1
50- if is_non_standalone_annotation (statement ) and is_first_annotation :
58+
59+ # Handle first annotation case
60+ if (
61+ is_non_standalone_annotation (statement ) or is_abstract_for_statement
62+ ) and len (context .annotations ) == 1 :
5163 formatted_lines += blank_lines
5264 continue
65+
5366 if len (context .annotations ) == 0 :
5467 formatted_lines += blank_lines
68+
5569 lines , previously_processed_line_number = statement_formatter (
5670 statement , context
5771 )
5872 if len (context .annotations ) > 0 :
5973 lines = prepend_annotations_to_formatted_line (lines [0 ], context ) + lines [1 :]
6074 formatted_lines += lines
6175 previous_statement_name = statement .data
76+
77+ # Handle end of block
6278 dedent_line_number = _find_dedent_line_number (
6379 previously_processed_line_number , context
6480 )
65- lines_at_the_end = reconstruct_blank_lines_in_range (
66- previously_processed_line_number , dedent_line_number , context
81+ formatted_lines += _remove_empty_strings_from_end (
82+ reconstruct_blank_lines_in_range (
83+ previously_processed_line_number , dedent_line_number , context
84+ )
6785 )
68- lines_at_the_end = _remove_empty_strings_from_end (lines_at_the_end )
69- formatted_lines += lines_at_the_end
70- previously_processed_line_number = dedent_line_number - 1
71- return (formatted_lines , previously_processed_line_number )
86+ return (formatted_lines , dedent_line_number - 1 )
7287
7388
7489def reconstruct_blank_lines_in_range (
0 commit comments