14
14
from .annotation import (
15
15
is_non_standalone_annotation ,
16
16
prepend_annotations_to_formatted_line ,
17
+ is_abstract_annotation_for_statement ,
17
18
)
18
19
19
20
@@ -26,12 +27,20 @@ def format_block(
26
27
previous_statement_name = None
27
28
formatted_lines = [] # type: FormattedLines
28
29
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 :
31
40
context .annotations .append (statement )
32
- is_first_annotation = len (context .annotations ) == 1
33
- if not is_first_annotation :
41
+ if len (context .annotations ) > 1 :
34
42
continue
43
+
35
44
blank_lines = reconstruct_blank_lines_in_range (
36
45
previously_processed_line_number , get_line (statement ), context
37
46
)
@@ -46,29 +55,35 @@ def format_block(
46
55
blank_lines = _add_extra_blanks_due_to_next_statement (
47
56
blank_lines , statement .data , surrounding_empty_lines_table
48
57
)
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 :
51
63
formatted_lines += blank_lines
52
64
continue
65
+
53
66
if len (context .annotations ) == 0 :
54
67
formatted_lines += blank_lines
68
+
55
69
lines , previously_processed_line_number = statement_formatter (
56
70
statement , context
57
71
)
58
72
if len (context .annotations ) > 0 :
59
73
lines = prepend_annotations_to_formatted_line (lines [0 ], context ) + lines [1 :]
60
74
formatted_lines += lines
61
75
previous_statement_name = statement .data
76
+
77
+ # Handle end of block
62
78
dedent_line_number = _find_dedent_line_number (
63
79
previously_processed_line_number , context
64
80
)
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
+ )
67
85
)
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 )
72
87
73
88
74
89
def reconstruct_blank_lines_in_range (
0 commit comments