2
2
import re
3
3
from keyword import iskeyword
4
4
from textwrap import indent
5
- from typing import Optional
5
+ from typing import List , Optional
6
6
7
7
import isort
8
8
from autoflake import fix_code # type: ignore
@@ -25,13 +25,31 @@ def ast_to_str(
25
25
) -> str :
26
26
"""Convert ast object into string."""
27
27
code = ast .unparse (ast_obj )
28
+ code = remove_blank_line_between_class_and_content (code )
28
29
if remove_unused_imports :
29
30
code = fix_code (code , remove_all_unused_imports = True )
30
31
if multiline_strings :
31
32
code = format_multiline_strings (code , offset = multiline_strings_offset )
32
33
return format_str (isort .code (code ), mode = Mode ())
33
34
34
35
36
+ def remove_blank_line_between_class_and_content (code : str ) -> str :
37
+ """Removes blank lines between class and first method.
38
+
39
+ We are doing this for code style consistency and backwards compatibility.
40
+ """
41
+ code_lines : List [str ] = []
42
+ skip_blank_lines = False
43
+ for line in code .splitlines ():
44
+ if skip_blank_lines and line :
45
+ skip_blank_lines = False
46
+ elif line .startswith ("class " ):
47
+ skip_blank_lines = True
48
+ if not skip_blank_lines or line :
49
+ code_lines .append (line )
50
+ return "\n " .join (code_lines )
51
+
52
+
35
53
def str_to_snake_case (name : str ) -> str :
36
54
"""Converts camelCase or PascalCase string into snake_case."""
37
55
# lower-case letters that optionally start with a single upper-case letter
0 commit comments