Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 4673bde

Browse files
committed
Correct bug in parser
The prefix of class declarations should never belong to the `kind`, but the `J.ClassDeclaration`.
1 parent d6c6c55 commit 4673bde

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

rewrite/rewrite/python/_parser_visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def visit_AsyncFunctionDef(self, node):
222222
return self.visit_FunctionDef(node)
223223

224224
def visit_ClassDef(self, node):
225-
prefix = self.__whitespace() if node.decorator_list else Space.EMPTY
225+
prefix = self.__whitespace()
226226
decorators = [self.__map_decorator(d) for d in node.decorator_list]
227227
kind_prefix = self.__source_before('class')
228228
name = self.__convert_name(node.name)

rewrite/rewrite/python/format/normalize_format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def visit_class_declaration(self, cd: ClassDeclaration, p: P) -> J:
2222
cd = cd.with_leading_annotations(Space.format_first_prefix(cd.leading_annotations, Space.EMPTY))
2323
return cd
2424

25+
cd = _concatenate_prefix(cd, cd.padding.kind.prefix)
26+
cd = cd.padding.with_kind(cd.padding.kind.with_prefix(Space.EMPTY))
2527
return cd
2628

2729
def visit_method_declaration(self, md: MethodDeclaration, p: P) -> J:

rewrite/tests/python/all/format/blank_lines_test.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from rewrite.java import Space
2-
from rewrite.python import IntelliJ
2+
from rewrite.python import IntelliJ, NormalizeFormatVisitor
33
from rewrite.python.format import BlankLinesVisitor
44
from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor
55

@@ -19,3 +19,35 @@ def test_remove_leading_module_blank_lines():
1919
spec=RecipeSpec()
2020
.with_recipe(from_visitor(BlankLinesVisitor(IntelliJ.blank_lines())))
2121
)
22+
23+
24+
def test_blank_lines_between_top_level_declarations():
25+
rewrite_run(
26+
# language=python
27+
python(
28+
"""\
29+
class Foo:
30+
pass
31+
class Bar:
32+
pass
33+
def f():
34+
pass
35+
""",
36+
"""\
37+
class Foo:
38+
pass
39+
40+
41+
class Bar:
42+
pass
43+
44+
45+
def f():
46+
pass
47+
"""
48+
),
49+
spec=RecipeSpec()
50+
.with_recipes(
51+
from_visitor(BlankLinesVisitor(IntelliJ.blank_lines()))
52+
)
53+
)

0 commit comments

Comments
 (0)