Skip to content

Commit 257d392

Browse files
Fix removed comments in stub files (#3745)
1 parent 2593af2 commit 257d392

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
- Fix a bug where an illegal trailing comma was added to return type annotations using
1717
PEP 604 unions (#3735)
18+
- Fix several bugs and crashes where comments in stub files were removed or mishandled
19+
under some circumstances. (#3745)
1820
- Fix a bug where multi-line open parenthesis magic comment like `type: ignore` were not
1921
correctly parsed (#3740)
2022

src/black/nodes.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,11 @@ def is_multiline_string(leaf: Leaf) -> bool:
714714

715715
def is_stub_suite(node: Node) -> bool:
716716
"""Return True if `node` is a suite with a stub body."""
717+
718+
# If there is a comment, we want to keep it.
719+
if node.prefix.strip():
720+
return False
721+
717722
if (
718723
len(node.children) != 4
719724
or node.children[0].type != token.NEWLINE
@@ -722,6 +727,9 @@ def is_stub_suite(node: Node) -> bool:
722727
):
723728
return False
724729

730+
if node.children[3].prefix.strip():
731+
return False
732+
725733
return is_stub_body(node.children[2])
726734

727735

@@ -735,7 +743,8 @@ def is_stub_body(node: LN) -> bool:
735743

736744
child = node.children[0]
737745
return (
738-
child.type == syms.atom
746+
not child.prefix.strip()
747+
and child.type == syms.atom
739748
and len(child.children) == 3
740749
and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
741750
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def f(): # type: ignore
2+
...
3+
4+
class x: # some comment
5+
...
6+
7+
class y:
8+
... # comment
9+
10+
# whitespace doesn't matter (note the next line has a trailing space and tab)
11+
class z:
12+
...
13+
14+
def g():
15+
# hi
16+
...
17+
18+
def h():
19+
...
20+
# bye
21+
22+
# output
23+
24+
def f(): # type: ignore
25+
...
26+
27+
class x: # some comment
28+
...
29+
30+
class y: ... # comment
31+
32+
# whitespace doesn't matter (note the next line has a trailing space and tab)
33+
class z: ...
34+
35+
def g():
36+
# hi
37+
...
38+
39+
def h():
40+
...
41+
# bye

tests/test_format.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ def check_file(
3333
@pytest.mark.parametrize("filename", all_data_cases("simple_cases"))
3434
def test_simple_format(filename: str) -> None:
3535
magic_trailing_comma = filename != "skip_magic_trailing_comma"
36-
check_file(
37-
"simple_cases", filename, black.Mode(magic_trailing_comma=magic_trailing_comma)
36+
mode = black.Mode(
37+
magic_trailing_comma=magic_trailing_comma, is_pyi=filename.endswith("_pyi")
3838
)
39+
check_file("simple_cases", filename, mode)
3940

4041

4142
@pytest.mark.parametrize("filename", all_data_cases("preview"))

0 commit comments

Comments
 (0)