1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from argparse import Namespace
a = Namespace()
(
a
# comment
.b # trailing comment
)
(
a
# comment
.b # trailing dot comment # trailing identifier comment
)
(
a
# comment
.b # trailing identifier comment
)
(
a
# comment
. # trailing dot comment
# in between
b # trailing identifier comment
)
a.aaaaaaaaaaaaaaaaaaaaa.lllllllllllllllllllllllllllloooooooooong.chaaaaaaaaaaaaaaaaaaaaaaiiiiiiiiiiiiiiiiiiiiiiinnnnnnnn.ooooooooooooooooooooooooofffffffff.aaaaaaaaaattr
# Test that we add parentheses around the outermost attribute access in an attribute
# chain if and only if we need them, that is if there are own line comments inside
# the chain.
x10 = (
a
.b
# comment 1
. # comment 2
# comment 3
c
.d
)
x11 = (
a
.b
# comment 1
. # comment 2
# comment 3
c
).d
x12 = (
(
a
.b
# comment 1
. # comment 2
# comment 3
c
)
.d
)
x20 = (
a
.b
)
x21 = (
# leading name own line
a # trailing name end-of-line
.b
)
x22 = (
a
# outermost leading own line
.b # outermost trailing end-of-line
)
x31 = (
a
# own line between nodes 1
.b
)
x321 = (
a
. # end-of-line dot comment
b
)
x322 = (
a
. # end-of-line dot comment 2
b
.c
)
x331 = (
a.
# own line between nodes 3
b
)
x332 = (
""
# own line between nodes
.find
)
x8 = (
(a + a)
.b
)
x51 = (
a.b.c
)
x52 = a.askjdfahdlskjflsajfadhsaf.akjdsf.aksjdlfadhaljsashdfljaf.askjdflhasfdlashdlfaskjfd.asdkjfksahdfkjafs
x53 = (
a.askjdfahdlskjflsajfadhsaf.akjdsf.aksjdlfadhaljsashdfljaf.askjdflhasfdlashdlfaskjfd.asdkjfksahdfkjafs
)
x6 = (
# Check assumption with enclosing nodes
a.b
)
# Regression test for: https://github.com/astral-sh/ruff/issues/6181
(#
()).a
(
(
a # trailing end-of-line
# trailing own-line
) # dangling before dot end-of-line
.b # trailing end-of-line
)
(
(
a
)
# dangling before dot own-line
.b
)
# Regression test for: https://github.com/astral-sh/ruff/issues/7370
result = (
f(111111111111111111111111111111111111111111111111111111111111111111111111111111111)
+ 1
).bit_length()
# Regression test for https://github.com/astral-sh/ruff/issues/16151
result = (
(await query_the_thing(mypy_doesnt_understand)) # type: ignore[x]
.foo()
.bar()
)
(
(
a # trailing end-of-line
# trailing own-line
) # trailing closing parentheses
# dangling before dot
.b # trailing end-of-line
)
# Regression test for https://github.com/astral-sh/ruff/issues/19350
variable = (
(something) # a comment
.first_method("some string")
)
variable = (
something # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
)
if (
(something) # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
): pass
variable = (
(something # a comment
).first_method("some string")
)
if (
(something # a comment
).first_method("some string") # second comment
): pass
variable = ( # 1
# 2
(something) # 3
# 4
.first_method("some string") # 5
# 6
) # 7
if (
(something
# trailing own line on value
)
.first_method("some string")
): ...
variable = (
(something
# 1
) # 2
.first_method("some string")
)