@@ -4225,6 +4225,7 @@ def analyze_lvalue(
42254225 is_final : bool = False ,
42264226 escape_comprehensions : bool = False ,
42274227 has_explicit_value : bool = False ,
4228+ is_index_var : bool = False ,
42284229 ) -> None :
42294230 """Analyze an lvalue or assignment target.
42304231
@@ -4235,6 +4236,7 @@ def analyze_lvalue(
42354236 escape_comprehensions: If we are inside a comprehension, set the variable
42364237 in the enclosing scope instead. This implements
42374238 https://www.python.org/dev/peps/pep-0572/#scope-of-the-target
4239+ is_index_var: If lval is the index variable in a for loop
42384240 """
42394241 if escape_comprehensions :
42404242 assert isinstance (lval , NameExpr ), "assignment expression target must be NameExpr"
@@ -4245,6 +4247,7 @@ def analyze_lvalue(
42454247 is_final ,
42464248 escape_comprehensions ,
42474249 has_explicit_value = has_explicit_value ,
4250+ is_index_var = is_index_var ,
42484251 )
42494252 elif isinstance (lval , MemberExpr ):
42504253 self .analyze_member_lvalue (lval , explicit_type , is_final , has_explicit_value )
@@ -4271,6 +4274,7 @@ def analyze_name_lvalue(
42714274 is_final : bool ,
42724275 escape_comprehensions : bool ,
42734276 has_explicit_value : bool ,
4277+ is_index_var : bool ,
42744278 ) -> None :
42754279 """Analyze an lvalue that targets a name expression.
42764280
@@ -4309,7 +4313,9 @@ def analyze_name_lvalue(
43094313
43104314 if (not existing or isinstance (existing .node , PlaceholderNode )) and not outer :
43114315 # Define new variable.
4312- var = self .make_name_lvalue_var (lvalue , kind , not explicit_type , has_explicit_value )
4316+ var = self .make_name_lvalue_var (
4317+ lvalue , kind , not explicit_type , has_explicit_value , is_index_var
4318+ )
43134319 added = self .add_symbol (name , var , lvalue , escape_comprehensions = escape_comprehensions )
43144320 # Only bind expression if we successfully added name to symbol table.
43154321 if added :
@@ -4361,7 +4367,12 @@ def is_alias_for_final_name(self, name: str) -> bool:
43614367 return existing is not None and is_final_node (existing .node )
43624368
43634369 def make_name_lvalue_var (
4364- self , lvalue : NameExpr , kind : int , inferred : bool , has_explicit_value : bool
4370+ self ,
4371+ lvalue : NameExpr ,
4372+ kind : int ,
4373+ inferred : bool ,
4374+ has_explicit_value : bool ,
4375+ is_index_var : bool ,
43654376 ) -> Var :
43664377 """Return a Var node for an lvalue that is a name expression."""
43674378 name = lvalue .name
@@ -4380,6 +4391,7 @@ def make_name_lvalue_var(
43804391 v ._fullname = name
43814392 v .is_ready = False # Type not inferred yet
43824393 v .has_explicit_value = has_explicit_value
4394+ v .is_index_var = is_index_var
43834395 return v
43844396
43854397 def make_name_lvalue_point_to_existing_def (
@@ -5290,7 +5302,7 @@ def visit_for_stmt(self, s: ForStmt) -> None:
52905302 s .expr .accept (self )
52915303
52925304 # Bind index variables and check if they define new names.
5293- self .analyze_lvalue (s .index , explicit_type = s .index_type is not None )
5305+ self .analyze_lvalue (s .index , explicit_type = s .index_type is not None , is_index_var = True )
52945306 if s .index_type :
52955307 if self .is_classvar (s .index_type ):
52965308 self .fail_invalid_classvar (s .index )
0 commit comments