使用官方提供的 bump-pydantic 工具 来自动化部分迁移pydantic代码

初始化Kotti_next 数据库报错kotti-next db init,于是进行pytest测试,发现很多warning信息,与pydantic代码相关。

复制代码
这些怎么解决?kotti_next\schemas\auth.py:42
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\schemas\auth.py:42: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class UserResponse(BaseModel):

kotti_next\schemas\node.py:35
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\schemas\node.py:35: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class NodeResponse(BaseModel):

kotti_next\routers\webhooks.py:42
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\routers\webhooks.py:42: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class WebhookResponse(BaseModel):

kotti_next\routers\webhooks.py:58
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\routers\webhooks.py:58: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class WebhookDeliveryResponse(BaseModel):

kotti_next\schemas\media.py:11
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\schemas\media.py:11: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class MediaFileResponse(BaseModel):

kotti_next\schemas\public.py:11
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\schemas\public.py:11: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class PublicNodeBase(BaseModel):

kotti_next\schemas\public.py:36
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\schemas\public.py:36: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.12/migration/
    class PublicTreeNode(BaseModel):

tests/test_acl.py: 68 warnings
tests/test_auth.py: 9 warnings
tests/test_media.py: 11 warnings
tests/test_nodes.py: 170 warnings
tests/test_public_api.py: 41 warnings
tests/test_users.py: 23 warnings
tests/test_webhooks.py: 25 warnings
  G:\workbuddy\.venv\Lib\site-packages\sqlalchemy\sql\schema.py:3624: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    return util.wrap_callable(lambda ctx: fn(), fn)  # type: ignore

tests/test_acl.py: 7 warnings
tests/test_auth.py: 8 warnings
tests/test_media.py: 7 warnings
tests/test_nodes.py: 10 warnings
tests/test_public_api.py: 8 warnings
tests/test_users.py: 16 warnings
tests/test_webhooks.py: 10 warnings
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\routers\auth.py:58: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    user.last_login = datetime.utcnow()

tests/test_acl.py: 7 warnings
tests/test_auth.py: 10 warnings
tests/test_media.py: 7 warnings
tests/test_nodes.py: 10 warnings
tests/test_public_api.py: 8 warnings
tests/test_users.py: 16 warnings
tests/test_webhooks.py: 10 warnings
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\auth\jwt.py:32: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    now = datetime.utcnow()

tests/test_acl.py: 7 warnings
tests/test_auth.py: 9 warnings
tests/test_media.py: 7 warnings
tests/test_nodes.py: 10 warnings
tests/test_public_api.py: 8 warnings
tests/test_users.py: 16 warnings
tests/test_webhooks.py: 10 warnings
  c:\Users\Admin\WorkBuddy\20260330150804\backend\kotti_next\auth\jwt.py:55: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    now = datetime.utcnow()

tests/test_acl.py: 10 warnings
tests/test_auth.py: 7 warnings
tests/test_media.py: 10 warnings

AI说:

要解决 kotti_next 代码中的 PydanticDeprecatedSince20 警告和 datetime.utcnow()DeprecationWarning,需要分别处理 Pydantic V2 的迁移和 UTC 时间的正确使用方式。

1. 解决 PydanticDeprecatedSince20 警告

警告信息如 Support for class-based config is deprecated, use ConfigDict instead 表明项目中使用了 Pydantic V1 风格的配置方式。根据 Pydantic V2 的迁移指南,你需要将旧的 Config 类替换为 model_config 字典配置方式。

2.解决 datetime.utcnow() DeprecationWarning

警告信息如 datetime.datetime.utcnow() is deprecated 表明项目中仍在使用被弃用的 utcnow() 方法。根据 Python 官方建议,应使用 datetime.now(datetime.UTC)datetime.now(timezone.utc) 来替代。

具体修改步骤‌:

  • ‌**替换 utcnow()now(timezone.utc)**‌:

    复制代码
    复制代码
    # 旧代码
    from datetime import datetime
    now = datetime.utcnow()
    
    # 新代码
    from datetime import datetime, timezone
    now = datetime.now(timezone.utc)
  • ‌**如果使用 Python 3.11+,可使用 datetime.UTC**‌:

    复制代码
    复制代码
    # Python 3.11+
    from datetime import datetime, UTC
    now = datetime.now(UTC)
  • sqlalchemy/schema.py:3624 中修改 ‌:

    找到该文件中使用 datetime.utcnow() 的地方,将其替换为 datetime.now(timezone.utc)datetime.now(UTC)

重点解决pydantic的问题

方法步骤如下:

复制代码
pip install bump-pydantic
cd /path/to/your/project
bump-pydantic your_package

对我的项目来说,路径在c:\Users\Admin\WorkBuddy\20260330150804>

执行命令为:

复制代码
cd c:\Users\Admin\WorkBuddy\20260330150804
bump-pydantic backend

执行结果:

复制代码
c:\Users\Admin\WorkBuddy\20260330150804>bump-pydantic backend
[22:20:57] Start bump-pydantic.                                                                              main.py:61
           Found 67 files to process.                                                                        main.py:78
[22:21:41] Refactored 5 files.                                                                              main.py:144
           Run successfully!                                                                                main.py:154

再执行测试,pydantic 的报错就没有了。

解决完之后,又让AI帮着解决了数据库初始化问题,现在数据库初始化ok

复制代码
(.venv) c:\Users\Admin\WorkBuddy\20260330150804\backend>kotti-next db init
→ Creating database tables...
  Admin user already exists, skipping
✓ Initialisation complete
相关推荐
杜子不疼.2 小时前
浏览器秒连服务器!WebSSH 实战体验,远程运维再也不折腾
运维·服务器·人工智能
RenPenry2 小时前
2026 在Linux上搭建CS2插件服务器
linux·运维·服务器·cs2·debian13
流年笙歌_2 小时前
(超详细)手把手教你安装银河麒麟高级服务器操作系统 V11
运维·服务器
微学AI2 小时前
code-server 体验:一行命令把 VS Code 跑在服务器上,随时用浏览器写代码
运维·服务器
嫂子开门我是_我哥2 小时前
一文讲透服务器:普通人也能看懂的服务器科普,网站为啥离不开它?
运维·服务器
qq_232888752 小时前
麒麟V10服务器安装搜狗输入法
运维·服务器
学亮编程手记2 小时前
一台服务器能支持的A800或H800 GPU最大数量分析
运维·服务器·人工智能
大灰狼来喽2 小时前
McPorter 实战:一键管理 OpenClaw 的 MCP 服务器
运维·服务器·人工智能·aigc·ai编程
灰子学技术2 小时前
Envoy 中 TCP 网络连接实现分析
运维·服务器·网络·网络协议·tcp/ip