Skip to content

fix: avoid mutable input kwargs defaults#10576

Open
he-yufeng wants to merge 1 commit into
hiyouga:mainfrom
he-yufeng:fix/hf-engine-input-kwargs-default
Open

fix: avoid mutable input kwargs defaults#10576
he-yufeng wants to merge 1 commit into
hiyouga:mainfrom
he-yufeng:fix/hf-engine-input-kwargs-default

Conversation

@he-yufeng

Copy link
Copy Markdown

Summary

Removes the shared mutable {} defaults from the HuggingFace chat engine helpers.

_process_args() and _get_scores() both consume input_kwargs with .pop(). With {} as the function default, calls that omitted input_kwargs shared the same dict object. This patch changes the four HuggingfaceEngine helper signatures to default to None and normalizes to a fresh dict in the two helpers that pop values.

Closes #10476.

To verify

  • PYTHONPATH=src python -m pytest tests\chat\test_hf_engine.py -q
  • python -m py_compile src\llamafactory\chat\hf_engine.py tests\chat\test_hf_engine.py
  • python -m ruff check src\llamafactory\chat\hf_engine.py tests\chat\test_hf_engine.py
  • git diff --check

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request resolves a common Python issue by replacing mutable default dictionary arguments ({}) with None for input_kwargs across several methods in HuggingfaceEngine, and adds a unit test to verify this behavior. The reviewer suggests a further improvement: instead of using input_kwargs = input_kwargs or {}, a shallow copy should be created (e.g., input_kwargs.copy()) to prevent mutating the caller's original dictionary when .pop() is called on it.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

input_kwargs: Optional[dict[str, Any]] = {},
input_kwargs: Optional[dict[str, Any]] = None,
) -> tuple[dict[str, Any], int]:
input_kwargs = input_kwargs or {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Mutating input_kwargs via .pop() directly modifies the dictionary passed by the caller, which can lead to unexpected side effects if the caller reuses the dictionary. It is safer to create a shallow copy of the dictionary.

Suggested change
input_kwargs = input_kwargs or {}
input_kwargs = input_kwargs.copy() if input_kwargs else {}

input_kwargs: Optional[dict[str, Any]] = {},
input_kwargs: Optional[dict[str, Any]] = None,
) -> list[float]:
input_kwargs = input_kwargs or {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Mutating input_kwargs via .pop() directly modifies the dictionary passed by the caller, which can lead to unexpected side effects if the caller reuses the dictionary. It is safer to create a shallow copy of the dictionary.

Suggested change
input_kwargs = input_kwargs or {}
input_kwargs = input_kwargs.copy() if input_kwargs else {}

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.

fix: mutable default argument input_kwargs={} in HuggingfaceEngine static methods

1 participant