Skip to content

fix(core): prevent mutation via deepcopy in get_params#562

Open
Aditya-9215 wants to merge 1 commit intodswah:mainfrom
Aditya-9215:fix-get-params-mutation
Open

fix(core): prevent mutation via deepcopy in get_params#562
Aditya-9215 wants to merge 1 commit intodswah:mainfrom
Aditya-9215:fix-get-params-mutation

Conversation

@Aditya-9215
Copy link
Copy Markdown

@Aditya-9215 Aditya-9215 commented Mar 30, 2026

Summary

Fixes issue where get_params() returns references to mutable nested objects,
allowing unintended mutation of internal model state.

Problem

Currently:

params = gam.get_params()
params["terms"][0].lam = 1000

mutates the original model.

This violates sklearn’s estimator API expectations and can break clone(),
GridSearchCV, and reproducibility.

Solution

  • Updated Core.get_params() to return deep copies of parameters
  • Prevents mutation of nested objects (e.g., Term, TermList)
  • Fix is applied centrally at Core level so all subclasses inherit correct behavior

Tests

Added test:
test_get_params_does_not_mutate_model

Notes

Deepcopy introduces minor overhead but ensures correctness and API compliance.

🔗 Related Issue

Closes #522

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.

[BUG] get_params() exposes mutable nested objects allowing external state mutation

1 participant