Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Compatible with pydantic 2.0 #1826

Merged
merged 9 commits into from
Jan 10, 2025
Merged

✨ Compatible with pydantic 2.0 #1826

merged 9 commits into from
Jan 10, 2025

Conversation

BalconyJH
Copy link
Collaborator

@BalconyJH BalconyJH commented Jan 7, 2025

✨ Compatible with pydantic 2.0

主要变更

此PR主要解决了 zhenxun_bot 代码库与 pydantic 2.0 的兼容性问题(Resolved #1825)。

依赖变动

  • 移除 pydantic 在项目层面的限制
  • 升级 nonebot2 从 2.4.0 到 2.4.1
  • 升级 nonebot-plugin-apscheduler 从 0.3.0 到 0.5.0
  • 移除多余的适配器依赖:
    • nonebot-adapter-discord
    • nonebot-adapter-dodo
    • nonebot-adapter-kaiheila
  • 移除 nonebot-plugin-userinfo 依赖

功能增强

  • 更新 register_use 函数,新增支持 send_success_msgmax_num_limit 的关键字参数
  • ShopParam 模型中添加 extra_data 字段,用于存储额外数据
  • 移除代码中未使用的导入,优化代码结构

技术改进

  • 将所有 pydantic 模型中的 dict() 方法调用更新为 to_dict(),以适配 pydantic 2.0 的新API
  • 更新相关测试用例,确保与 ShopParamregister_use 的变更保持一致

杂项

  • 更新 poetry 依赖组名称:从 tool.poetry.dev-dependencies 更新为 tool.poetry.group.dev.dependencies,以适配最新的 poetry 规范

标签

  • enhancement
  • dependencies
  • bugfix

Original description in English

✨ Compatible with pydantic 2.0

Major Changes

This PR addresses the compatibility issues between zhenxun_bot codebase and pydantic 2.0 (Resolved #1825).

Dependency Updates

  • Removed version constraint for pydantic at project level
  • Upgraded nonebot2 from 2.4.0 to 2.4.1
  • Upgraded nonebot-plugin-apscheduler from 0.3.0 to 0.5.0
  • Removed unnecessary adapter dependencies:
    • nonebot-adapter-discord
    • nonebot-adapter-dodo
    • nonebot-adapter-kaiheila
  • Removed nonebot-plugin-userinfo dependency

Enhancements

  • Updated register_use function to support keyword arguments for send_success_msg and max_num_limit
  • Added extra_data field to ShopParam model for storing additional data
  • Removed unused imports to optimize code structure

Technical Improvements

  • Updated all pydantic model dict() method calls to to_dict() to align with pydantic 2.0 API
  • Updated test fixtures and configurations:
    • Refactored test_check.py to use namedtuples for better test data organization
    • Updated conftest.py to remove unnecessary plugin dependencies
    • Modified test code to adapt to pydantic 2.0's model method changes

Miscellaneous

  • Updated poetry dependency group name from tool.poetry.dev-dependencies to tool.poetry.group.dev.dependencies to comply with latest poetry specification

Labels

  • enhancement
  • dependencies
  • bugfix

@BalconyJH BalconyJH added enhancement New feature or request dependencies Tracks issues related to external dependencies or library updates. labels Jan 7, 2025
Copy link
Contributor

sourcery-ai bot commented Jan 7, 2025

审阅者指南 by Sourcery

这个拉取请求将项目升级为与 pydantic 2.0 兼容。主要更改包括将 BaseModel.dict() 替换为 BaseModel.model_dump(),并在 ShopParam 模型中添加 Config 类,设置 arbitrary_types_allowed = True

显示更新后的 ShopParam 模型的类图

classDiagram
    class ShopParam {
        +text: str
        +session: Uninfo|None
        +message: UniMsg
        +send_success_msg: ClassVar[bool]
        +max_num_limit: ClassVar[int]
        +extra_data: ClassVar[dict[str, Any]]
        +to_dict(kwargs): dict
    }
    class Config {
        +arbitrary_types_allowed: bool
    }
    ShopParam --> Config
    note for ShopParam "更新为对静态字段使用 ClassVar
并为额外参数添加 extra_data"
Loading

显示 BaseModel 变更的类图

classDiagram
    class BaseModel {
        +to_dict(kwargs): dict
    }
    note for BaseModel "将 dict() 替换为 to_dict()
内部使用 model_dump()"

    class ConfigModel
    class ConfigGroup
    class BaseBlock
    class PluginExtraData

    BaseModel <|-- ConfigModel
    BaseModel <|-- ConfigGroup
    BaseModel <|-- BaseBlock
    BaseModel <|-- PluginExtraData
Loading

文件级别变更

变更 详情 文件
dict() 替换为 model_dump() 并为 ShopParam 添加 Config
  • param.dict() 调用替换为 param.to_dict()
  • ShopParam 添加 Config 类,设置 arbitrary_types_allowed = True
  • 修改传递给 create_model 函数的关键字参数
zhenxun/builtin_plugins/shop/_data_source.py
添加 to_dict 方法并更新使用情况
  • 为各种模型添加 to_dict 方法
  • dict() 的使用更新为 to_dict()
zhenxun/configs/utils/__init__.py
zhenxun/utils/github_utils/models.py
zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/model.py
zhenxun/builtin_plugins/plugin_store/models.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/models/model.py
zhenxun/builtin_plugins/web_ui/api/menu/model.py
zhenxun/builtin_plugins/web_ui/api/tabs/main/model.py
zhenxun/builtin_plugins/web_ui/api/tabs/manage/model.py
zhenxun/utils/echart_utils/models.py
更新类型注解并删除不必要的导入
  • 为各种变量和函数更新类型注解
  • 删除不必要的导入
zhenxun/models/group_info.py
zhenxun/builtin_plugins/hooks/_auth_checker.py
zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py
zhenxun/utils/_build_image.py
zhenxun/builtin_plugins/superuser/fg_manage.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/__init__.py
zhenxun/builtin_plugins/platform/__init__.py
zhenxun/builtin_plugins/web_ui/api/menu/__init__.py
zhenxun/builtin_plugins/web_ui/api/tabs/main/data_source.py
zhenxun/builtin_plugins/web_ui/api/tabs/plugin_manage/__init__.py
zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py
zhenxun/builtin_plugins/web_ui/base_model.py
zhenxun/models/user_console.py
zhenxun/builtin_plugins/check/data_source.py
更新依赖并删除未使用的适配器
  • 将 pydantic 版本更新到 2.0
  • 删除 nonebot-adapter-kaiheila、nonebot-adapter-discord 和 nonebot-adapter-dodo
pyproject.toml
scripts/bot_check.py
requirements.txt
更新插件元数据和示例插件
  • 在插件元数据中将 dict() 调用更新为 to_dict()
[大量文件列表,与原文相同]

针对关联问题的评估

问题 目标 已解决 说明
#1825 确保与 Pydantic 2.x 兼容

提示和命令

与 Sourcery 交互

  • 触发新的审阅: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub 问题: 通过回复审阅评论要求 Sourcery 创建问题。
  • 生成拉取请求标题: 在拉取请求标题的任何位置写 @sourcery-ai 以随时生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任何位置写 @sourcery-ai summary 以随时生成 PR 摘要。您还可以使用此命令指定摘要的插入位置。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审阅功能,如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、删除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request upgrades the project to be compatible with pydantic 2.0. The main changes include replacing BaseModel.dict() with BaseModel.model_dump() and adding a Config class to the ShopParam model with arbitrary_types_allowed = True.

Class diagram showing updated ShopParam model

classDiagram
    class ShopParam {
        +text: str
        +session: Uninfo|None
        +message: UniMsg
        +send_success_msg: ClassVar[bool]
        +max_num_limit: ClassVar[int]
        +extra_data: ClassVar[dict[str, Any]]
        +to_dict(kwargs): dict
    }
    class Config {
        +arbitrary_types_allowed: bool
    }
    ShopParam --> Config
    note for ShopParam "Updated to use ClassVar for static fields
and added extra_data for additional parameters"
Loading

Class diagram showing BaseModel changes

classDiagram
    class BaseModel {
        +to_dict(kwargs): dict
    }
    note for BaseModel "Replaced dict() with to_dict()
which uses model_dump() internally"

    class ConfigModel
    class ConfigGroup
    class BaseBlock
    class PluginExtraData

    BaseModel <|-- ConfigModel
    BaseModel <|-- ConfigGroup
    BaseModel <|-- BaseBlock
    BaseModel <|-- PluginExtraData
Loading

File-Level Changes

Change Details Files
Replace dict() with model_dump() and add a Config class to ShopParam
  • Replaced calls to param.dict() with param.to_dict()
  • Added a Config class to ShopParam with arbitrary_types_allowed = True
  • Modified how keyword arguments are passed to the create_model function
zhenxun/builtin_plugins/shop/_data_source.py
Add to_dict methods and update usages
  • Added to_dict methods to various models
  • Updated usages of dict() to to_dict()
zhenxun/configs/utils/__init__.py
zhenxun/utils/github_utils/models.py
zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/model.py
zhenxun/builtin_plugins/plugin_store/models.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/models/model.py
zhenxun/builtin_plugins/web_ui/api/menu/model.py
zhenxun/builtin_plugins/web_ui/api/tabs/main/model.py
zhenxun/builtin_plugins/web_ui/api/tabs/manage/model.py
zhenxun/utils/echart_utils/models.py
Update type annotations and remove unnecessary imports
  • Updated type annotations for various variables and functions
  • Removed unnecessary imports
zhenxun/models/group_info.py
zhenxun/builtin_plugins/hooks/_auth_checker.py
zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py
zhenxun/utils/_build_image.py
zhenxun/builtin_plugins/superuser/fg_manage.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/__init__.py
zhenxun/builtin_plugins/platform/__init__.py
zhenxun/builtin_plugins/web_ui/api/menu/__init__.py
zhenxun/builtin_plugins/web_ui/api/tabs/main/data_source.py
zhenxun/builtin_plugins/web_ui/api/tabs/plugin_manage/__init__.py
zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py
zhenxun/builtin_plugins/web_ui/base_model.py
zhenxun/models/user_console.py
zhenxun/builtin_plugins/check/data_source.py
Update dependencies and remove unused adapters
  • Updated pydantic version to 2.0
  • Removed nonebot-adapter-kaiheila, nonebot-adapter-discord, and nonebot-adapter-dodo
pyproject.toml
scripts/bot_check.py
requirements.txt
Update plugin metadata and example plugins
  • Updated calls to dict() with to_dict() in plugin metadata
tests/content/plugin_store/bilibili_sub.py
tests/content/plugin_store/github_sub.py
tests/content/plugin_store/jitang.py
tests/content/plugin_store/search_image.py
zhenxun/builtin_plugins/about.py
zhenxun/builtin_plugins/admin/admin_help/__init__.py
zhenxun/builtin_plugins/admin/admin_watch.py
zhenxun/builtin_plugins/admin/ban/__init__.py
zhenxun/builtin_plugins/admin/group_member_update/__init__.py
zhenxun/builtin_plugins/admin/group_update.py
zhenxun/builtin_plugins/admin/plugin_switch/__init__.py
zhenxun/builtin_plugins/admin/welcome_message/__init__.py
zhenxun/builtin_plugins/admin/welcome_message/data_source.py
zhenxun/builtin_plugins/auto_update/__init__.py
zhenxun/builtin_plugins/chat_history/chat_message.py
zhenxun/builtin_plugins/chat_history/chat_message_handle.py
zhenxun/builtin_plugins/check/__init__.py
zhenxun/builtin_plugins/help/__init__.py
zhenxun/builtin_plugins/help_help.py
zhenxun/builtin_plugins/info/__init__.py
zhenxun/builtin_plugins/init/manager.py
zhenxun/builtin_plugins/nickname.py
zhenxun/builtin_plugins/platform/qq/group_handle/__init__.py
zhenxun/builtin_plugins/plugin_store/__init__.py
zhenxun/builtin_plugins/record_request.py
zhenxun/builtin_plugins/restart/__init__.py
zhenxun/builtin_plugins/scheduler/morning.py
zhenxun/builtin_plugins/sign_in/__init__.py
zhenxun/builtin_plugins/statistics/statistics_handle.py
zhenxun/builtin_plugins/statistics/statistics_hook.py
zhenxun/builtin_plugins/superuser/broadcast/__init__.py
zhenxun/builtin_plugins/superuser/clear_data.py
zhenxun/builtin_plugins/superuser/exec_sql.py
zhenxun/builtin_plugins/superuser/group_manage.py
zhenxun/builtin_plugins/superuser/reload_setting.py
zhenxun/builtin_plugins/superuser/request_manage.py
zhenxun/builtin_plugins/superuser/set_admin.py
zhenxun/builtin_plugins/superuser/super_help/__init__.py
zhenxun/builtin_plugins/superuser/update_fg_info.py
zhenxun/builtin_plugins/web_ui/__init__.py
zhenxun/builtin_plugins/web_ui/api/menu/data_source.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/models/sql_log.py
zhenxun/builtin_plugins/web_ui/api/tabs/manage/chat.py
zhenxun/builtin_plugins/web_ui/api/tabs/manage/data_source.py
zhenxun/builtin_plugins/web_ui/api/tabs/system/model.py
zhenxun/builtin_plugins/withdraw.py

Assessment against linked issues

Issue Objective Addressed Explanation
#1825 Ensure compatibility with Pydantic 2.x

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

@BalconyJH - 我已经审查了你的更改,看起来非常棒!

以下是我在审查期间查看的内容
  • 🟡 一般性问题:发现1个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源项目是免费的 - 如果你喜欢我们的评论,请考虑分享 ✨
帮助我变得更有用!请在每条评论上点击 👍 或 👎,我将使用这些反馈来改进你的评论。
Original comment in English

Hey @BalconyJH - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -91,7 +96,7 @@ class ConfigGroup(BaseModel):
"""模块名"""
name: str | None = None
"""插件名"""
configs: dict[str, ConfigModel] = {}
configs: dict[str, ConfigModel] = defaultdict()
Copy link
Contributor

Choose a reason for hiding this comment

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

问题(bug风险): 没有default_factory参数的defaultdict()将引发TypeError

Original comment in English

issue (bug_risk): defaultdict() without a default_factory parameter will raise TypeError

@BalconyJH BalconyJH force-pushed the feature/migrate_pydantic2 branch 5 times, most recently from 30b00b7 to 87756c5 Compare January 10, 2025 03:28
@BalconyJH BalconyJH force-pushed the feature/migrate_pydantic2 branch from 87756c5 to 15fd196 Compare January 10, 2025 03:32
@BalconyJH BalconyJH added bug Something isn't working ci Issues related to automated workflows, build pipelines, and CI tools. and removed ci Issues related to automated workflows, build pipelines, and CI tools. labels Jan 10, 2025
@BalconyJH BalconyJH merged commit dc143c0 into main Jan 10, 2025
3 checks passed
@BalconyJH BalconyJH deleted the feature/migrate_pydantic2 branch January 10, 2025 04:19
@BalconyJH BalconyJH removed the bug Something isn't working label Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Tracks issues related to external dependencies or library updates. enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

兼容 Pydantic 2.x
2 participants