Skip to content

Commit

Permalink
Closes #1864 (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Feb 11, 2021
1 parent 56132c8 commit 5671e97
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Semantic versioning in our case means:
- Fixes `InconsistentComprehensionViolation` work with `async` comprehensions
- Fixes nested compehensions support for `InconsistentComprehensionViolation`
- Fixes multiple `if` support for `InconsistentComprehensionViolation`
- Fixes that `NestedTernaryViolation` was not reported for a comprehension
- Fixes that `ConstantConditionViolation` was not reported for a comprehension

### Misc

Expand Down
45 changes: 41 additions & 4 deletions tests/test_visitors/test_ast/test_compares/test_conditionals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,42 @@
if_statement = 'if {0}: ...'
ternary = 'ternary = 0 if {0} else 1'

if_statement_in_comprehension = """
list_comprehension = """
def container():
[x for x in [1, 2, 3] if {0}]
"""

set_comprehension = """
def container():
{{
x
for x in [1, 2, 3]
if {0}
}}
"""

dict_comprehension = """
def container():
{{
x: '1'
for x in [1, 2, 3]
if {0}
}}
"""

gen_comprehension = """
def container():
(x for x in [1, 2, 3] if {0})
"""


@pytest.mark.parametrize('code', [
if_statement,
ternary,
if_statement_in_comprehension,
list_comprehension,
set_comprehension,
dict_comprehension,
gen_comprehension,
])
@pytest.mark.parametrize('comparators', [
'variable < 3',
Expand Down Expand Up @@ -62,6 +88,10 @@ def test_valid_conditional(
@pytest.mark.parametrize('code', [
if_statement,
ternary,
# list_comprehension,
# set_comprehension,
# dict_comprehension,
# gen_comprehension,
])
@pytest.mark.parametrize('comparators', [
'True',
Expand All @@ -81,16 +111,23 @@ def test_valid_conditional(
'(unique := True)',
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
),
pytest.param(
'(unique := -1)',
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
),
])
def test_useless(
def test_constant_condition(
assert_errors,
parse_ast_tree,
code,
comparators,
default_options,
mode,
):
"""Testing that violations are when using invalid conditional."""
tree = parse_ast_tree(create_variable.format(code.format(comparators)))
tree = parse_ast_tree(
mode(create_variable.format(code.format(comparators))),
)

visitor = WrongConditionalVisitor(default_options, tree=tree)
visitor.run()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_visitors/test_ast/test_compares/test_nested_ternary.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
wrong_if3 = 'if attr.call(a if b else c): ...'
wrong_if4 = 'if x := 1 if True else 2: ...'

wrong_comprehension1 = '[x for x in number if (some if x else other)]'
wrong_comprehension2 = '(x for x in number if (some if x else other))'
wrong_comprehension3 = '{{x for x in number if (some if x else other)}}'
wrong_comprehension4 = '{{x: 1 for x in number if (some if x else other)}}'

# Correct:

correct_if1 = """
Expand Down Expand Up @@ -157,6 +162,11 @@ def test_non_nested_ternary(
wrong_if3,
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
),
wrong_comprehension1,
wrong_comprehension2,
wrong_comprehension3,
wrong_comprehension4,
])
def test_nested_ternary(
assert_errors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ def wrapper():
]
"""

wrong_list_split_multiple_ifs3 = """
def wrapper():
[
some(number)
for number in matrix
if some
for other in iterable
if number > 0
]
"""

wrong_list_two_compehensions1 = """
def wrapper():
comp = [
Expand Down Expand Up @@ -506,6 +517,7 @@ def wrapper():
wrong_list_split_for_in,
wrong_list_split_multiple_ifs1,
wrong_list_split_multiple_ifs2,
wrong_list_split_multiple_ifs3,
wrong_list_two_compehensions1,
wrong_list_two_compehensions2,
wrong_dict_almost_one_line,
Expand Down
14 changes: 9 additions & 5 deletions wemake_python_styleguide/visitors/ast/compares.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class WrongConditionalVisitor(BaseNodeVisitor):
ast.BinOp,
ast.UnaryOp,
ast.Compare,
ast.comprehension,
)

def visit_any_if(self, node: AnyIf) -> None:
Expand All @@ -322,14 +323,17 @@ def visit_any_if(self, node: AnyIf) -> None:
self._check_simplifiable_ifexpr(node)

self._check_nested_ifexpr(node)
self._check_constant_condition(node)
self._check_constant_condition(node.test)
self.generic_visit(node)

def _check_constant_condition(self, node: AnyIf) -> None:
real_node = operators.unwrap_unary_node(
get_assigned_expr(node.test),
)
def visit_comprehension(self, node: ast.comprehension) -> None:
"""Checks all possible comprehensions."""
for expr in node.ifs:
self._check_constant_condition(expr)
self.generic_visit(node)

def _check_constant_condition(self, node: ast.AST) -> None:
real_node = operators.unwrap_unary_node(get_assigned_expr(node))
if isinstance(real_node, self._forbidden_nodes):
self.add_violation(ConstantConditionViolation(node))

Expand Down

0 comments on commit 5671e97

Please sign in to comment.