Skip to content

fix: replace mutable default argument in override_dist_dtype_device_args#554

Open
Mr-Neutr0n wants to merge 1 commit intozai-org:mainfrom
Mr-Neutr0n:fix/mutable-default-arguments
Open

fix: replace mutable default argument in override_dist_dtype_device_args#554
Mr-Neutr0n wants to merge 1 commit intozai-org:mainfrom
Mr-Neutr0n:fix/mutable-default-arguments

Conversation

@Mr-Neutr0n
Copy link
Copy Markdown

Summary

  • Replace mutable default argument b={} with b=None in override_dist_dtype_device_args in both utils/models/cogvlm_model.py and utils/models/cogagent_model.py
  • Initialize b to {} inside the function body when None is passed

Motivation

Using a mutable default argument (b={}) is a well-known Python anti-pattern (docs). The default dict is created once at function definition time and shared across all calls. If it is ever mutated, the mutation persists into subsequent calls, leading to subtle and hard-to-diagnose bugs.

Changes

utils/models/cogvlm_model.py (line 38):

# Before
def override_dist_dtype_device_args(args, b={}):

# After
def override_dist_dtype_device_args(args, b=None):
    if b is None:
        b = {}

utils/models/cogagent_model.py (line 39):
Same fix applied.

Test plan

  • Verified the fix is syntactically correct
  • No functional change when callers pass an explicit dict
  • Callers relying on the default now get a fresh dict each time (correct behavior)

Using a mutable default argument (b={}) is a known Python pitfall:
the dict is created once at function definition time and shared across
all calls, which can lead to subtle, hard-to-diagnose bugs when the
dict is mutated.

Replace `b={}` with `b=None` and initialize inside the function body
in both cogvlm_model.py and cogagent_model.py.
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