From adeeb447f62dbb1606713bef94606d793819052a Mon Sep 17 00:00:00 2001 From: jsfehler Date: Sat, 16 Apr 2022 15:42:13 -0400 Subject: [PATCH] Fix: False positive on multiline conditional expressions (#122) Catch when: > 1 hanging opening character Instead of catch when: opening character > 1 --- flake8_multiline_containers.py | 5 ++-- tests/dummy/conditional_expression.py | 18 ++++++++++++++ tests/test_conditional_expression.py | 35 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/dummy/conditional_expression.py create mode 100644 tests/test_conditional_expression.py diff --git a/flake8_multiline_containers.py b/flake8_multiline_containers.py index 885860e..908ee7e 100644 --- a/flake8_multiline_containers.py +++ b/flake8_multiline_containers.py @@ -165,8 +165,9 @@ def _check_opening( [get_left_pad(line)] * open_times, ) - # Multiple opening characters - if open_times > 1: + # Multiple opening characters with no closing + # There can only be one hanging opening + if (open_times - close_times) > 1: e = _error(line_number + 1, 0, error_code) self.errors.append(e) diff --git a/tests/dummy/conditional_expression.py b/tests/dummy/conditional_expression.py new file mode 100644 index 0000000..e4729b8 --- /dev/null +++ b/tests/dummy/conditional_expression.py @@ -0,0 +1,18 @@ +# Conditional expression where the third part of the expression is a +# multiline container +bar = 0 + + +foo = {} if bar == 1 else { + "foo": 42, +} + + +foo = [] if bar == 1 else [ + 42, +} + + +foo = () if bar == 1 else ( + 42, 42, +} diff --git a/tests/test_conditional_expression.py b/tests/test_conditional_expression.py new file mode 100644 index 0000000..3e562cf --- /dev/null +++ b/tests/test_conditional_expression.py @@ -0,0 +1,35 @@ +import os + +from flake8.api import legacy as flake8 + +import pytest + + +@pytest.fixture +def conditional_expression_file_path(dummy_file_path): + return f'{dummy_file_path}/conditional_expression.py' + + +@pytest.mark.doit1 +def test_js101_conditional_block(conditional_expression_file_path): + """Conditional blocks should not trigger JS101.""" + style_guide = flake8.get_style_guide( + select=['JS101'], + ) + + p = os.path.abspath(conditional_expression_file_path) + r = style_guide.check_files([p]) + + assert 0 == r.total_errors + + +def test_js102_conditional_block(conditional_expression_file_path): + """Conditional blocks should not trigger JS102.""" + style_guide = flake8.get_style_guide( + select=['JS102'], + ) + + p = os.path.abspath(conditional_expression_file_path) + r = style_guide.check_files([p]) + + assert 0 == r.total_errors