Support cloning functions that reassign their module globals - #4
Open
aarranz wants to merge 1 commit into
Open
Conversation
A cloned function that reassigns one of its own module-level globals (`global x; x = ...`) could not be executed: CPython's STORE_GLOBAL writes the raw value straight into the globals dict via the C dict API, bypassing DefaultMockingContext.__setitem__, so the value is not wrapped in a MockItem. Reading the name back then raised `AttributeError: 'str' object has no attribute 'metadata'`. The context now adopts raw (non-MockItem) values into a MockItem on read, tagged with a new MockOrigin.REASSIGNED_GLOBAL origin, and reset() tolerates raw values that were never read back. Using a sentinel for the presence check also fixes a latent bug where a stored falsy value would be treated as absent. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A cloned function that reassigns one of its own module-level globals (
global x; x = ...) could not be executed: CPython's STORE_GLOBAL writes the raw value straight into the globals dict via the C dict API, bypassing DefaultMockingContext.setitem, so the value is not wrapped in a MockItem. Reading the name back then raisedAttributeError: 'str' object has no attribute 'metadata'.The context now adopts raw (non-MockItem) values into a MockItem on read, tagged with a new MockOrigin.REASSIGNED_GLOBAL origin, and reset() tolerates raw values that were never read back. Using a sentinel for the presence check also fixes a latent bug where a stored falsy value would be treated as absent.