Skip to content

Commit fad352f

Browse files
authored
feat!: 适配 Pydantic V2 (#107)
1 parent a3dce6f commit fad352f

File tree

8 files changed

+700
-990
lines changed

8 files changed

+700
-990
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ dmypy.json
145145
!.vscode/launch.json
146146

147147
data/
148+
bot.py

.vscode/launch.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@
66
"configurations": [
77
{
88
"name": "ORM Upgrade",
9-
"type": "python",
9+
"type": "debugpy",
1010
"request": "launch",
11-
"module": "nb-cli",
11+
"program": "${workspaceFolder}/bot.py",
1212
"args": ["orm", "upgrade"],
1313
"justMyCode": true
1414
},
1515
{
1616
"name": "Run",
17-
"type": "python",
17+
"type": "debugpy",
1818
"request": "launch",
19-
"module": "nb_cli",
19+
"program": "${workspaceFolder}/bot.py",
2020
"args": ["run", "--reload"],
2121
"justMyCode": false
2222
},
2323
{
2424
"name": "Pytest",
25-
"type": "python",
25+
"type": "debugpy",
2626
"request": "launch",
2727
"module": "pytest"
2828
},
2929
{
3030
"name": "Debug Unit Test",
31-
"type": "python",
31+
"type": "debugpy",
3232
"request": "test",
3333
"justMyCode": false
3434
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- 适配 Pydantic V2
13+
1014
## [0.1.2] - 2023-11-21
1115

1216
### Fixed

nonebot_plugin_user/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from nonebot import get_driver
1+
from nonebot import get_plugin_config
22
from pydantic import BaseModel
33

44

@@ -7,4 +7,4 @@ class Config(BaseModel):
77
"""生成令牌的前缀"""
88

99

10-
plugin_config = Config.parse_obj(get_driver().config)
10+
plugin_config = get_plugin_config(Config)

nonebot_plugin_user/models.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from dataclasses import dataclass
21
from datetime import datetime
32

3+
from nonebot.compat import PYDANTIC_V2, ConfigDict
44
from nonebot_plugin_orm import Model
55
from nonebot_plugin_session import Session, SessionIdType, SessionLevel
6+
from pydantic import BaseModel
67
from sqlalchemy import DateTime, String
78
from sqlalchemy.orm import Mapped, mapped_column
89

@@ -24,8 +25,14 @@ class Bind(Model):
2425
"""初始时绑定的账号 ID"""
2526

2627

27-
@dataclass
28-
class UserSession:
28+
class UserSession(BaseModel):
29+
if PYDANTIC_V2:
30+
model_config = ConfigDict(arbitrary_types_allowed=True)
31+
else:
32+
33+
class Config: # pragma: no cover
34+
arbitrary_types_allowed = True
35+
2936
session: Session
3037
user: User
3138

nonebot_plugin_user/params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ async def get_user_session(session: Session = Depends(extract_session)):
2626
"""获取用户会话"""
2727
user = await get_user(session)
2828
if user:
29-
return UserSession(session, user)
29+
return UserSession(session=session, user=user)

poetry.lock

Lines changed: 666 additions & 966 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@ documentation = "https://github.com/he0119/nonebot-plugin-user#readme"
1111

1212
[tool.poetry.dependencies]
1313
python = "^3.8"
14-
nonebot2 = "^2.1.0"
15-
nonebot-plugin-orm = ">=0.3.0"
16-
nonebot-plugin-alconna = ">=0.24.0"
17-
nonebot-plugin-session = "^0.2.0"
14+
nonebot2 = "^2.2.0"
15+
nonebot-plugin-orm = ">=0.7.0"
16+
nonebot-plugin-alconna = ">=0.37.1"
17+
nonebot-plugin-session = "^0.3.0"
1818
expiringdict = "^1.2.2"
1919

2020
[tool.poetry.group.dev.dependencies]
21-
nb-cli = "^1.0.4"
22-
nonebot-plugin-orm = { extras = ["default"], version = ">=0.3.0" }
21+
nonebot-plugin-orm = { extras = ["default"], version = ">=0.7.0" }
2322
nonebot-adapter-onebot = ">=2.2.3"
24-
nonebot-adapter-red = ">=0.6.1"
25-
nonebot-adapter-qq = "^1.1.1"
26-
nonebot-adapter-satori = "^0.8.0"
27-
nonebot2 = { extras = ["fastapi", "httpx", "websockets"], version = "^2.1.2" }
23+
nonebot-adapter-qq = ">=1.1.1"
24+
nonebot-adapter-satori = ">=0.8.0"
25+
nonebot2 = { extras = ["fastapi", "httpx", "websockets"], version = "^2.2.0" }
2826

2927
[tool.poetry.group.test.dependencies]
3028
nonebug = "^0.3.1"
3129
pytest-cov = "^4.0.0"
3230
pytest-xdist = "^3.0.2"
3331
pytest-mock = "^3.10.0"
34-
pytest-asyncio = ">=0.20.2,<0.22.0"
32+
pytest-asyncio = "^0.23.5"
3533
freezegun = "^1.2.2"
3634

3735
[tool.black]
@@ -49,6 +47,7 @@ asyncio_mode = "auto"
4947
pythonVersion = "3.8"
5048
pythonPlatform = "All"
5149
typeCheckingMode = "basic"
50+
defineConstant = { PYDANTIC_V2 = true }
5251

5352
[tool.ruff]
5453
select = ["E", "W", "F", "UP", "C", "T", "PYI", "Q"]
@@ -58,7 +57,6 @@ ignore = ["E402", "E501", "C901", "UP037"]
5857
adapters = [
5958
{ name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" },
6059
{ name = "OneBot V12", module_name = "nonebot.adapters.onebot.v12" },
61-
{ name = "RedProtocol", module_name = "nonebot.adapters.red" },
6260
{ name = "QQ", module_name = "nonebot.adapters.qq" },
6361
{ name = "Satori", module_name = "nonebot.adapters.satori" },
6462
]

0 commit comments

Comments
 (0)