Skip to content

[BUG] Fix validate_callback_data flagging local variables as missing args - #595

Open
sumitjhadev wants to merge 1 commit into
dswah:mainfrom
sumitjhadev:fix/callback-local-vars
Open

[BUG] Fix validate_callback_data flagging local variables as missing args#595
sumitjhadev wants to merge 1 commit into
dswah:mainfrom
sumitjhadev:fix/callback-local-vars

Conversation

@sumitjhadev

@sumitjhadev sumitjhadev commented Aug 31, 2026

Copy link
Copy Markdown

method.code.co_varnames includes every local variable declared inside a function body, not just its formal parameters. Since validate_callback_data used co_varnames directly to determine which keys a callback needs pulled from the training loop's variables dict, any custom CallBack.on_loop_start or on_loop_end that declares its own local variables was incorrectly flagged as a missing argument and raised: AssertionError: CallBack cannot reference: step_threshold

Fix: slice co_varnames down to co_argcount, which isolates the function's formal parameters from its internal locals. Built-in callbacks (Deviance/Diffs/Accuracy) are unaffected since none of them declare local variables, which is also why this had gone unnoticed.

Adds pygam/tests/test_callbacks.py with a unit test reproducing the original AssertionError, a check that missing genuine arguments are still caught, and an end-to-end GAM.fit() using a custom callback with a local variable.

Testing: pytest -s pygam/tests -> 165 passed, 1 skipped, matching main's baseline.

Fixes #596

@sumitjhadev
sumitjhadev force-pushed the fix/callback-local-vars branch from 8438a2a to 5602279 Compare August 31, 2026 10:02
…args

method.__code__.co_varnames includes every local variable declared
inside a function body, not just its formal parameters. Since
validate_callback_data used co_varnames directly to determine which
keys a callback needs pulled from the training loop's variables dict,
any custom CallBack.on_loop_start or on_loop_end that declares its own
local variables was incorrectly flagged as a missing argument and
raised: AssertionError: CallBack cannot reference: step_threshold

Fix: slice co_varnames down to co_argcount, which isolates the
function's formal parameters from its internal locals. Built-in
callbacks (Deviance/Diffs/Accuracy) are unaffected since none of them
declare local variables, which is also why this had gone unnoticed.

Adds pygam/tests/test_callbacks.py with a unit test reproducing the
original AssertionError, a check that missing genuine arguments are
still caught, and an end-to-end GAM.fit() using a custom callback
with a local variable.

Testing: pytest -s pygam/tests -> 165 passed, 1 skipped, matching
main's baseline.
@sumitjhadev
sumitjhadev force-pushed the fix/callback-local-vars branch from 5602279 to dea59e3 Compare August 31, 2026 10:16
@sumitjhadev sumitjhadev changed the title [BUG] Fix validate_callback_data flagging local variables as missing args Fixes #596 Aug 31, 2026
@sumitjhadev sumitjhadev changed the title Fixes #596 [BUG] Fix validate_callback_data flagging local variables as missing args Aug 31, 2026
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] validate_callback_data flags local variables as missing callback args

1 participant