Skip to content

Feat/run#1506

Merged
SAKURA-CAT merged 4 commits intomainfrom
feat/run
Mar 17, 2026
Merged

Feat/run#1506
SAKURA-CAT merged 4 commits intomainfrom
feat/run

Conversation

@SAKURA-CAT
Copy link
Copy Markdown
Member

Background

本次 PR 对 swanlab.log_* 系列 API 进行了系统性整理,主要解决三个方面的问题:参数设计不一致、类型标注分散且不准确、测试覆盖不完整。


Changes

  1. 新增独立的 transform 类型模块 (swanlab/sdk/typings/run/transforms/)

将各媒体类型的输入类型定义从实现代码中抽离,集中维护:

  • text.py — TextDataType, TextDatasType
  • image.py — ImageDataType, ImageDatasType, ImageModeType/s, ImageFileType/s, ImageSizeType/s
  • audio.py — AudioDataType, AudioDatasType, AudioRateType/s
  • video.py — VideoDataType, VideoDatasType
  • init.py — CaptionType, CaptionsType

单数类型(XxxDataType)用于媒体类构造函数,复数类型(XxxDatasType)用于 log_* 方法,接受单个值或列表。

  1. log_* 方法参数改为 keyword-only

SwanLabRun.log_text/log_image/log_audio/log_video/log_scalar/define_scalar 所有参数(除 self)均改为 keyword-only(*),避免位置参数引起的歧义。

  1. log_image 补全缺失参数

SwanLabRun.log_image 此前缺少 mode、file_type、size 三个参数,无法透传给 Image 构造函数,本次一并补齐。

  1. 修复 AudioDatasType 类型缺失

AudioDatasType 未包含 np.ndarray 和 List[np.ndarray],与 Audio 实际接受的输入不符,已与 image.py 对齐。

  1. 更新 swanlab/init.pyi 类型存根
  • log_* 函数签名改为 keyword-only
  • 类型注解统一使用新 typings 模块中的别名(TextDatasType, ImageDatasType 等)替换原有的内联 Union[...]
  • log_image 补齐 mode、file_type、size
  • define_scalar 同步改为 keyword-only
  1. 新增 swanlab.run 懒加载属性

通过模块 getattr 暴露顶层 swanlab.run,活跃时返回 SwanLabRun 实例,无活跃 run 时返回 None,无需显式调用 get_run()。

  1. 测试完善

修复并扩充 test_log_media.py:

  • Bug fix:原测试以位置参数调用 keyword-only 方法,运行时会 TypeError,已改为关键字调用
  • 新增原始数据输入测试(str/ndarray/bytes 能被正确包装为媒体对象)
  • 新增 caption 行为测试:默认 None、列表 caption 逐项匹配、单个 caption 广播
  • 新增 log_image 专项测试:mode/file_type/size
  • 新增 log_audio 专项测试:sample_rate 透传
  • 新增 log_video 格式测试:bytes / BytesIO 输入
  • 新增 swanlab.run 语义的端到端测试

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.
@SAKURA-CAT SAKURA-CAT self-assigned this Mar 17, 2026
@SAKURA-CAT SAKURA-CAT added the 💪 enhancement New feature or request label Mar 17, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 swanlab.log_* API series by addressing inconsistencies in parameter design, improving type annotations, and expanding test coverage. The changes aim to enhance API clarity, robustness, and maintainability, particularly for media logging functions, while also introducing a convenient lazy-loading mechanism for the swanlab.run object.

Highlights

  • Refactored Media Typing: Introduced dedicated transform typing modules for Text, Image, Audio, and Video, centralizing type definitions and improving consistency across media logging APIs.
  • Keyword-Only Arguments: Converted all log_* and define_scalar methods to use keyword-only arguments, enhancing API clarity and preventing positional argument ambiguities.
  • Enhanced log_image Parameters: Added mode, file_type, and size parameters to log_image, allowing more control over image processing and matching the Image constructor's capabilities.
  • Improved Type Annotations: Corrected AudioDatasType to properly include np.ndarray and List[np.ndarray] and updated swanlab/__init__.pyi with new type aliases and keyword-only signatures.
  • Lazy Loading for swanlab.run: Implemented a lazy-loading mechanism for swanlab.run via __getattr__, providing direct access to the active run instance without explicit get_run() calls.
  • Expanded Test Coverage: Significantly improved test coverage for media logging, including tests for raw data input, various caption behaviors, and specific media parameters, along with new end-to-end tests for swanlab.run.

🧠 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
  • swanlab/init.py
    • Implemented __getattr__ for lazy loading of swanlab.run.
    • Added run to the __all__ export list.
  • swanlab/init.pyi
    • Imported new dedicated typing modules for media transforms.
    • Added run to the __all__ export list and provided its type hint.
    • Updated log_text, log_image, log_audio, log_video, and define_scalar function signatures to be keyword-only.
    • Replaced inline Union[...] type annotations with new type aliases.
    • Added mode, file_type, and size parameters to log_image's type signature.
  • swanlab/sdk/internal/run/init.py
    • Imported new typing modules for media transforms.
    • Modified log_scalar, log_text, log_image, log_audio, log_video, and define_scalar methods to accept keyword-only arguments.
    • Updated log_image to correctly pass mode, caption, file_type, and size to the normalize_media_input function.
  • swanlab/sdk/internal/run/transforms/audio/init.py
    • Refactored Audio constructor to use new dedicated type hints AudioDataType and CaptionType.
    • Removed redundant Union import.
  • swanlab/sdk/internal/run/transforms/image/init.py
    • Refactored Image constructor with new type hints ImageDataType, ImageModeType, CaptionType, ImageFileType, and ImageSizeType.
    • Improved file_type validation by using get_args from the new typing module.
    • Modified numpy array processing to apply mode conversion correctly.
  • swanlab/sdk/internal/run/transforms/text/init.py
    • Refactored Text constructor to use new dedicated type hints TextDataType and CaptionType.
    • Removed redundant Union import.
  • swanlab/sdk/internal/run/transforms/video/init.py
    • Refactored Video constructor to use new dedicated type hints VideoDataType and CaptionType.
    • Removed redundant Union import.
  • swanlab/sdk/typings/run/transforms/init.py
    • Added base type aliases CaptionType and CaptionsType.
  • swanlab/sdk/typings/run/transforms/audio.py
    • Defined specific type aliases for audio data (AudioDataType, AudioDatasType) and rates (AudioRateType, AudioRatesType).
  • swanlab/sdk/typings/run/transforms/image.py
    • Defined specific type aliases for image data (ImageDataType, ImageDatasType), modes (ImageModeType, ImageModesType), file types (ImageFileType, ImageFilesType), and sizes (ImageSizeType, ImageSizesType).
  • swanlab/sdk/typings/run/transforms/text.py
    • Defined specific type aliases for text data (TextDataType, TextDatasType).
  • swanlab/sdk/typings/run/transforms/video.py
    • Defined specific type aliases for video data (VideoDataType, VideoDatasType).
  • tests/unit/conftest.py
    • Updated the isolate_sdk_environment fixture to correctly handle the new swanlab.run lazy loading.
    • Imported the swanlab module.
  • tests/unit/sdk/internal/run/test_log_media.py
    • Imported BytesIO for video testing.
    • Refactored the test helper _call_step to _call_log to correctly handle keyword-only arguments.
    • Added new test cases for raw data wrapping, caption broadcasting, and specific parameters for log_image, log_audio, and log_video.
  • tests/unit/test_swanlab_run_e2e.py
    • Added a new test file to verify the end-to-end behavior of the swanlab.run lazy-loaded attribute.
Activity
  • No specific activity (comments, reviews, progress updates) has been recorded for this pull request since its creation.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 对 swanlab.log_* 系列 API 进行了全面的重构和优化,显著提升了代码的一致性、类型安全性和可维护性。主要亮点包括:

  1. API 规范化:将 log_* 方法的参数统一为 keyword-only,避免了因参数位置混淆导致的问题,这是一个很好的实践。
  2. 类型系统重构:将媒体类型的定义抽离到独立的 typings 模块,使得类型定义更集中、更清晰,也更易于维护。
  3. 功能补齐:为 log_image 补全了 mode, file_type, size 等参数,增强了其灵活性。
  4. 易用性提升:新增了 swanlab.run 懒加载属性,简化了获取当前 run 实例的操作,提升了用户体验。
  5. 测试增强:大幅扩充和完善了单元测试,覆盖了新功能和各种边界情况,为代码质量提供了坚实的保障。

总体而言,这是一次高质量的重构,代码结构清晰,改动考虑周全。我只发现了一个关于导入路径的小问题,修正后代码将更加完善。

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.
@SAKURA-CAT SAKURA-CAT merged commit 51ead50 into main Mar 17, 2026
18 checks passed
@SAKURA-CAT SAKURA-CAT deleted the feat/run branch March 17, 2026 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

💪 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant