Skip to content

Commit 03600c9

Browse files
committed
Fix "# pyright: ignore" comment mobility
1 parent de65741 commit 03600c9

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

Diff for: CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
<!-- Changes that affect Black's preview style -->
1616

17+
- Black now avoids breaking up lines containing a comment beginning with "# pyright:
18+
ignore". (#3661)
19+
1720
### Configuration
1821

1922
<!-- Changes to how Black can be configured -->

Diff for: src/black/nodes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,9 @@ def is_type_comment(leaf: Leaf, suffix: str = "") -> bool:
821821
Only returns true for type comments for now."""
822822
t = leaf.type
823823
v = leaf.value
824-
return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:" + suffix)
824+
return t in {token.COMMENT, STANDALONE_COMMENT} and (
825+
v.startswith(f"# pyright:{suffix}") or v.startswith(f"# type:{suffix}")
826+
)
825827

826828

827829
def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None:

Diff for: tests/data/preview/comments7.py

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def func():
187187
0.0789,
188188
a[-1], # type: ignore
189189
)
190+
c = call(0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1]) # pyright: ignore[reportGeneralTypeIssues]
190191
c = call(0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1]) # type: ignore
191192
c = call(
192193
0.0123,

Diff for: tests/data/preview/prefer_rhs_split.py

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@
5555
)
5656

5757

58+
# Make sure unsplittable pyright: ignore comments won't be moved.
59+
some_kind_of_table[some_key] = util.some_function( # pyright: ignore[reportGeneralTypeIssues] # noqa: E501
60+
some_arg
61+
).intersection(pk_cols)
62+
63+
some_kind_of_table[
64+
some_key
65+
] = lambda obj: obj.some_long_named_method() # pyright: ignore[reportGeneralTypeIssues] # noqa: E501
66+
67+
some_kind_of_table[
68+
some_key # pyright: ignore[reportGeneralTypeIssues] # noqa: E501
69+
] = lambda obj: obj.some_long_named_method()
70+
71+
5872
# Make sure unsplittable type ignore won't be moved.
5973
some_kind_of_table[some_key] = util.some_function( # type: ignore # noqa: E501
6074
some_arg

Diff for: tests/data/simple_cases/comments6.py

+26
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ def f(
8686
def func(
8787
a=some_list[0], # type: int
8888
): # type: () -> int
89+
c = call(
90+
0.0123,
91+
0.0456,
92+
0.0789,
93+
0.0123,
94+
0.0456,
95+
0.0789,
96+
0.0123,
97+
0.0456,
98+
0.0789,
99+
a[-1], # pyright: ignore[reportGeneralTypeIssues]
100+
)
101+
89102
c = call(
90103
0.0123,
91104
0.0456,
@@ -99,6 +112,10 @@ def func(
99112
a[-1], # type: ignore
100113
)
101114

115+
c = call(
116+
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # pyright: ignore[reportGeneralTypeIssues]
117+
)
118+
102119
c = call(
103120
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # type: ignore
104121
)
@@ -108,11 +125,20 @@ def func(
108125
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
109126
)
110127

128+
AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # pyright: ignore[reportGeneralTypeIssues]
129+
111130
AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # type: ignore
112131

132+
call_to_some_function_asdf(
133+
foo,
134+
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # pyright: ignore[reportGeneralTypeIssues]
135+
)
136+
113137
call_to_some_function_asdf(
114138
foo,
115139
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # type: ignore
116140
)
117141

142+
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # pyright: ignore[reportGeneralTypeIssues]
143+
118144
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]

0 commit comments

Comments
 (0)