Skip to content

Support cloning functions that reassign their module globals - #4

Open
aarranz wants to merge 1 commit into
aafrecct:mainfrom
aarranz:fix/reassigned-module-global
Open

Support cloning functions that reassign their module globals#4
aarranz wants to merge 1 commit into
aafrecct:mainfrom
aarranz:fix/reassigned-module-global

Conversation

@aarranz

@aarranz aarranz commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

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.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant