Skip to content

Commit

Permalink
Closes #1200, closes #1202
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Mar 12, 2020
1 parent 4fa9f41 commit 7d98e40
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
11 changes: 10 additions & 1 deletion tests/test_visitors/test_ast/test_compares/test_conditionals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from wemake_python_styleguide.compat.constants import PY38
from wemake_python_styleguide.violations.consistency import (
ConstantConditionViolation,
)
Expand Down Expand Up @@ -31,9 +32,13 @@ def container():
'variable',
'variable is True',
'variable is False',
'[1,2,3].size > 3',
'[1, 2, 3].size > 3',
'variable is None',
'variable is int or not None',
pytest.param(
'(unique := some()) is True',
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
),
])
def test_valid_conditional(
assert_errors,
Expand Down Expand Up @@ -72,6 +77,10 @@ def test_valid_conditional(
'{"set"}',
'("tuple",)',
'["list"]',
pytest.param(
'(unique := True)',
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
),
])
def test_useless(
assert_errors,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from wemake_python_styleguide.compat.constants import PY38
from wemake_python_styleguide.violations.refactoring import (
FalsyConstantCompareViolation,
WrongIsCompareViolation,
Expand All @@ -8,7 +9,7 @@
WrongConstantCompareVisitor,
)

wrong_comparators = (
wrong_comparators = [
('some', '[1, 2]'),
('some', '{}'), # noqa: P103
('some', '()'),
Expand All @@ -32,7 +33,13 @@
('{1, 2}', 'some'),
('()', 'some'),
('"test"', 'some'),
)
]

if PY38:
wrong_comparators.extend([
('(x := some())', '"abc"'),
('(x := "abc")', 'some()'),
])


@pytest.mark.filterwarnings('ignore::SyntaxWarning')
Expand Down
6 changes: 4 additions & 2 deletions tests/test_visitors/test_ast/test_compares/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
])

correct_walrus = pytest.param(
('(x := first(1, 2))', '"str"'),
marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
['(x := first(1, 2))', '"str"'],
marks=pytest.mark.skipif(
not PY38, reason='walrus appeared in 3.8',
),
)


Expand Down
9 changes: 7 additions & 2 deletions wemake_python_styleguide/visitors/ast/compares.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ def _check_is_constant_comprare(
if not isinstance(op, (ast.Is, ast.IsNot)):
return

unwrapped = operators.unwrap_unary_node(comparator)
unwrapped = operators.unwrap_unary_node(
get_assigned_expr(comparator),
)
if isinstance(unwrapped, self._forbidden_for_is):
self.add_violation(WrongIsCompareViolation(comparator))

Expand Down Expand Up @@ -324,7 +326,10 @@ def visit_any_if(self, node: AnyIf) -> None:
self.generic_visit(node)

def _check_constant_condition(self, node: AnyIf) -> None:
real_node = operators.unwrap_unary_node(node.test)
real_node = operators.unwrap_unary_node(
get_assigned_expr(node.test),
)

if isinstance(real_node, self._forbidden_nodes):
self.add_violation(ConstantConditionViolation(node))

Expand Down

0 comments on commit 7d98e40

Please sign in to comment.