@@ -3814,6 +3814,78 @@ def _direct_shell_name(call: ast.Call) -> str | None:
38143814 return None
38153815
38163816
3817+ def _passive_assignment_name_snapshots (tree : ast .AST ) -> dict [ast .AST , frozenset [str ]]:
3818+ """Prove prior simple-name bindings in supported straight-line scopes."""
3819+ from . import static_python_shell_truthiness
3820+
3821+ snapshots : dict [ast .AST , frozenset [str ]] = {}
3822+ for scope in ast .walk (tree ):
3823+ if not isinstance (scope , (ast .Module , ast .FunctionDef , ast .AsyncFunctionDef )):
3824+ continue
3825+ bound_names = (
3826+ set (_parameter_names (scope .args )) if not isinstance (scope , ast .Module ) else set ()
3827+ )
3828+ declarations = static_python_shell_truthiness ._DirectBindingCollector (frozenset ())
3829+ for statement in scope .body :
3830+ declarations .visit (statement )
3831+ bound_names .update (declarations .nonlocal_names )
3832+ safe_names : set [str ] = set ()
3833+ trusted_names = set (static_python_shell_truthiness ._DIRECT_CALL_NAMES ).difference (
3834+ bound_names
3835+ )
3836+ unknown_effects = [False ]
3837+ for statement in scope .body :
3838+ if isinstance (statement , (ast .Assign , ast .AnnAssign )) and not unknown_effects [0 ]:
3839+ targets = (
3840+ statement .targets if isinstance (statement , ast .Assign ) else [statement .target ]
3841+ )
3842+ value = statement .value
3843+ if value is not None :
3844+ pairs = [
3845+ pair for target in targets for pair in _paired_store_values (target , value )
3846+ ]
3847+ if pairs and all (
3848+ isinstance (target , ast .Name )
3849+ and (target .id not in bound_names or target .id in safe_names )
3850+ for target , _ in pairs
3851+ ):
3852+ snapshots [statement ] = frozenset (
3853+ item .id
3854+ for item in ast .walk (value )
3855+ if isinstance (item , ast .Name ) and item .id in safe_names
3856+ )
3857+ if isinstance (statement , (ast .Import , ast .ImportFrom )):
3858+ native_import = (
3859+ isinstance (statement , ast .Import )
3860+ and all (
3861+ alias .name == "subprocess" and alias .asname in (None , "subprocess" )
3862+ for alias in statement .names
3863+ )
3864+ or isinstance (statement , ast .ImportFrom )
3865+ and statement .level == 0
3866+ and statement .module == "subprocess"
3867+ and all (
3868+ alias .name == "Popen" and alias .asname in (None , "Popen" )
3869+ for alias in statement .names
3870+ )
3871+ )
3872+ if not native_import :
3873+ # Arbitrary module initialization can change existing names.
3874+ unknown_effects [0 ] = True
3875+ safe_names .clear ()
3876+ static_python_shell_truthiness ._advance_trusted_names (
3877+ statement , trusted_names , bound_names , safe_names , unknown_effects
3878+ )
3879+ if isinstance (statement , (ast .Import , ast .ImportFrom )) and not unknown_effects [0 ]:
3880+ # Only a fresh, proven native import adds release-safe names.
3881+ safe_names .update (
3882+ trusted_names .intersection (
3883+ static_python_shell_truthiness ._direct_bound_names (statement )
3884+ )
3885+ )
3886+ return snapshots
3887+
3888+
38173889def _build_variable_shell_ast_index (parsed : ParsedPythonFile ) -> _VariableShellAstIndex | None :
38183890 """Index relevant nodes, bindings, and declarations in one AST traversal."""
38193891 tree = parsed .tree
@@ -3833,6 +3905,7 @@ def _build_variable_shell_ast_index(parsed: ParsedPythonFile) -> _VariableShellA
38333905 # self-stores. They neither replace nor restore a binding.
38343906 identity_name_stores : set [ast .Name ] = set ()
38353907 native_aliases = _native_receiver_aliases (tree )
3908+ passive_assignment_snapshots = _passive_assignment_name_snapshots (tree )
38363909
38373910 def mark_attribute_identity_stores (target : ast .AST , value : ast .AST ) -> None :
38383911 for destination , source in _paired_store_values (target , value ):
@@ -3846,16 +3919,49 @@ def mark_attribute_identity_stores(target: ast.AST, value: ast.AST) -> None:
38463919 ):
38473920 attribute_identity_stores .add (destination )
38483921
3849- def record_name_store_values (scope : ast .AST , target : ast .AST , value : ast .AST , end : int ) -> None :
3922+ def record_name_store_values (
3923+ scope : ast .AST , targets : list [ast .AST ], value : ast .AST , end : int
3924+ ) -> None :
3925+ from . import static_python_shell_truthiness
3926+
38503927 # Pair unpacked targets with their RHS elements so a native alias, a
38513928 # truthy flag, or a self-store is not mistaken for a replacement.
3852- for destination , source in _paired_store_values (target , value ):
3853- if not isinstance (destination , ast .Name ):
3854- continue
3929+ pairs = [
3930+ (destination , source )
3931+ for target in targets
3932+ for destination , source in reversed (list (_paired_store_values (target , value )))
3933+ ]
3934+ stores = [
3935+ (destination , source )
3936+ for destination , source in pairs
3937+ if isinstance (destination , ast .Name )
3938+ ]
3939+ # The paired iterator is stack-based. Restore Python's left-to-right
3940+ # target order before deciding which occurrence owns the final value.
3941+ final_values = {destination .id : source for destination , source in stores }
3942+ safe_names = passive_assignment_snapshots .get (node )
3943+ safe_stores = safe_names is not None
3944+ safe_value = (
3945+ safe_names is not None
3946+ and static_python_shell_truthiness ._is_finalizer_safe_value (value , set (safe_names ))
3947+ )
3948+ # Unsupported RHS effects or target protocols cannot prove the final
3949+ # flag. Preserve the existing conservative event policy for those shapes.
3950+ false_values = (
3951+ {
3952+ source
3953+ for source in set (final_values .values ())
3954+ if static_python_shell_truthiness ._truth_value (source , {}) is False
3955+ }
3956+ if safe_stores and safe_value
3957+ else set ()
3958+ )
3959+ definitely_false = {name for name , source in final_values .items () if source in false_values }
3960+ for destination , source in stores :
38553961 keeps_signal = _store_keeps_shell_signal (destination .id , source , native_aliases )
38563962 if keeps_signal is None :
38573963 identity_name_stores .add (destination )
3858- elif keeps_signal :
3964+ elif keeps_signal and destination . id not in definitely_false :
38593965 record_binding (scope , destination .id , end , True )
38603966
38613967 def record_binding (scope : ast .AST , name : str , start : int , restores : bool = False ) -> None :
@@ -4126,16 +4232,15 @@ def bind(scope: ast.AST, name: str, start: int) -> None:
41264232 if isinstance (node , ast .Assign ):
41274233 stack .append ((node .value , scope_chain , None ))
41284234 stack .extend ((target , scope_chain , end ) for target in node .targets )
4129- for target in node .targets :
4130- record_name_store_values (scope , target , node .value , end )
4235+ record_name_store_values (scope , list (node .targets ), node .value , end )
41314236 elif isinstance (node , (ast .AnnAssign , ast .AugAssign )):
41324237 if node .value is not None :
41334238 stack .append ((node .value , scope_chain , None ))
41344239 stack .append ((node .target , scope_chain , end ))
41354240 if isinstance (node , ast .AnnAssign ):
41364241 stack .append ((node .annotation , scope_chain , None ))
41374242 if node .value is not None :
4138- record_name_store_values (scope , node .target , node .value , end )
4243+ record_name_store_values (scope , [ node .target ] , node .value , end )
41394244 elif isinstance (node , ast .NamedExpr ):
41404245 stack .append ((node .value , scope_chain , None ))
41414246 # PEP 572 makes a walrus target inside one or more
@@ -4148,7 +4253,7 @@ def bind(scope: ast.AST, name: str, start: int) -> None:
41484253 ):
41494254 target_chain = target_chain [:- 1 ]
41504255 stack .append ((node .target , target_chain , end ))
4151- record_name_store_values (target_chain [- 1 ], node .target , node .value , end )
4256+ record_name_store_values (target_chain [- 1 ], [ node .target ] , node .value , end )
41524257 elif isinstance (node , (ast .For , ast .AsyncFor )):
41534258 iteration_span = _node_character_span (parsed , node .iter )
41544259 iteration_end = iteration_span [1 ] if iteration_span is not None else end
0 commit comments