ruff_python_formatter 0.0.6

This is an internal component crate of Ruff
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
(aaaaaaaa
    + # trailing operator comment
    b # trailing right comment
)


(aaaaaaaa # trailing left comment
    +  # trailing operator comment
    # leading right comment
    b
)

(
    # leading left most comment
    aaaaaaaa
    +  # trailing operator comment
    # leading b comment
    b # trailing b comment
    # trailing b ownline comment
    +  # trailing second operator comment
    # leading c comment
    c # trailing c comment
    # trailing own line comment
 )


# Black breaks the right side first for the following expressions:
aaaaaaaaaaaaaa + caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal(argument1, argument2, argument3)
aaaaaaaaaaaaaa + [bbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc, dddddddddddddddd, eeeeeee]
aaaaaaaaaaaaaa + (bbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc, dddddddddddddddd, eeeeeee)
aaaaaaaaaaaaaa + { key1:bbbbbbbbbbbbbbbbbbbbbb, key2: ccccccccccccccccccccc, key3: dddddddddddddddd, key4: eeeeeee }
aaaaaaaaaaaaaa + { bbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc, dddddddddddddddd, eeeeeee }
aaaaaaaaaaaaaa + [a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ]
aaaaaaaaaaaaaa + (a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb )
aaaaaaaaaaaaaa + {a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb}

# Wraps it in parentheses if it needs to break both left and right
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + [
    bbbbbbbbbbbbbbbbbbbbbb,
    ccccccccccccccccccccc,
    dddddddddddddddd,
    eee
] # comment



# But only for expressions that have a statement parent.
not (aaaaaaaaaaaaaa + {a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb})
[a + [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] in c ]


# leading comment
(
    # comment
    content + b
)


if (
    aaaaaaaaaaaaaaaaaa +
    # has the child process finished?
    bbbbbbbbbbbbbbb +
    # the child process has finished, but the
    # transport hasn't been notified yet?
    ccccccccccc
):
    pass


# Left only breaks
if [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
] & aaaaaaaaaaaaaaaaaaaaaaaaaa:
    pass

if [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
] & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
    pass

# Right only can break
if aaaaaaaaaaaaaaaaaaaaaaaaaa & [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
]:
    pass

if aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa & [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
]:
    pass


# Left or right can break
if [2222, 333] & [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
]:
    pass

if [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
] & [2222, 333]:
    pass

if [
    aaaaaaaaaaaaa,
    bbbbbbbbbbbbbbbbbbbb,
    cccccccccccccccccccc,
    dddddddddddddddddddd,
    eeeeeeeeee,
] & [fffffffffffffffff, gggggggggggggggggggg, hhhhhhhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjj]:
    pass

if (
    # comment
    [
        aaaaaaaaaaaaa,
        bbbbbbbbbbbbbbbbbbbb,
        cccccccccccccccccccc,
        dddddddddddddddddddd,
        eeeeeeeeee,
    ]
) & [
    fffffffffffffffff,
    gggggggggggggggggggg,
    hhhhhhhhhhhhhhhhhhhhh,
    iiiiiiiiiiiiiiii,
    jjjjjjjjjjjjj,
]:
    pass

    pass

# Nesting
if (aaaa + b) & [
    fffffffffffffffff,
    gggggggggggggggggggg,
    hhhhhhhhhhhhhhhhhhhhh,
    iiiiiiiiiiiiiiii,
    jjjjjjjjjjjjj,
]:
    pass

if [
    fffffffffffffffff,
    gggggggggggggggggggg,
    hhhhhhhhhhhhhhhhhhhhh,
    iiiiiiiiiiiiiiii,
    jjjjjjjjjjjjj,
] & (a + b):
    pass


if [
    fffffffffffffffff,
    gggggggggggggggggggg,
    hhhhhhhhhhhhhhhhhhhhh,
    iiiiiiiiiiiiiiii,
    jjjjjjjjjjjjj,
] & (
    # comment
    a
    + b
):
    pass

if (
    [
        fffffffffffffffff,
        gggggggggggggggggggg,
        hhhhhhhhhhhhhhhhhhhhh,
        iiiiiiiiiiiiiiii,
        jjjjjjjjjjjjj,
    ]
    &
    # comment
    a + b
):
    pass


# Unstable formatting in https://github.com/realtyem/synapse-unraid/blob/unraid_develop/synapse/handlers/presence.py
for user_id in set(target_user_ids) - {u.user_id for u in updates}:
    updates.append(UserPresenceState.default(user_id))

# If either operator is parenthesized, use non-simple formatting.
# See: https://github.com/astral-sh/ruff/issues/7318.
10**(2)
10**2

# Keeps parenthesized left hand sides
(
    log(self.price / self.strike)
    + (self.risk_free - self.div_cont + 0.5 * (self.sigma**2)) * self.exp_time
) / self.sigmaT

# Stability with end-of-line comments between empty tuples and bin op
x = () - (#
)
x = (
    ()
    - ()  #
)
x = (
    () - ()  #
)


# Avoid unnecessary parentheses around multiline strings.
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
""" % (
    self.base_url,
    date.today(),
)

expected_content = (
    """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
"""
    # Needs parentheses
    % (
    self.base_url,
    date.today(),
    )
)

expected_content = (
    """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
"""
    %
    # Needs parentheses
    (
        self.base_url,
        date.today(),
    )
)


expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
""" + a.call.expression(
    self.base_url,
    date.today(),
)

expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
""" + sssssssssssssssssssssssssssssssssssssssssooooo * looooooooooooooooooooooooooooooongggggggggggg

call(arg1, arg2, """
short
""", arg3=True)

expected_content = (
    """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
"""
    %
    (
        self.base_url
    )
)


expected_content = (
    """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod>
</sitemap>
</sitemapindex>
"""
    %
    (
        # Needs parentheses
        self.base_url
    )
)

# Skip FString content when determining whether to omit optional parentheses or not.0
# The below expression should be parenthesized because it ends with an fstring and starts with a name.
# (Call expressions at the beginning don't count as parenthesized because they don't start with parens).
assert (
    format.format_event(spec)
    == f'Event("_remove_cookie", {{key:`testkey`,options:{json.dumps(options)}}})'
)
# Avoid parentheses for this example because it starts with a tuple expression.
assert (
    (spec, format)
    == f'Event("_remove_cookie", {{key:`testkey`,options:{json.dumps(options)}}})'
)

rowuses = [(1 << j) |                  # column ordinal
           (1 << (n + i-j + n-1)) |    # NW-SE ordinal
           (1 << (n + 2*n-1 + i+j))    # NE-SW ordinal
           for j in rangen]

rowuses = [((1 << j) # column ordinal
         )|
           (
               # comment
               (1 << (n + i-j + n-1))) |    # NW-SE ordinal
           (1 << (n + 2*n-1 + i+j))    # NE-SW ordinal
           for j in rangen]

skip_bytes = (
    header.timecnt * 5  # Transition times and types
    + header.typecnt * 6  # Local time type records
    + header.charcnt  # Time zone designations
    + header.leapcnt * 8  # Leap second records
    + header.isstdcnt  # Standard/wall indicators
    + header.isutcnt  # UT/local indicators
)


if (
    (1 + 2)  # test
    or (3 + 4)  # other
    or (4 + 5)  # more
):
    pass


if (
    (1 and 2)  # test
    + (3 and 4)  # other
    + (4 and 5)  # more
):
    pass


if (
    (1 + 2)  # test
    < (3 + 4)  # other
    > (4 + 5)  # more
):
    pass

z = (
                 a
                 +
                 # a: extracts this comment
                 (
                     # b: and this comment
                     (
                         # c: formats it as part of the expression
                         x and y
                     )
             )
)

z = (
    (

        (

            x and y
            # a: formats it as part of the expression

        )
        # b: extracts this comment

    )
    # c: and this comment
    + a
)

# Test for https://github.com/astral-sh/ruff/issues/7431
if True:
    if True:
        if True:
            if True:
                msg += " " + _(
                    "Since the role is not mentionable, it will be momentarily made mentionable "
                    "when announcing a streamalert. Please make sure I have the correct "
                    "permissions to manage this role, or else members of this role won't receive "
                    "a notification."
                )

# This f-string should be flattened
xxxxxxxxxxxxxxxx = f"aaaaaaaaaaaaaaaaaaaaa {
    expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (
    yyyyyyyyyyyyyy + zzzzzzzzzzz
)

# This is not a multiline f-string, but the expression is too long so it should be
# wrapped in parentheses.
f"hellooooooooooooooooooooooo \
        worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
aaaaaaaaaaa = f"hellooooooooooooooooooooooo \
        worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)

# This t-string should be flattened
xxxxxxxxxxxxxxxx = t"aaaaaaaaaaaaaaaaaaaaa {
    expression } bbbbbbbbbbbbbbbbbbbbbbbb" + (
    yyyyyyyyyyyyyy + zzzzzzzzzzz
)

# This is not a multiline t-string, but the expression is too long so it should be
# wrapped in parentheses.
t"hellooooooooooooooooooooooo \
        worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)
aaaaaaaaaaa = t"hellooooooooooooooooooooooo \
        worlddddddddddddddddddddddddddddddddd" + (aaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbb)