From abc2271406b1e8e2190fc7cb0545673a7e461dc1 Mon Sep 17 00:00:00 2001 From: Zen Lee <53538590+zenlyj@users.noreply.github.com> Date: Mon, 10 Nov 2025 04:18:15 +0800 Subject: [PATCH] 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) --- doc/whatsnew/fragments/10707.bugfix | 4 ++++ pylint/extensions/code_style.py | 2 +- .../ext/code_style/cs_consider_using_assignment_expr.py | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/10707.bugfix diff --git a/doc/whatsnew/fragments/10707.bugfix b/doc/whatsnew/fragments/10707.bugfix new file mode 100644 index 0000000000..cd82216b74 --- /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 0eff5f4786..8d045a40ca 100644 --- a/pylint/extensions/code_style.py +++ b/pylint/extensions/code_style.py @@ -314,7 +314,7 @@ def _check_prev_sibling_to_if_stmt( 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 62f5ba7a9b..aba15bd2e4 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 @@ class A: A.var = 2 if A.var: ... + + +i: int +if i: # pylint: disable=used-before-assignment + pass