Skip to content

fix: replace mutable default set with None in wait_for_job_status#330

Open
Roaimkhan wants to merge 2 commits intokubeflow:mainfrom
Roaimkhan:enhancement
Open

fix: replace mutable default set with None in wait_for_job_status#330
Roaimkhan wants to merge 2 commits intokubeflow:mainfrom
Roaimkhan:enhancement

Conversation

@Roaimkhan
Copy link

@Roaimkhan Roaimkhan commented Feb 25, 2026

What this PR does / why we need it:
Python evaluates default argument values once at function definition time. Using a mutable set literal as a default means all callers share the same object — if it's ever mutated, every subsequent call using the default sees the corrupted state.

This PR fixes all instances of this pattern across the trainer and optimizer backends by replacing the mutable default with None and assigning the real value inside the function body, which is the canonical Python solution.
Before:

def wait_for_job_status(
    self,
    name: str,
    status: set[str] = {constants.TRAINJOB_COMPLETE},  # shared mutable object!
    ...

After:

def wait_for_job_status(
    self,
    name: str,
    status: set[str] | None = None,
    ...
) -> types.TrainJob:
    if status is None:
        status = {constants.TRAINJOB_COMPLETE}  # fresh set per call
    ...

Fixes #329

Checklist:

  • Docs included if any changes are user facing

Copilot AI review requested due to automatic review settings February 25, 2026 18:48
@google-oss-prow
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign astefanutti for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Contributor

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 Python anti-pattern where mutable set literals were used as default arguments in wait_for_job_status methods across trainer and optimizer backends. Using mutable defaults can cause all callers to share the same object, leading to subtle bugs if that object is ever mutated. The fix follows the canonical Python pattern: replace the mutable default with None and assign the real default value inside the function body.

Changes:

  • Updated all wait_for_job_status method signatures to use status: set[str] | None = None instead of status: set[str] = {constants.CONSTANT}
  • Added if status is None: checks at the beginning of each method to assign the default value
  • Updated docstring in trainer_client.py to document the default value

Reviewed changes

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

Show a summary per file
File Description
kubeflow/trainer/backends/localprocess/backend.py Fixed mutable default in wait_for_job_status
kubeflow/trainer/backends/kubernetes/backend.py Fixed mutable default in wait_for_job_status
kubeflow/trainer/backends/container/backend.py Fixed mutable default in wait_for_job_status
kubeflow/trainer/backends/base.py Fixed mutable default in abstract base method
kubeflow/trainer/api/trainer_client.py Fixed mutable default and updated docstring
kubeflow/optimizer/backends/kubernetes/backend.py Fixed mutable default in wait_for_job_status
kubeflow/optimizer/backends/base.py Fixed mutable default in abstract base method
kubeflow/optimizer/api/optimizer_client.py Fixed mutable default in wait_for_job_status

Signed-off-by: Roaim <roimkhan459@gmail.com>
Signed-off-by: Roaim <roimkhan459@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug:Mutable Default Set Argument

2 participants