Skip to content

Commit f67b7ba

Browse files
committed
Fix positional-only marker being reported as W504
1 parent b6af9c1 commit f67b7ba

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

pycodestyle.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,9 @@ def _break_around_binary_operators(tokens):
12911291
"""
12921292
line_break = False
12931293
unary_context = True
1294+
param_name_context = False
1295+
# only counted when inside param list, `-1` when not counting
1296+
enclosure_count = -1
12941297
# Previous non-newline token types and text
12951298
previous_token_type = None
12961299
previous_text = None
@@ -1301,11 +1304,30 @@ def _break_around_binary_operators(tokens):
13011304
line_break = True
13021305
else:
13031306
yield (token_type, text, previous_token_type, previous_text,
1304-
line_break, unary_context, start)
1307+
line_break, unary_context, param_name_context, start)
13051308
unary_context = text in '([{,;'
13061309
line_break = False
13071310
previous_token_type = token_type
13081311
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
13091331

13101332

13111333
@register_check
@@ -1330,9 +1352,10 @@ def break_before_binary_operator(logical_line, tokens):
13301352
"""
13311353
for context in _break_around_binary_operators(tokens):
13321354
(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
13341356
if (_is_binary_operator(token_type, text) and line_break and
13351357
not unary_context and
1358+
not param_name_context and
13361359
not _is_binary_operator(previous_token_type,
13371360
previous_text)):
13381361
yield start, "W503 line break before binary operator"
@@ -1362,15 +1385,18 @@ def break_after_binary_operator(logical_line, tokens):
13621385
Okay: var = (1 +\n -1 +\n -2)
13631386
"""
13641387
prev_start = None
1388+
prev_param_name_context = False
13651389
for context in _break_around_binary_operators(tokens):
13661390
(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
13681392
if (_is_binary_operator(previous_token_type, previous_text) and
13691393
line_break and
13701394
not unary_context and
1395+
not prev_param_name_context and
13711396
not _is_binary_operator(token_type, text)):
13721397
yield prev_start, "W504 line break after binary operator"
13731398
prev_start = start
1399+
prev_param_name_context = param_name_context
13741400

13751401

13761402
@register_check

0 commit comments

Comments
 (0)