fix: emit deprecation warning only when deprecated aliases are accessed #1380
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