@@ -2843,9 +2843,7 @@ def _check_is_unused(
2843
2843
return
2844
2844
2845
2845
# 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 ):
2849
2847
return
2850
2848
2851
2849
self .add_message (message_name , args = name , node = stmt )
@@ -2921,6 +2919,15 @@ def _check_unused_arguments(
2921
2919
2922
2920
self .add_message ("unused-argument" , args = name , node = stmt , confidence = confidence )
2923
2921
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
+
2924
2931
def _check_late_binding_closure (self , node : nodes .Name ) -> None :
2925
2932
"""Check whether node is a cell var that is assigned within a containing loop.
2926
2933
@@ -3252,6 +3259,8 @@ def _check_globals(self, not_consumed: Consumption) -> None:
3252
3259
for node in node_lst :
3253
3260
if in_type_checking_block (node ):
3254
3261
continue
3262
+ if self ._is_exception_binding_used_in_handler (node , name ):
3263
+ continue
3255
3264
self .add_message ("unused-variable" , args = (name ,), node = node )
3256
3265
3257
3266
# pylint: disable = too-many-branches
0 commit comments