@@ -324,6 +324,7 @@ class _ReflectiveScope:
324324 global_names : set [str ] = field (default_factory = set )
325325 nonlocal_names : set [str ] = field (default_factory = set )
326326 is_class : bool = False
327+ is_comprehension : bool = False
327328
328329 def clone (self ) -> _ReflectiveScope :
329330 return _ReflectiveScope (
@@ -334,6 +335,7 @@ def clone(self) -> _ReflectiveScope:
334335 global_names = set (self .global_names ),
335336 nonlocal_names = set (self .nonlocal_names ),
336337 is_class = self .is_class ,
338+ is_comprehension = self .is_comprehension ,
337339 )
338340
339341 def restore (self , snapshot : _ReflectiveScope ) -> None :
@@ -345,6 +347,7 @@ def restore(self, snapshot: _ReflectiveScope) -> None:
345347 self .global_names = set (snapshot .global_names )
346348 self .nonlocal_names = set (snapshot .nonlocal_names )
347349 self .is_class = snapshot .is_class
350+ self .is_comprehension = snapshot .is_comprehension
348351
349352
350353class _LocalBindingCollector (ast .NodeVisitor ):
@@ -397,16 +400,37 @@ def visit_Lambda(self, node: ast.Lambda) -> None:
397400 return
398401
399402 def visit_ListComp (self , node : ast .ListComp ) -> None :
400- return
403+ self . _collect_comprehension_bindings ( node )
401404
402405 def visit_SetComp (self , node : ast .SetComp ) -> None :
403- return
406+ self . _collect_comprehension_bindings ( node )
404407
405408 def visit_DictComp (self , node : ast .DictComp ) -> None :
406- return
409+ self . _collect_comprehension_bindings ( node )
407410
408411 def visit_GeneratorExp (self , node : ast .GeneratorExp ) -> None :
409- return
412+ self ._collect_comprehension_bindings (node )
413+
414+ def _collect_comprehension_bindings (
415+ self , node : ast .ListComp | ast .SetComp | ast .DictComp | ast .GeneratorExp
416+ ) -> None :
417+ # Iteration targets are comprehension-local; walrus targets belong to
418+ # the containing function, including when the comprehension is nested.
419+ pending : list [ast .AST ] = [node ]
420+ while pending :
421+ if self .check_runtime is not None :
422+ self .check_runtime ()
423+ item = pending .pop ()
424+ if isinstance (item , ast .Lambda ):
425+ pending .extend (
426+ default
427+ for default in [* item .args .defaults , * item .args .kw_defaults ]
428+ if default is not None
429+ )
430+ continue
431+ if isinstance (item , ast .NamedExpr ):
432+ self .names .add (item .target .id )
433+ pending .extend (ast .iter_child_nodes (item ))
410434
411435 def visit_Global (self , node : ast .Global ) -> None :
412436 self .global_names .update (node .names )
@@ -419,10 +443,12 @@ def generic_visit(self, node: ast.AST) -> None:
419443 pending : list [ast .expr ] = [node ]
420444 while pending :
421445 expression = pending .pop ()
446+ if isinstance (expression , ast .Lambda ):
447+ continue
422448 if isinstance (
423- expression ,
424- (ast .Lambda , ast .ListComp , ast .SetComp , ast .DictComp , ast .GeneratorExp ),
449+ expression , (ast .ListComp , ast .SetComp , ast .DictComp , ast .GeneratorExp )
425450 ):
451+ self ._collect_comprehension_bindings (expression )
426452 continue
427453 if isinstance (expression , ast .Name ):
428454 self .visit_Name (expression )
@@ -467,10 +493,15 @@ def scope(self) -> _ReflectiveScope:
467493 return self .scopes [- 1 ]
468494
469495 def _lookup_scopes (self , name : str ) -> list [_ReflectiveScope ]:
470- if name in self .scope .global_names :
471- return self .scopes [:1 ]
472- if name in self .scope .nonlocal_names :
473- return [self ._binding_scope (name )]
496+ index = len (self .scopes ) - 1
497+ while self .scopes [index ].is_comprehension :
498+ index -= 1
499+ containing = self .scopes [index ]
500+ inner = self .scopes [index + 1 :]
501+ if name in containing .global_names :
502+ return [self .scopes [0 ], * inner ]
503+ if name in containing .nonlocal_names :
504+ return [self ._binding_scope (name , scope_index = index ), * inner ]
474505 return self .scopes
475506
476507 def _lookup (self , kind : str , name : str ) -> str | None :
@@ -490,18 +521,29 @@ def _lookup_function(self, name: str) -> ast.FunctionDef | ast.AsyncFunctionDef
490521 return None
491522 return None
492523
493- def _binding_scope (self , name : str ) -> _ReflectiveScope :
494- current = self .scope
524+ def _binding_scope (self , name : str , * , scope_index : int | None = None ) -> _ReflectiveScope :
525+ index = len (self .scopes ) - 1 if scope_index is None else scope_index
526+ current = self .scopes [index ]
495527 if name in current .global_names :
496528 return self .scopes [0 ]
497529 if name in current .nonlocal_names :
498- for scope in reversed (self .scopes [:- 1 ]):
530+ for scope in reversed (self .scopes [:index ]):
499531 if name in scope .modules or name in scope .callables or name in scope .shadowed :
500532 return scope
501- if len ( self . scopes ) > 1 :
502- return self .scopes [- 2 ]
533+ if index > 0 :
534+ return self .scopes [index - 1 ]
503535 return current
504536
537+ def _module_name (self , node : ast .expr ) -> str | None :
538+ """Resolve static dotted bases through live bindings, never by spelling alone."""
539+ dotted = resolve_dotted_name (node )
540+ if dotted is not None :
541+ root , separator , rest = dotted .partition ("." )
542+ module = self ._lookup ("module" , root )
543+ if module is not None :
544+ return f"{ module } .{ rest } " if separator else module
545+ return self ._dynamic_module_name (node )
546+
505547 def _is_unshadowed_builtin (self , name : str ) -> bool :
506548 for scope in reversed (self ._lookup_scopes (name )):
507549 if name in scope .modules or name in scope .callables or name in scope .shadowed :
@@ -551,24 +593,36 @@ def _set_binding(self, kind: str, name: str, value: str) -> None:
551593 values [name ] = value
552594 scope .shadowed .add (name )
553595
554- def _bind (self , targets : list [ast .expr ], value : ast .expr ) -> None :
596+ def _bind (
597+ self , targets : list [ast .expr ], value : ast .expr , * , named_expression : bool = False
598+ ) -> None :
555599 names = self ._target_names (targets )
556- module = self ._dynamic_module_name (value )
557- canonical = self ._reflective_callable (value )
600+ module = self ._module_name (value )
601+ canonical = (
602+ self ._lookup ("callable" , value .id )
603+ if isinstance (value , ast .Name )
604+ else self ._reflective_callable (value )
605+ )
558606 function = self ._lookup_function (value .id ) if isinstance (value , ast .Name ) else None
559-
560- self ._shadow_names (names )
561- if module is not None :
562- for name in names :
563- self ._set_binding ("module" , name , module )
564- return
565- if canonical is not None :
566- for name in names :
567- self ._set_binding ("callable" , name , canonical )
568- return
569- if function is not None :
607+ # Resolve the RHS in the comprehension first, then write through to the
608+ # containing scope (and honor its global/nonlocal declarations).
609+ caller_scopes = self .scopes
610+ if named_expression :
611+ index = len (self .scopes ) - 1
612+ while self .scopes [index ].is_comprehension :
613+ index -= 1
614+ self .scopes = self .scopes [: index + 1 ]
615+ try :
616+ self ._shadow_names (names )
570617 for name in names :
571- self ._binding_scope (name ).functions [name ] = function
618+ if module is not None :
619+ self ._set_binding ("module" , name , module )
620+ elif canonical is not None :
621+ self ._set_binding ("callable" , name , canonical )
622+ elif function is not None :
623+ self ._binding_scope (name ).functions [name ] = function
624+ finally :
625+ self .scopes = caller_scopes
572626
573627 def _reflective_callable (self , value : ast .expr ) -> str | None :
574628 if (
@@ -579,11 +633,7 @@ def _reflective_callable(self, value: ast.expr) -> str | None:
579633 and len (value .args ) >= 2
580634 ):
581635 base = value .args [0 ]
582- reflected_module = (
583- self ._lookup ("module" , base .id ) if isinstance (base , ast .Name ) else None
584- )
585- if reflected_module is None :
586- reflected_module = self ._dynamic_module_name (base )
636+ reflected_module = self ._module_name (base )
587637 attribute = _constant_string (value .args [1 ])
588638 candidate = (
589639 f"{ reflected_module } .{ attribute } "
@@ -712,21 +762,63 @@ def _visit_comprehension(
712762 if not node .generators :
713763 return
714764 self .visit (node .generators [0 ].iter )
765+
766+ if isinstance (node , ast .GeneratorExp ):
767+ # Creating a generator evaluates only its outer iterator. Scan the
768+ # deferred body for sinks without applying effects to live frames.
769+ self ._run_isolated (lambda : self ._analyze_comprehension_body (node ))
770+ else :
771+ self ._analyze_comprehension_body (node )
772+
773+ @staticmethod
774+ def _literal_iteration_state (node : ast .expr ) -> bool | None :
775+ if isinstance (node , (ast .List , ast .Tuple , ast .Set )) and not any (
776+ isinstance (item , ast .Starred ) for item in node .elts
777+ ):
778+ return bool (node .elts )
779+ if isinstance (node , ast .Dict ) and all (key is not None for key in node .keys ):
780+ return bool (node .keys )
781+ if isinstance (node , ast .Constant ) and isinstance (node .value , (str , bytes )):
782+ return bool (node .value )
783+ return None
784+
785+ def _analyze_comprehension_body (
786+ self , node : ast .ListComp | ast .SetComp | ast .DictComp | ast .GeneratorExp
787+ ) -> None :
788+ base = self ._snapshot_frames ()
789+ definite = True
715790 targets = [
716791 name for generator in node .generators for name in self ._target_names ([generator .target ])
717792 ]
718- self .scopes .append (_ReflectiveScope (shadowed = set (targets )))
719- for index , generator in enumerate (node .generators ):
720- if index :
721- self .visit (generator .iter )
722- for condition in generator .ifs :
723- self .visit (condition )
724- if isinstance (node , ast .DictComp ):
725- self .visit (node .key )
726- self .visit (node .value )
727- else :
728- self .visit (node .elt )
729- self .scopes .pop ()
793+ self .scopes .append (_ReflectiveScope (shadowed = set (targets ), is_comprehension = True ))
794+ try :
795+ for index , generator in enumerate (node .generators ):
796+ if index :
797+ self .visit (generator .iter )
798+ iteration = self ._literal_iteration_state (generator .iter )
799+ if iteration is False :
800+ return
801+ definite = definite and iteration is True and not generator .is_async
802+ for condition in generator .ifs :
803+ self .visit (condition )
804+ if isinstance (condition , ast .Constant ):
805+ if not condition .value :
806+ return
807+ else :
808+ definite = False
809+ if isinstance (node , ast .DictComp ):
810+ self .visit (node .key )
811+ self .visit (node .value )
812+ else :
813+ self .visit (node .elt )
814+ finally :
815+ self .scopes .pop ()
816+ # An unknown iterator/filter may execute zero times. Keep both
817+ # feasible binding states rather than clearing a possible sink.
818+ if not definite :
819+ for scope , snapshot in base .values ():
820+ self ._check_runtime ()
821+ scope .restore (self ._merge_scope (snapshot , snapshot , scope ))
730822
731823 def _visit_expression (self , expression : ast .expr ) -> None :
732824 pending : list [tuple [str , ast .AST ]] = [("visit" , expression )]
@@ -735,7 +827,7 @@ def _visit_expression(self, expression: ast.expr) -> None:
735827 action , item = pending .pop ()
736828 if action == "bind_named" :
737829 assert isinstance (item , ast .NamedExpr )
738- self ._bind ([item .target ], item .value )
830+ self ._bind ([item .target ], item .value , named_expression = True )
739831 continue
740832 if action == "finish_call_callee" :
741833 assert isinstance (item , ast .Call )
0 commit comments