Skip to content

fix: replace mutable default arguments with None defaults#1063

Open
dieutx wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
dieutx:fix/mutable-default-arguments
Open

fix: replace mutable default arguments with None defaults#1063
dieutx wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
dieutx:fix/mutable-default-arguments

Conversation

@dieutx
Copy link
Copy Markdown

@dieutx dieutx commented Mar 24, 2026

Summary

Replace mutable default arguments (dict = {}, list = []) with None defaults in core library files. This is a well-known Python bug where mutable defaults are shared across all calls to a function — if the object is stored as an instance attribute or mutated, it silently corrupts state across unrelated callers.

Changed functions

  • Environment.__init__: map_kwargs is stored as self.map_kwargs, meaning all instances using the default share the same dict
  • Environment._ensure_prompt, _ensure_task, _format_dataset, _format_completion_dataset: map_kwargs spread via ** — safe today but fragile if any caller later stores or mutates it
  • EnvGroup.__init__, _format_dataset, _format_completion_dataset: same pattern as Environment
  • StatefulToolEnv.add_tool: args_to_skip is stored in self.skipped_args[tool_name] — all tools added without explicit args_to_skip share the same list
  • format_dataset in data_utils.py: map_kwargs spread via **
  • OpenAIChatCompletionsTokenClient.tokenize: extra_kwargs spread via **

Pattern applied

# Before
def foo(param: dict = {}):
    ...

# After  
def foo(param: dict | None = None):
    param = param or {}
    ...

Test plan

  • ruff check passes on all changed files
  • ruff format reports no changes needed
  • Full test suite passes: pytest tests/ --ignore=tests/test_envs --ignore=tests/test_envs.py

Note

Low Risk
Low risk, behavior-preserving change that prevents shared mutable defaults from leaking state across calls/instances. Minor risk that callers relied on mutating the passed-in default object (now always a fresh dict/list when omitted).

Overview
Fixes several functions that previously used mutable default arguments (e.g. dict = {} / list = []) by switching them to None defaults and initializing locally.

This affects dataset mapping kwargs plumbing (map_kwargs) across Environment, EnvGroup, and format_dataset, tool registration in StatefulToolEnv.add_tool (args_to_skip), and extra request parameters in OpenAIChatCompletionsTokenClient.tokenize (extra_kwargs), preventing accidental state sharing between calls or instances.

Written by Cursor Bugbot for commit e9b8054. This will update automatically on new commits. Configure here.

Replace dangerous mutable default arguments (dict={}, list=[]) with
None defaults across core library files. Mutable defaults are shared
across all calls to a function, which can cause silent data corruption
when the default object is stored or mutated.

Changed functions:
- Environment.__init__, _ensure_prompt, _ensure_task, _format_dataset,
  _format_completion_dataset: map_kwargs: dict = {} -> dict | None = None
- EnvGroup.__init__, _format_dataset, _format_completion_dataset:
  map_kwargs: dict = {} -> dict | None = None
- StatefulToolEnv.add_tool: args_to_skip: list[str] = [] -> list[str] | None = None
- format_dataset: map_kwargs: dict = {} -> dict | None = None
- OpenAIChatCompletionsTokenClient.tokenize: extra_kwargs: dict = {} -> dict | None = None
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