@@ -132,13 +132,6 @@ def _is_from_future_import(stmt, name):
132
132
return None
133
133
134
134
135
- def in_for_else_branch (parent , stmt ):
136
- """Returns True if stmt in inside the else branch for a parent For stmt."""
137
- return isinstance (parent , nodes .For ) and any (
138
- else_stmt .parent_of (stmt ) or else_stmt == stmt for else_stmt in parent .orelse
139
- )
140
-
141
-
142
135
@lru_cache (maxsize = 1000 )
143
136
def overridden_method (klass , name ):
144
137
"""Get overridden method if any."""
@@ -448,7 +441,7 @@ def _has_locals_call_after_node(stmt, scope):
448
441
"W0621" : (
449
442
"Redefining name %r from outer scope (line %s)" ,
450
443
"redefined-outer-name" ,
451
- "Used when a variable's name hides a name defined in the outer scope." ,
444
+ "Used when a variable's name hides a name defined in an outer scope or except handler ." ,
452
445
),
453
446
"W0622" : (
454
447
"Redefining built-in %r" ,
@@ -961,7 +954,7 @@ class VariablesChecker(BaseChecker):
961
954
Checks for
962
955
* unused variables / imports
963
956
* undefined variables
964
- * redefinition of variable from builtins or from an outer scope
957
+ * redefinition of variable from builtins or from an outer scope or except handler
965
958
* use of variable before assignment
966
959
* __all__ consistency
967
960
* self/cls assignment
@@ -1062,7 +1055,6 @@ def __init__(self, linter=None):
1062
1055
super ().__init__ (linter )
1063
1056
self ._to_consume : list [NamesConsumer ] = []
1064
1057
self ._checking_mod_attr = None
1065
- self ._loop_variables = []
1066
1058
self ._type_annotation_names = []
1067
1059
self ._except_handler_names_queue : list [
1068
1060
tuple [nodes .ExceptHandler , nodes .AssignName ]
@@ -1079,31 +1071,7 @@ def open(self) -> None:
1079
1071
"undefined-loop-variable"
1080
1072
)
1081
1073
1082
- @utils .only_required_for_messages ("redefined-outer-name" )
1083
- def visit_for (self , node : nodes .For ) -> None :
1084
- assigned_to = [a .name for a in node .target .nodes_of_class (nodes .AssignName )]
1085
-
1086
- # Only check variables that are used
1087
- dummy_rgx = self .linter .config .dummy_variables_rgx
1088
- assigned_to = [var for var in assigned_to if not dummy_rgx .match (var )]
1089
-
1090
- for variable in assigned_to :
1091
- for outer_for , outer_variables in self ._loop_variables :
1092
- if variable in outer_variables and not in_for_else_branch (
1093
- outer_for , node
1094
- ):
1095
- self .add_message (
1096
- "redefined-outer-name" ,
1097
- args = (variable , outer_for .fromlineno ),
1098
- node = node ,
1099
- )
1100
- break
1101
-
1102
- self ._loop_variables .append ((node , assigned_to ))
1103
-
1104
- @utils .only_required_for_messages ("redefined-outer-name" )
1105
1074
def leave_for (self , node : nodes .For ) -> None :
1106
- self ._loop_variables .pop ()
1107
1075
self ._store_type_annotation_names (node )
1108
1076
1109
1077
def visit_module (self , node : nodes .Module ) -> None :
@@ -2231,7 +2199,7 @@ def _loopvar_name(self, node: astroid.Name) -> None:
2231
2199
for i , stmt in enumerate (astmts [1 :]):
2232
2200
if astmts [i ].statement (future = True ).parent_of (
2233
2201
stmt
2234
- ) and not in_for_else_branch (astmts [i ].statement (future = True ), stmt ):
2202
+ ) and not utils . in_for_else_branch (astmts [i ].statement (future = True ), stmt ):
2235
2203
continue
2236
2204
_astmts .append (stmt )
2237
2205
astmts = _astmts
0 commit comments