fix: emit deprecation warning only when deprecated aliases are accessed#1380
Merged
zastrowm merged 2 commits intostrands-agents:mainfrom Jan 2, 2026
Merged
Conversation
zastrowm
previously approved these changes
Dec 31, 2025
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Previously, the deprecation warning was emitted at module import time, which triggered whenever `strands` was imported because other modules import from `experimental.hooks`. Changed to use `__getattr__` to lazily emit the warning only when the deprecated aliases (BeforeToolInvocationEvent, AfterToolInvocationEvent, BeforeModelInvocationEvent, AfterModelInvocationEvent) are actually accessed. Fixes strands-agents#1236
6cdc814 to
5e8262b
Compare
Member
|
I made a mess adding a return type and rebasing to main, but I think it's good now |
zastrowm
approved these changes
Jan 2, 2026
dbschmigelski
approved these changes
Jan 2, 2026
7 tasks
manoj-selvakumar5
pushed a commit
to manoj-selvakumar5/strands-sdk-python
that referenced
this pull request
Feb 5, 2026
…ed (strands-agents#1380) Previously, the deprecation warning was emitted at module import time, which triggered whenever `strands` was imported because other modules import from `experimental.hooks`. Changed to use `__getattr__` to lazily emit the warning only when the deprecated aliases (BeforeToolInvocationEvent, AfterToolInvocationEvent, BeforeModelInvocationEvent, AfterModelInvocationEvent) are actually accessed. Fixes strands-agents#1236 --------- Co-authored-by: Mackenzie Zastrow <zastrowm@users.noreply.github.com>
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.
Problem
When importing
strandswith warnings enabled (PYTHONWARNINGS=once python -c "import strands"), a deprecation warning is emitted:This happens because the deprecation warning was emitted at module import time in
strands/experimental/hooks/events.py, which triggered wheneverstrandswas imported because other modules (likesession_manager.pyand_executor.py) import fromexperimental.hooks.Solution
Changed from module-level warning to lazy warning using
__getattr__:src/strands/experimental/hooks/events.py: Replaced the module-levelwarnings.warn()call andTypeAliasdefinitions with a__getattr__function that emits the warning only when deprecated aliases (BeforeToolInvocationEvent,AfterToolInvocationEvent,BeforeModelInvocationEvent,AfterModelInvocationEvent) are actually accessed.src/strands/experimental/hooks/__init__.py: Updated to not import the deprecated aliases at module level. Added a__getattr__function to delegate attribute access to the events module, which handles the deprecation warnings.tests/strands/experimental/hooks/test_hook_aliases.py: Updated the test to verify the new behavior (warning on access, not on import).Verification
Before fix:
After fix:
Fixes #1236