Fix mutable default arguments across codebase#286
Fix mutable default arguments across codebase#286williballenthin wants to merge 6 commits intomasterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
9afa3e4 to
96d0471
Compare
|
lgtm |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a comprehensive refactor to eliminate mutable default arguments throughout the codebase. This change significantly enhances the reliability and predictability of the system by preventing unintended shared state modifications across object instances and function calls. The fixes target various core components and a large number of API handlers, aligning the code with best practices for Python default argument handling. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request systematically addresses the mutable default argument anti-pattern across the codebase, which is a significant improvement for code correctness and predictability. The changes are largely correct and adhere to standard Python practices. I've identified one minor issue in DynCodeHook where the ctx parameter is not passed to its superclass constructor, which could lead to unexpected behavior. My review includes a suggestion to fix this. Overall, this is a solid and valuable refactoring effort.
Note: Security Review did not run due to the size of the PR.
|
can you rebase, please? |
Process.ldr_entries was a class-level mutable list shared across all instances, causing PEB corruption in sequential emulations. Move it to __init__. Also fix Process(user_modules=[]) and Hook(ctx=[]) family of mutable default arguments.
Replace argv=[], config={} defaults with None and materialize fresh
collections in __init__ bodies.
All ~1016 API handler methods used ctx={} as a mutable default argument.
Replace with ctx: dict[str, str] | None = None and guard with
ctx = ctx or {} in each method body.
Define ApiContext = dict[str, str] | None in api.py and reference it as api.ApiContext in all 1016 handler signatures, replacing the verbose inline dict[str, str] | None annotation.
852 of the 1016 handlers never reference ctx after the guard line.
Remove the dead `ctx = ctx or {}` assignment in those methods, keeping
it only in the 164 that actually use ctx downstream.
4d09769 to
a1fb393
Compare
Summary
Systematic fix for Python's mutable default argument antipattern across the codebase:
list = []shared across all instances, causing PEB corruption in sequential emulations. Moved to__init__.self.modules— latent shared-state bugctx={}: replaced withctx: dict[str, str] | None = Noneand actx = ctx or {}guardAll mutable defaults replaced with
None+ materialization in the method body, following standard Python practice.Test plan
ctx={}patterns in API handler directory🤖 Generated with Claude Code