feat: add swanlab.merge_callbacks API and callbacker tests#1591
Merged
SAKURA-CAT merged 3 commits intomainfrom Apr 30, 2026
Merged
feat: add swanlab.merge_callbacks API and callbacker tests#1591SAKURA-CAT merged 3 commits intomainfrom
SAKURA-CAT merged 3 commits intomainfrom
Conversation
Refactor callback handling to support injecting per-run callbacks while preserving global defaults. Key changes: - swanlab/sdk/cmd/init.py: Accept callbacks as Iterable, pass callbacks into _init and into RunContext instead of merging into a global callbacker. - swanlab/sdk/internal/context/__init__.py: RunContext now accepts optional callbacks and builds its callback manager from them. - swanlab/sdk/internal/context/components/callbacker/__init__.py: Introduce global_callbacker, make CallbackManager accept an initial dict, and change create_callback_manager to merge global callbacks with injected per-run callbacks (local callbacks take precedence). Adjust type hints and API accordingly. - swanlab/sdk/internal/context/components/__init__.py: Remove exporting the previous global callbacker symbol. - tests/unit/conftest.py: Updated to reference global_callbacker and clear its registered callbacks during test isolation. Notes: This is a behavioral/API change — the old module-level "callbacker" usage was removed in favor of "global_callbacker" plus per-run injection via create_callback_manager(callbacks=...). Call signatures and exported symbols were updated accordingly.
Introduce a merge_callbacks API and refactor callback manager behavior. - Add swanlab/sdk/cmd/merge_callbacks.py implementing merge_callbacks (protected by with_cmd_lock and without_run) and export it from swanlab.sdk and top-level swanlab package (including .pyi typing stub). - Refactor CallbackManager: simplify __init__ to start with an empty registry and accept Optional[Iterable[Callback]] in merge_callbacks (no-op for None/empty). - Change create_callback_manager to build a new CallbackManager that first merges global callbacks and then local ones (local callbacks override global and do not mutate global state). - Add comprehensive unit tests tests/unit/sdk/internal/context/components/callbacker/test_callbacker.py to cover merging, overwriting, removal, dispatch, and isolation between global and local callbacks. These changes allow registering global callbacks before swanlab.init(), ensure local callbacks can override globals without polluting global state, and add tests to verify behavior.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces the merge_callbacks function to the SwanLab SDK, allowing users to register custom callbacks before run initialization. The internal architecture has been refactored to support both global and local callback registries, ensuring that local callbacks take precedence. The review feedback focuses on improving type hint consistency across the SDK by suggesting the use of Union[Iterable[Callback], Callback] for callback parameters in several internal functions and ensuring the necessary Union typing import is included. Additionally, a refactoring opportunity was identified to simplify callback merging if internal methods are updated to handle the union type directly.
Introduce swanlab.sdk.typings.context.CallbacksType (single Callback or iterable) and update callback-related APIs to use it. Change callback manager to accept a single Callback instance (normalize to list) and guard against None, update merge_callbacks, RunContext, init internals, and type stubs to use the new alias. Add a deprecated register_callbacks wrapper in package __init__ for backwards compatibility and update tests to cover single-callback cases.
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
swanlab.merge_callbacks()公开 API,允许用户在swanlab.init()之前注册全局回调CallbackManager,移除callbacks_dict构造参数,统一通过merge_callbacks注入callbacker模块单元测试(12 cases)Changes
swanlab/sdk/cmd/merge_callbacks.pymerge_callbacks命令实现swanlab/__init__.pymerge_callbacksswanlab/__init__.pyimerge_callbacks类型声明swanlab/sdk/__init__.pymerge_callbacksswanlab/sdk/internal/context/components/callbacker/__init__.pytests/unit/sdk/internal/context/components/callbacker/test_callbacker.pyUsage