初始化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