From 05c42865940be9bafe2763f55b54bdb6ffb38268 Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Sun, 11 Sep 2022 03:26:59 +0200 Subject: [PATCH 1/3] Make a few test cases --- testsuite/python38.py | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/testsuite/python38.py b/testsuite/python38.py index 536448d8..ad09d69b 100644 --- a/testsuite/python38.py +++ b/testsuite/python38.py @@ -59,3 +59,49 @@ def f3( #: E741 if (l := 1): pass +#: Okay +def f( + x=4 / 2, y=(1, 4 / 2), / +): + ... +#: W504:4:14 +def f( + x=4 / 2, + y=( + 1, 4 / + 2 + ), + / +): + ... +#: W504:2:9 +def f( + x=4 / + 2, + y=(1, 4 / 2), + / +): + ... +#: Okay +def f( + x=4 / 2, y=(1, 4 / 2), / +): + ... +#: W503:5:9 +def f( + x=4 / 2, + y=( + 1, 4 + / 2 + ), + / +): + ... +#: W503:3:5 +def f( + x=4 + / 2, + y=(1, 4 / 2), + / +): + ... From b6af9c1674556eb29a68511de6f700089d4417bf Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Sun, 11 Sep 2022 03:52:45 +0200 Subject: [PATCH 2/3] Add some tests for keyword-only as well --- testsuite/python3.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/testsuite/python3.py b/testsuite/python3.py index 959956e5..6f87c479 100644 --- a/testsuite/python3.py +++ b/testsuite/python3.py @@ -46,3 +46,55 @@ def f( x: str = ... ): ... +#: E203 +def f( + x=4 / 2, y=(1, 4 / 2), * + , arg +): + ... +#: E203 W504:4:14 +def f( + x=4 * 2, + y=( + 1, 4 * + 2 + ), + * + , arg +): + ... +#: E203 W504:2:9 +def f( + x=4 * + 2, + y=(1, 4 * 2), + * + , arg +): + ... +#: E203 +def f( + x=4 * 2, y=(1, 4 * 2), * + , arg +): + ... +#: E203 W503:5:9 +def f( + x=4 * 2, + y=( + 1, 4 + * 2 + ), + * + , arg +): + ... +#: E203 W503:3:5 +def f( + x=4 + * 2, + y=(1, 4 * 2), + * + , arg +): + ... From f67b7ba895658911b0e157fb4d732698771c3986 Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Sun, 11 Sep 2022 03:44:37 +0200 Subject: [PATCH 3/3] Fix positional-only marker being reported as W504 --- pycodestyle.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index f5e05cfa..fe62f06c 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1291,6 +1291,9 @@ def _break_around_binary_operators(tokens): """ line_break = False unary_context = True + param_name_context = False + # only counted when inside param list, `-1` when not counting + enclosure_count = -1 # Previous non-newline token types and text previous_token_type = None previous_text = None @@ -1301,11 +1304,30 @@ def _break_around_binary_operators(tokens): line_break = True else: yield (token_type, text, previous_token_type, previous_text, - line_break, unary_context, start) + line_break, unary_context, param_name_context, start) unary_context = text in '([{,;' line_break = False previous_token_type = token_type previous_text = text + if enclosure_count != -1: + if token_type != tokenize.OP: + pass + elif text in '([{': + enclosure_count += 1 + elif text in ')]}': + enclosure_count -= 1 + if enclosure_count == 0: + # stop counting enclosures + enclosure_count = -1 + elif token_type == tokenize.NAME and text == 'def': + # start counting enclosures + enclosure_count = 0 + # check for enclosure count and last token to make sure that + # only the actual positional-only marker matches here: + # def f( + # x=4/2, y=(1, 4/2), / + # ): + param_name_context = text == ',' and enclosure_count == 1 @register_check @@ -1330,9 +1352,10 @@ def break_before_binary_operator(logical_line, tokens): """ for context in _break_around_binary_operators(tokens): (token_type, text, previous_token_type, previous_text, - line_break, unary_context, start) = context + line_break, unary_context, param_name_context, start) = context if (_is_binary_operator(token_type, text) and line_break and not unary_context and + not param_name_context and not _is_binary_operator(previous_token_type, previous_text)): yield start, "W503 line break before binary operator" @@ -1362,15 +1385,18 @@ def break_after_binary_operator(logical_line, tokens): Okay: var = (1 +\n -1 +\n -2) """ prev_start = None + prev_param_name_context = False for context in _break_around_binary_operators(tokens): (token_type, text, previous_token_type, previous_text, - line_break, unary_context, start) = context + line_break, unary_context, param_name_context, start) = context if (_is_binary_operator(previous_token_type, previous_text) and line_break and not unary_context and + not prev_param_name_context and not _is_binary_operator(token_type, text)): yield prev_start, "W504 line break after binary operator" prev_start = start + prev_param_name_context = param_name_context @register_check