Skip to content

Commit

Permalink
issue-1880: fixes that ConstantConditionViolation was not reported fo…
Browse files Browse the repository at this point in the history
…r a BoolOp (#1915)
  • Loading branch information
novikovfred authored Feb 26, 2021
1 parent 9fc7352 commit a605c95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Semantic versioning in our case means:
- Fixes `BitwiseAndBooleanMixupViolation` work with PEP 604 union types #1884
- Fixes `CognitiveModuleComplexityViolation` to not trigger
for a single-item modules
- Fixes that `ConstantConditionViolation` was not reported for a BoolOp


## 0.15.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def container():
'variable is False',
'[1, 2, 3].size > 3',
'variable is None',
'variable is int or not None',
'variable is int or not some()',
pytest.param(
'(unique := some()) is True',
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
Expand Down Expand Up @@ -107,6 +107,10 @@ def test_valid_conditional(
'{"set"}',
'("tuple",)',
'["list"]',
'variable or False',
'variable and False',
'variable or True',
'variable and True',
pytest.param(
'(unique := True)',
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
Expand Down
10 changes: 7 additions & 3 deletions wemake_python_styleguide/visitors/ast/compares.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,13 @@ def visit_comprehension(self, node: ast.comprehension) -> None:
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))
if isinstance(node, ast.BoolOp):
for condition in node.values:
self._check_constant_condition(condition)
else:
real_node = operators.unwrap_unary_node(get_assigned_expr(node))
if isinstance(real_node, self._forbidden_nodes):
self.add_violation(ConstantConditionViolation(node))

def _check_simplifiable_if(self, node: ast.If) -> None:
if not ifs.is_elif(node) and not ifs.root_if(node):
Expand Down

0 comments on commit a605c95

Please sign in to comment.