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 d8ea869cb9..40c54d816e 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