[Backport maintenance/4.0.x] Fix crash when a variable annotation is used as `if` test expression (#10729)
Fix crash when a variable annotation is used as `if` test expression (#10714)
Fix crash for ``consider-using-assignment-expr``.
(cherry picked from commit 926529bb3288402c7ccc96a9f5bd7ca85cf38390)
Co-authored-by: Zen Lee <53538590+zenlyj@users.noreply.github.com>
diff --git a/doc/whatsnew/fragments/10707.bugfix b/doc/whatsnew/fragments/10707.bugfix
new file mode 100644
index 0000000..cd82216
--- /dev/null
+++ b/doc/whatsnew/fragments/10707.bugfix
@@ -0,0 +1,4 @@
+Fix crash for ``consider-using-assignment-expr`` when a variable annotation without assignment
+is used as the ``if`` test expression.
+
+Closes #10707
diff --git a/pylint/extensions/code_style.py b/pylint/extensions/code_style.py
index 0eff5f4..8d045a4 100644
--- a/pylint/extensions/code_style.py
+++ b/pylint/extensions/code_style.py
@@ -314,7 +314,7 @@
case nodes.Assign(
targets=[nodes.AssignName(name=target_name)]
) | nodes.AnnAssign(target=nodes.AssignName(name=target_name)):
- return target_name == name # type: ignore[no-any-return]
+ return target_name == name and prev_sibling.value is not None
return False
@staticmethod
diff --git a/tests/functional/ext/code_style/cs_consider_using_assignment_expr.py b/tests/functional/ext/code_style/cs_consider_using_assignment_expr.py
index 62f5ba7..aba15bd 100644
--- a/tests/functional/ext/code_style/cs_consider_using_assignment_expr.py
+++ b/tests/functional/ext/code_style/cs_consider_using_assignment_expr.py
@@ -157,3 +157,8 @@
A.var = 2
if A.var:
...
+
+
+i: int
+if i: # pylint: disable=used-before-assignment
+ pass