@@ -2537,13 +2537,20 @@ def inner(arg: x):
25372537 # With no explicit namespaces, the value from the enclosing
25382538 # scope, held in the cell, is used.
25392539 self .assertEqual (fwdref .evaluate (), 1 )
2540- # Explicitly passed namespaces take precedence over the cell.
2540+ # An explicitly passed locals takes precedence over the cell,
2541+ # but globals does not: the cell holds a nonlocal binding, which
2542+ # sits between locals and globals in the lookup order (as in
2543+ # the dict-of-cells path, where the cells are merged into the
2544+ # locals with setdefault).
25412545 self .assertEqual (fwdref .evaluate (locals = {"x" : 2 }), 2 )
2542- self .assertEqual (fwdref .evaluate (globals = {"x" : 3 }), 3 )
2546+ self .assertEqual (fwdref .evaluate (globals = {"x" : 3 }), 1 )
25432547 self .assertEqual (fwdref .evaluate (globals = {"x" : 3 }, locals = {"x" : 2 }), 2 )
25442548 self .assertEqual (
25452549 fwdref .evaluate (locals = {"x" : 2 }, format = Format .FORWARDREF ), 2
25462550 )
2551+ self .assertEqual (
2552+ fwdref .evaluate (globals = {"x" : 3 }, format = Format .FORWARDREF ), 1
2553+ )
25472554 # If the name is not in the explicit namespaces, the cell
25482555 # value is used as a fallback.
25492556 self .assertEqual (fwdref .evaluate (locals = {}), 1 )
@@ -2568,11 +2575,12 @@ def inner(arg: x):
25682575 self .assertEqual (fwdref .evaluate (locals = {"x" : 2 }), 2 )
25692576 self .assertEqual (fwdref .evaluate (globals = {"x" : 3 }), 3 )
25702577
2571- # Refilling the cell restores it as the fallback, but explicit
2572- # namespaces still take precedence over it.
2578+ # Refilling the cell restores it: an explicitly passed locals
2579+ # still takes precedence over it, but globals does not .
25732580 x = 7
25742581 self .assertEqual (fwdref .evaluate (), 7 )
25752582 self .assertEqual (fwdref .evaluate (locals = {"x" : 2 }), 2 )
2583+ self .assertEqual (fwdref .evaluate (globals = {"x" : 3 }), 7 )
25762584
25772585 def test_fwdref_final_class (self ):
25782586 with self .assertRaises (TypeError ):
0 commit comments