Skip to content

Replace mutable default arguments with None#43961

Open
DimiChatzipavlis wants to merge 1 commit intohuggingface:mainfrom
DimiChatzipavlis:fix-mutable-defaults-idefics
Open

Replace mutable default arguments with None#43961
DimiChatzipavlis wants to merge 1 commit intohuggingface:mainfrom
DimiChatzipavlis:fix-mutable-defaults-idefics

Conversation

@DimiChatzipavlis
Copy link

What does this PR do?

Fixes a common Python pitfall regarding mutable default arguments.

In Python, default arguments are evaluated only once at function definition time. If a mutable object (like a list) is used as a default, that same list instance is shared across all calls to the function. This can lead to insidious bugs where the list grows unexpectedly between function calls or instances.

Modifications

Refactored the following method signatures to use None as the default value and initialize the list inside the function:

  • src/transformers/models/idefics/modeling_idefics.py:

    • freeze_model(..., module_exceptions=[]) -> module_exceptions=None
    • freeze_text_layers(..., module_exceptions=[]) -> module_exceptions=None
    • freeze_vision_layers(..., module_exceptions=[]) -> module_exceptions=None
  • src/transformers/debug_utils.py:

    • DebugUnderflowOverflow.__init__(..., trace_batch_nums=[]) -> trace_batch_nums=None

Verification

  • Verified that the logic inside the functions correctly initializes the list if the argument is None (if arg is None: arg = []).
  • Verified that existing calls passing explicit lists are unaffected.

Who can review?

@sgugger
@amyeroberts
@ArthurZucker

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

@github-actions
Copy link
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: idefics

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a common Python pitfall where mutable default arguments (specifically empty lists []) are shared across function calls, which can lead to unexpected behavior. The fix replaces these mutable defaults with None and initializes the list inside each function when needed.

Changes:

  • Replaced mutable default argument module_exceptions=[] with module_exceptions=None in three functions in the Idefics model
  • Replaced mutable default argument trace_batch_nums=[] with trace_batch_nums=None in the DebugUnderflowOverflow class
  • Added appropriate if None checks inside each function to initialize empty lists

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/transformers/models/idefics/modeling_idefics.py Updated freeze_model, freeze_text_layers, and freeze_vision_layers to use None as default with internal initialization
src/transformers/debug_utils.py Updated DebugUnderflowOverflow.__init__ to use None as default for trace_batch_nums with internal initialization

@Rocketknight1
Copy link
Member

These methods are all very short and none of them mutate the input argument, though! I agree that it's an antipattern, which is why your code agent picked it up when asked to "improve the codebase" or however you prompted it, but it seems too insignificant to fix right now

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.

2 participants