fix: replace mutable default arguments with None in StatefulToolEnv and MCPEnv#1331
Open
vominh1919 wants to merge 1 commit into
Open
fix: replace mutable default arguments with None in StatefulToolEnv and MCPEnv#1331vominh1919 wants to merge 1 commit into
vominh1919 wants to merge 1 commit into
Conversation
…nd MCPEnv Two instances of the classic Python mutable default argument bug: 1. StatefulToolEnv.add_tool(args_to_skip=[]) — the default [] is created once at function definition time and shared across all calls. If any caller mutates it (e.g., args_to_skip.append(...)), subsequent calls without an explicit argument see the mutated list. 2. MCPEnv.__init__(mcp_servers=[]) — same issue. The default list is shared across all MCPEnv instances that don't pass mcp_servers explicitly. Fix: Replace `=[]` with `=None` and initialize inside the function body.
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.
Problem
Two instances of the classic Python mutable default argument bug:
1.
StatefulToolEnv.add_tool(args_to_skip=[])—stateful_tool_env.py:69The default
[]is created once at function definition time and shared across all calls. If any caller mutates it (e.g.,args_to_skip.append(x)), subsequent calls without an explicit argument see the mutated list. This is a well-known Python footgun.2.
MCPEnv.__init__(mcp_servers=[])—mcp_env.py:151Same issue — the default list is shared across all
MCPEnvinstances that don't passmcp_serversexplicitly.Fix
Replace
=[]with=Noneand initialize inside the function body:Also changed
if mcp_servers:toif mcp_servers is not None:to correctly handle an explicitly passed empty list (which is truthy withif mcp_servers:but should still be iterated).Tests
ast.parse()syntax check[]each time instead of a shared oneNote
Low Risk
Low risk: changes only adjust default parameter initialization to prevent shared mutable state; runtime behavior should be unchanged except eliminating cross-call/instance leakage.
Overview
Prevents Python mutable-default-argument bugs in
MCPEnv.__init__andStatefulToolEnv.add_toolby switching list defaults toNoneand instantiating fresh lists inside the method.Also updates the MCP server initialization guard to
if mcp_servers is not Noneso an explicitly provided empty list is handled consistently.Reviewed by Cursor Bugbot for commit 04ad13c. Bugbot is set up for automated code reviews on this repo. Configure here.