@@ -1291,6 +1291,9 @@ def _break_around_binary_operators(tokens):
1291
1291
"""
1292
1292
line_break = False
1293
1293
unary_context = True
1294
+ param_name_context = False
1295
+ # only counted when inside param list, `-1` when not counting
1296
+ enclosure_count = - 1
1294
1297
# Previous non-newline token types and text
1295
1298
previous_token_type = None
1296
1299
previous_text = None
@@ -1301,11 +1304,30 @@ def _break_around_binary_operators(tokens):
1301
1304
line_break = True
1302
1305
else :
1303
1306
yield (token_type , text , previous_token_type , previous_text ,
1304
- line_break , unary_context , start )
1307
+ line_break , unary_context , param_name_context , start )
1305
1308
unary_context = text in '([{,;'
1306
1309
line_break = False
1307
1310
previous_token_type = token_type
1308
1311
previous_text = text
1312
+ if enclosure_count != - 1 :
1313
+ if token_type != tokenize .OP :
1314
+ pass
1315
+ elif text in '([{' :
1316
+ enclosure_count += 1
1317
+ elif text in ')]}' :
1318
+ enclosure_count -= 1
1319
+ if enclosure_count == 0 :
1320
+ # stop counting enclosures
1321
+ enclosure_count = - 1
1322
+ elif token_type == tokenize .NAME and text == 'def' :
1323
+ # start counting enclosures
1324
+ enclosure_count = 0
1325
+ # check for enclosure count and last token to make sure that
1326
+ # only the actual positional-only marker matches here:
1327
+ # def f(
1328
+ # x=4/2, y=(1, 4/2), /
1329
+ # ):
1330
+ param_name_context = text == ',' and enclosure_count == 1
1309
1331
1310
1332
1311
1333
@register_check
@@ -1330,9 +1352,10 @@ def break_before_binary_operator(logical_line, tokens):
1330
1352
"""
1331
1353
for context in _break_around_binary_operators (tokens ):
1332
1354
(token_type , text , previous_token_type , previous_text ,
1333
- line_break , unary_context , start ) = context
1355
+ line_break , unary_context , param_name_context , start ) = context
1334
1356
if (_is_binary_operator (token_type , text ) and line_break and
1335
1357
not unary_context and
1358
+ not param_name_context and
1336
1359
not _is_binary_operator (previous_token_type ,
1337
1360
previous_text )):
1338
1361
yield start , "W503 line break before binary operator"
@@ -1362,15 +1385,18 @@ def break_after_binary_operator(logical_line, tokens):
1362
1385
Okay: var = (1 +\n -1 +\n -2)
1363
1386
"""
1364
1387
prev_start = None
1388
+ prev_param_name_context = False
1365
1389
for context in _break_around_binary_operators (tokens ):
1366
1390
(token_type , text , previous_token_type , previous_text ,
1367
- line_break , unary_context , start ) = context
1391
+ line_break , unary_context , param_name_context , start ) = context
1368
1392
if (_is_binary_operator (previous_token_type , previous_text ) and
1369
1393
line_break and
1370
1394
not unary_context and
1395
+ not prev_param_name_context and
1371
1396
not _is_binary_operator (token_type , text )):
1372
1397
yield prev_start , "W504 line break after binary operator"
1373
1398
prev_start = start
1399
+ prev_param_name_context = param_name_context
1374
1400
1375
1401
1376
1402
@register_check
0 commit comments