@@ -2843,9 +2843,7 @@ def _check_is_unused(
28432843 return
28442844
28452845 # Special case for exception variable
2846- if isinstance (stmt .parent , nodes .ExceptHandler ) and any (
2847- n .name == name for n in stmt .parent .nodes_of_class (nodes .Name )
2848- ):
2846+ if self ._is_exception_binding_used_in_handler (stmt , name ):
28492847 return
28502848
28512849 self .add_message (message_name , args = name , node = stmt )
@@ -2921,6 +2919,15 @@ def _check_unused_arguments(
29212919
29222920 self .add_message ("unused-argument" , args = name , node = stmt , confidence = confidence )
29232921
2922+ def _is_exception_binding_used_in_handler (
2923+ self , stmt : nodes .NodeNG , name : str
2924+ ) -> bool :
2925+ return (
2926+ isinstance (stmt .parent , nodes .ExceptHandler )
2927+ and stmt is stmt .parent .name
2928+ and any (n .name == name for n in stmt .parent .nodes_of_class (nodes .Name ))
2929+ )
2930+
29242931 def _check_late_binding_closure (self , node : nodes .Name ) -> None :
29252932 """Check whether node is a cell var that is assigned within a containing loop.
29262933
@@ -3252,6 +3259,8 @@ def _check_globals(self, not_consumed: Consumption) -> None:
32523259 for node in node_lst :
32533260 if in_type_checking_block (node ):
32543261 continue
3262+ if self ._is_exception_binding_used_in_handler (node , name ):
3263+ continue
32553264 self .add_message ("unused-variable" , args = (name ,), node = node )
32563265
32573266 # pylint: disable = too-many-branches
0 commit comments