使用官方提供的 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
相关推荐
YOLO数据集集合28 分钟前
风机叶片表面损伤检测数据集 | 风机叶片 表面损伤 污渍检测 无人机巡检 风电运维9119期
运维·人工智能·计算机视觉·目标跟踪·无人机·智慧城市·电力巡检
j7~41 分钟前
【Linux网络编程】四十七.《Linux IO 模型详解:阻塞 IO、非阻塞 IO 与 IO 多路转接(select)》
linux·运维·网络·select·非阻塞io·阻塞io·i/o多路连接
分布式存储与RustFS1 小时前
用RustFS给Harbor当S3存储后端
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
泡海椒1 小时前
趋势分析报表:jquick-pdf折线图PDF生成实战
java·大数据·运维·服务器·前端·pdf
丸子君君1 小时前
【以麒麟服务器时间为准,定时请求时间同步】
运维·服务器·单片机
海盗12342 小时前
AI 新闻日报 2026-09-28:正确性审查自动化、7×24 个人智能体、具身部署态
运维·人工智能·机器人·自动化·人工智能aigc
元岳数字人小元2 小时前
数字人源码系统:模块化开发赋能智能场景灵活迭代升级
运维·人工智能·开源·人机交互·交互
事已至此先睡覺吧2 小时前
(三)Linux 基础指令(二):基础常用的一些指令
linux·运维·服务器
dghongcheng2 小时前
UV固化设备怎么选?UV固化炉选购指南
自动化·喷涂设备·喷涂·喷漆设备·自动喷涂线
Fcy6483 小时前
Linux下 传输层协议UDP详解(含底层源码解析)
linux·运维·udp