pyGAM icon indicating copy to clipboard operation
pyGAM copied to clipboard

fix(core): prevent get_params from mutating estimator state

Open Karman580 opened this issue 8 months ago • 0 comments

Summary

This PR fixes an issue where Core.get_params(deep=True) could expose mutable estimator state.

Previously, the method could return references to internal attributes, which meant that modifying the returned dictionary could unintentionally mutate the estimator configuration.

Fix

The implementation now constructs the parameter dictionary exclusively from the estimator's _include parameters and returns a deep copy when deep=True. This ensures that external modifications to the returned dictionary do not affect the estimator state.

Changes

  • Updated get_params() implementation in pygam/core.py
  • Added a regression test to ensure returned parameters do not mutate estimator state

Test

Added:

def test_get_params_does_not_mutate_state():
    from pygam import LinearGAM

    gam = LinearGAM()

    params = gam.get_params(deep=True)
    params["max_iter"] = 999

    assert gam.max_iter != 999
    assert "max_iter" in params

Karman580 avatar Mar 11 '26 17:03 Karman580