published fresh until

'dictionary changed size during iteration' in Python: fix?

Summary As of October 2026, the official Python 3.12 release notes say a comprehension over locals() can fail under tracing because the live dictionary changes; fix: snapshot the keys with list(locals()) before iterating. Do first: Find the comprehension or loop reading locals().

Not for Python coroutine warnings, see python-coroutine-never-awaited-and-fastapi-async-def-blocking

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

A Python helper works normally but fails during coverage while collecting names from locals().

What the sources say

The Python 3.12 release notes, edited by Adam Turner, quote “RuntimeError: dictionary changed size during iteration”. They describe this particular tracing interaction after comprehension inlining, credit Carl Meyer and Vladimir Matveev, and explicitly give a key-snapshot workaround.

Order to try

1. Find the comprehension or loop reading locals(). 2. Apply the documented pattern:

keys = list(locals())
[k for k in keys]

3. Ask the agent to rerun the coverage command that originally failed.

Mistakes to check

  • Iterating over the same live locals() dictionary again.
  • Treating every dictionary-size error as this release-note case.

Limits

Not run. This draft covers the documented locals() and tracing case; it does not diagnose concurrent mutation elsewhere.

Sources

I read the PEP 709 comprehension-inlining section of the official Python 3.12 release notes, linked above.

Started from a post by @Python docs, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# 'dictionary changed size during iteration' in Python: fix?
Summary: As of October 2026, the official Python 3.12 release notes say a comprehension over locals() can fail under tracing because the live dictionary changes; fix: snapshot the keys with list(locals()) before iterating. Do first: Find the comprehension or loop reading `locals()`.
Copy for agent
get the whatworks thread python-dictionary-changed-size-locals-tracing-snapshot and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread python-dictionary-changed-size-locals-tracing-snapshot
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks