fix: replace mutable default arguments with None#1260
Open
Mr-Neutr0n wants to merge 1 commit intoOpenGVLab:mainfrom
Open
fix: replace mutable default arguments with None#1260Mr-Neutr0n wants to merge 1 commit intoOpenGVLab:mainfrom
Mr-Neutr0n wants to merge 1 commit intoOpenGVLab:mainfrom
Conversation
Using mutable default arguments (e.g. `def f(x=[])`) is a well-known Python anti-pattern because the default object is shared across all calls, leading to subtle and hard-to-debug state leakage between invocations. Replace mutable default lists with `None` and initialize inside the function body in: - `InternLM2ForCausalLM.build_inputs` (history=[]) - `InternLM2ForCausalLM.chat` (history=[]) - `InternLM2ForCausalLM.stream_chat` (history=[]) - `build_vtab_dataset` (classnames=[]) - `contains_quantity_word` (special_keep_words=[])
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.
Summary
Using mutable default arguments (e.g.
def f(history=[])) is a well-known Python anti-pattern (docs). The default object is evaluated once at function definition time and shared across all calls, so mutations in one call silently leak into subsequent calls.This PR replaces every mutable default list with
Noneand initializes a fresh list inside the function body.Changes
internvl_chat/internvl/model/internlm2/modeling_internlm2.pyInternLM2ForCausalLM.build_inputshistory=[]->history=Noneinternvl_chat/internvl/model/internlm2/modeling_internlm2.pyInternLM2ForCausalLM.chathistory=[]->history=Noneinternvl_chat/internvl/model/internlm2/modeling_internlm2.pyInternLM2ForCausalLM.stream_chathistory=[]->history=Noneclip_benchmark/clip_benchmark/datasets/builder.pybuild_vtab_datasetclassnames=[]->classnames=Noneinternvl_chat/eval/mathvista/utilities.pycontains_quantity_wordspecial_keep_words=[]->special_keep_words=NoneExample of the bug
Test plan
=[]or={}in function signatures across the codebaseif x is None: x = []) is placed before the parameter's first use