Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

Fix mutable default argument in __deepcopy__ methods#186

Open
Mr-Neutr0n wants to merge 1 commit intosalesforce:mainfrom
Mr-Neutr0n:fix/deepcopy-mutable-default
Open

Fix mutable default argument in __deepcopy__ methods#186
Mr-Neutr0n wants to merge 1 commit intosalesforce:mainfrom
Mr-Neutr0n:fix/deepcopy-mutable-default

Conversation

@Mr-Neutr0n
Copy link

Summary

All __deepcopy__ methods across the codebase use a mutable default argument (memodict={}). Mutable defaults are shared across all calls to the function, which is a well-known Python gotcha that can lead to subtle, hard-to-debug issues.

This PR replaces memodict={} with memodict=None and adds a guard clause (if memodict is None: memodict = {}) inside each method body.

Changes

Fixed 7 instances across 6 files:

  • merlion/utils/conj_priors.pyConjPrior.__deepcopy__
  • merlion/post_process/base.pyPostRuleBase.__deepcopy__
  • merlion/models/base.pyConfig.__deepcopy__ and ModelBase.__deepcopy__
  • merlion/models/layers.pyLayeredModelConfig.__deepcopy__
  • merlion/models/ensemble/base.pyEnsembleConfig.__deepcopy__
  • merlion/models/ensemble/combine.pyCombinerBase.__deepcopy__

Test plan

  • Existing tests pass (pytest / CI)
  • No behavioral change expected — the fix only avoids potential state leakage between calls

Replace `memodict={}` with `memodict=None` and add a guard clause
to initialize to `{}` inside the method body. Mutable default
arguments are shared across all calls to the function, which can
lead to subtle bugs (see https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments).

Affected files:
- merlion/utils/conj_priors.py
- merlion/post_process/base.py
- merlion/models/base.py (2 instances)
- merlion/models/layers.py
- merlion/models/ensemble/base.py
- merlion/models/ensemble/combine.py
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant