Conversation
Expose a module-level, lazy-access `run` attribute on swanlab: add __getattr__ to return the active SwanLabRun via get_run() or None if no run is active, and include `run` in __all__ (with a type-ignore in the stub). Move setting of __version__ earlier to ensure it's initialized. Update type stubs to declare run: Optional[SwanLabRun] and a short docstring. Update tests: import swanlab in conftest and use swanlab.run for cleanup, and add an end-to-end test file verifying swanlab.run semantics (None before init, SwanLabRun after init, equality with get_run(), and None after finish).
Add a keyword-only marker (*) to SwanLabRun logging methods (log_scalar, log_text, log_image, log_audio, log_video) and define_scalar in swanlab/sdk/internal/run/__init__.py. This enforces that callers provide key and other parameters as keywords to avoid accidental positional-argument misuse and clarify the public API. Decorators (@with_lock, @with_run) and behavior are otherwise unchanged.
Introduce explicit typing aliases for media transforms and migrate internal APIs to use them. New typing modules under swanlab/sdk/typings/run/transforms define Caption(s)Type and per-media Data/Rate/File/Mode/Size types. Update SwanLab public stubs and internal run API signatures (log_text/log_image/log_audio/log_video) to be keyword-only and accept the new typed aliases; add extra image/audio/video params (mode, file_type, size, sample_rate) and propagate caption typing. Refactor Transform classes (audio/image/text/video) to consume the typed aliases and validate/normalize inputs (image file_type check now uses typing.get_args and numpy→PIL mode conversion is handled). Tests updated to call keyword-only APIs, exercise caption broadcasting and new image/audio/video options, and to accept BytesIO video inputs.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request systematically refines the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
本次 PR 对 swanlab.log_* 系列 API 进行了全面的重构和优化,显著提升了代码的一致性、类型安全性和可维护性。主要亮点包括:
- API 规范化:将
log_*方法的参数统一为 keyword-only,避免了因参数位置混淆导致的问题,这是一个很好的实践。 - 类型系统重构:将媒体类型的定义抽离到独立的
typings模块,使得类型定义更集中、更清晰,也更易于维护。 - 功能补齐:为
log_image补全了mode,file_type,size等参数,增强了其灵活性。 - 易用性提升:新增了
swanlab.run懒加载属性,简化了获取当前 run 实例的操作,提升了用户体验。 - 测试增强:大幅扩充和完善了单元测试,覆盖了新功能和各种边界情况,为代码质量提供了坚实的保障。
总体而言,这是一次高质量的重构,代码结构清晰,改动考虑周全。我只发现了一个关于导入路径的小问题,修正后代码将更加完善。
Move and consolidate run transform typing imports to absolute module paths at the top of the file, adding Audio, Image, Text and Video typing imports and removing redundant relative imports. Cleans up import ordering and redundant lines for clearer module structure.
Background
本次 PR 对 swanlab.log_* 系列 API 进行了系统性整理,主要解决三个方面的问题:参数设计不一致、类型标注分散且不准确、测试覆盖不完整。
Changes
将各媒体类型的输入类型定义从实现代码中抽离,集中维护:
单数类型(XxxDataType)用于媒体类构造函数,复数类型(XxxDatasType)用于 log_* 方法,接受单个值或列表。
SwanLabRun.log_text/log_image/log_audio/log_video/log_scalar/define_scalar 所有参数(除 self)均改为 keyword-only(*),避免位置参数引起的歧义。
SwanLabRun.log_image 此前缺少 mode、file_type、size 三个参数,无法透传给 Image 构造函数,本次一并补齐。
AudioDatasType 未包含 np.ndarray 和 List[np.ndarray],与 Audio 实际接受的输入不符,已与 image.py 对齐。
通过模块 getattr 暴露顶层 swanlab.run,活跃时返回 SwanLabRun 实例,无活跃 run 时返回 None,无需显式调用 get_run()。
修复并扩充 test_log_media.py: