Move callback validation to __init__#574
Open
Tushar8466 wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to ensure callback method signatures are validated on the actual overridden methods (including user subclasses), by moving callback validation from class-definition time to instance creation time.
Changes:
- Run
validate_callback(self)insideCallBack.__init__so overriddenon_loop_start/on_loop_endmethods are validated per instance. - Remove
@validate_callbackusage from built-in callback class definitions (Deviance,Accuracy,Diffs,Coef). - Add inline notes describing the new validation approach.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Summary
This PR fixes a bug where the
@validate_callbackdecorator applied atclass definition time failed to validate overridden
on_loop_startoron_loop_endmethods in subclasses ofCallBack.🐛 Problem
In the original code,
@validate_callbackwas applied as a class-leveldecorator:
This means validation ran once at import time on the parent class
methods only. If a user subclassed
Devianceand overrodeon_loop_start,the new method was never validated:
This could silently pass through and only crash at runtime during model
fitting, with a confusing error unrelated to the real cause.
✅ Fix
Moved validation into
CallBack.__init__so it runs at instancecreation time on the actual instance — including any overridden methods:
Also removed
@validate_callbackdecorators from all subclass definitions(
Deviance,Accuracy,Diffs,Coef) since validation is now handledautomatically by the base class.
📂 Files Changed
pygam/callbacks.pyvalidate_callback(self)inCallBack.__init__pygam/callbacks.py@validate_callbackfromDeviance,Accuracy,Diffs,Coef🔍 Before vs After
🧪 How to Test
🔗 Related Issue
Closes #[ISSUE_NUMBER]
📝 Notes
validate_callbackfunction itself is unchanged — only whenit is called has changed.
_validatedflag insidevalidate_callbackensures there isno double validation if the function is somehow called twice on
the same instance.
correctly written subclasses will continue to work exactly as before.