Repository: wemake-services/wemake-python-styleguide

Issue:
Ensure that `FalsyConstantCompareViolation` counts `NamedExpr`
Currently we don't handle `NamedExpr` in this violation.

One can write this code:

```python
if (x := []) == other:
    ...
```

And it won't be handled properly. Here's how direct values are compared:

```python
  1:4      WPS520 Found compare with falsy constant
  if [] == other:
     ^
```

File to fix: https://github.com/wemake-services/wemake-python-styleguide/blob/ea20c44bbbb8dcf70b3541b073cee25ae91a8354/wemake_python_styleguide/visitors/ast/compares.py#L151

We also need to test this violation. Make sure that you use `pytest.skipif(PY38, ...)` because this syntax is only valid starting from `python3.8+`

Related #1137


Discussion:
Let me fix this one
Sure! Thank you, @edytagarbarz! Make sure to read [our contributing guides](https://github.com/wemake-services/wemake-python-styleguide/blob/master/CONTRIBUTING.md)

I am here to help: ask anything you need.
@edytagarbarz friendly ping to make sure you don't have any problems with the task. But, if there's something I can help you with - feel free to ask! 👍 
By the way, this commit does pretty much the same thing as you need: https://github.com/wemake-services/wemake-python-styleguide/commit/4fa9f411fbe5c24477f4b7d975ed84845b405e64 Check it out!

Resolving patch:
diff --git a/wemake_python_styleguide/visitors/ast/compares.py b/wemake_python_styleguide/visitors/ast/compares.py
--- a/wemake_python_styleguide/visitors/ast/compares.py
+++ b/wemake_python_styleguide/visitors/ast/compares.py
@@ -169,13 +169,13 @@ def visit_Compare(self, node: ast.Compare) -> None:
     def _check_constant(self, op: ast.cmpop, comparator: ast.expr) -> None:
         if not isinstance(op, (ast.Eq, ast.NotEq, ast.Is, ast.IsNot)):
             return
-
-        if not isinstance(comparator, (ast.List, ast.Dict, ast.Tuple)):
+        real = get_assigned_expr(comparator)
+        if not isinstance(real, (ast.List, ast.Dict, ast.Tuple)):
             return
 
-        length = len(comparator.keys) if isinstance(
-            comparator, ast.Dict,
-        ) else len(comparator.elts)
+        length = len(real.keys) if isinstance(
+            real, ast.Dict,
+        ) else len(real.elts)
 
         if not length:
             self.add_violation(FalsyConstantCompareViolation(comparator))


Test patch:
diff --git a/tests/test_visitors/test_ast/test_compares/test_constant_compares/test_falsy_constant.py b/tests/test_visitors/test_ast/test_compares/test_constant_compares/test_falsy_constant.py
--- a/tests/test_visitors/test_ast/test_compares/test_constant_compares/test_falsy_constant.py
+++ b/tests/test_visitors/test_ast/test_compares/test_constant_compares/test_falsy_constant.py
@@ -1,5 +1,6 @@
 import pytest
 
+from wemake_python_styleguide.compat.constants import PY38
 from wemake_python_styleguide.violations.refactoring import (
     FalsyConstantCompareViolation,
     WrongIsCompareViolation,
@@ -8,13 +9,26 @@
     WrongConstantCompareVisitor,
 )
 
-wrong_comparators = (
+wrong_comparators = [
     ('some', '[]'),
     ('some', '{}'),  # noqa: P103
     ('some', '()'),
     ('[]', 'some'),
     ('{}', 'some'),  # noqa: P103
     ('()', 'some'),
+]
+
+if PY38:
+    wrong_comparators.extend([
+        ('some', '(x := [])'),
+        ('(x := [])', 'some'),
+    ])
+
+correct_walrus = pytest.param(
+    ['(x := [1, 2])', 'some'],
+    marks=pytest.mark.skipif(
+        not PY38, reason='walrus appeared in 3.8',
+    ),
 )
 
 
@@ -92,8 +106,8 @@ def test_falsy_constant_not_eq(
     ('some', 'other.attr'),
     ('some', 'other.method()'),
     ('some', 'other[0]'),
-
     ('None', 'some'),
+    correct_walrus,
 ])
 def test_correct_constant_compare(
     assert_errors,