使用官方提供的 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
相关推荐
FlightYe5 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
橙橙笔记7 小时前
Linux基础与命令的学习
linux·运维·服务器·shell
无奈笑天下7 小时前
银河麒麟桌面操作系统【命令行配置文件与图片搜索功能】操作方法
linux·运维·服务器
飞花漫卷,情随岁迁8 小时前
网络数据包转发与内核机制:小白概念科普
linux·运维·网络·数据库·mysql·oracle·智能路由器
.道阻且长.9 小时前
Linux基础IO:理解文件以及回顾C文件接口
linux·运维·服务器·c语言·开发语言·职场和发展
goehou11 小时前
改错一行 fstab,SSH 就再也连不上了
linux·运维·故障恢复·fstab
贝锐12 小时前
远程运维如何实现网页触达:制造企业引入向日葵SDK重构机床运维实战
运维·sdk·web
无奈笑天下14 小时前
银河麒麟高级服务器操作系统【通过 sysctl 调整内核网络参数】操作方法
运维·服务器·网络
MSTcheng.16 小时前
【LinuxIO篇】Linux学习第二十三弹——Linux重定向到底改了什么?printf为何会写进文件,一文搞懂fd与dup2
linux·运维·服务器·操作系统·重定向·文件描述符