Skip to content

Commit

Permalink
feat!: 适配 Pydantic V2 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 authored Feb 24, 2024
1 parent a3dce6f commit fad352f
Show file tree
Hide file tree
Showing 8 changed files with 700 additions and 990 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ dmypy.json
!.vscode/launch.json

data/
bot.py
12 changes: 6 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
"configurations": [
{
"name": "ORM Upgrade",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "nb-cli",
"program": "${workspaceFolder}/bot.py",
"args": ["orm", "upgrade"],
"justMyCode": true
},
{
"name": "Run",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "nb_cli",
"program": "${workspaceFolder}/bot.py",
"args": ["run", "--reload"],
"justMyCode": false
},
{
"name": "Pytest",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "pytest"
},
{
"name": "Debug Unit Test",
"type": "python",
"type": "debugpy",
"request": "test",
"justMyCode": false
}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Added

- 适配 Pydantic V2

## [0.1.2] - 2023-11-21

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_user/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nonebot import get_driver
from nonebot import get_plugin_config
from pydantic import BaseModel


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


plugin_config = Config.parse_obj(get_driver().config)
plugin_config = get_plugin_config(Config)
13 changes: 10 additions & 3 deletions nonebot_plugin_user/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from dataclasses import dataclass
from datetime import datetime

from nonebot.compat import PYDANTIC_V2, ConfigDict
from nonebot_plugin_orm import Model
from nonebot_plugin_session import Session, SessionIdType, SessionLevel
from pydantic import BaseModel
from sqlalchemy import DateTime, String
from sqlalchemy.orm import Mapped, mapped_column

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


@dataclass
class UserSession:
class UserSession(BaseModel):
if PYDANTIC_V2:
model_config = ConfigDict(arbitrary_types_allowed=True)
else:

class Config: # pragma: no cover
arbitrary_types_allowed = True

session: Session
user: User

Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_user/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ async def get_user_session(session: Session = Depends(extract_session)):
"""获取用户会话"""
user = await get_user(session)
if user:
return UserSession(session, user)
return UserSession(session=session, user=user)
1,632 changes: 666 additions & 966 deletions poetry.lock

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@ documentation = "https://github.com/he0119/nonebot-plugin-user#readme"

[tool.poetry.dependencies]
python = "^3.8"
nonebot2 = "^2.1.0"
nonebot-plugin-orm = ">=0.3.0"
nonebot-plugin-alconna = ">=0.24.0"
nonebot-plugin-session = "^0.2.0"
nonebot2 = "^2.2.0"
nonebot-plugin-orm = ">=0.7.0"
nonebot-plugin-alconna = ">=0.37.1"
nonebot-plugin-session = "^0.3.0"
expiringdict = "^1.2.2"

[tool.poetry.group.dev.dependencies]
nb-cli = "^1.0.4"
nonebot-plugin-orm = { extras = ["default"], version = ">=0.3.0" }
nonebot-plugin-orm = { extras = ["default"], version = ">=0.7.0" }
nonebot-adapter-onebot = ">=2.2.3"
nonebot-adapter-red = ">=0.6.1"
nonebot-adapter-qq = "^1.1.1"
nonebot-adapter-satori = "^0.8.0"
nonebot2 = { extras = ["fastapi", "httpx", "websockets"], version = "^2.1.2" }
nonebot-adapter-qq = ">=1.1.1"
nonebot-adapter-satori = ">=0.8.0"
nonebot2 = { extras = ["fastapi", "httpx", "websockets"], version = "^2.2.0" }

[tool.poetry.group.test.dependencies]
nonebug = "^0.3.1"
pytest-cov = "^4.0.0"
pytest-xdist = "^3.0.2"
pytest-mock = "^3.10.0"
pytest-asyncio = ">=0.20.2,<0.22.0"
pytest-asyncio = "^0.23.5"
freezegun = "^1.2.2"

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

[tool.ruff]
select = ["E", "W", "F", "UP", "C", "T", "PYI", "Q"]
Expand All @@ -58,7 +57,6 @@ ignore = ["E402", "E501", "C901", "UP037"]
adapters = [
{ name = "OneBot V11", module_name = "nonebot.adapters.onebot.v11" },
{ name = "OneBot V12", module_name = "nonebot.adapters.onebot.v12" },
{ name = "RedProtocol", module_name = "nonebot.adapters.red" },
{ name = "QQ", module_name = "nonebot.adapters.qq" },
{ name = "Satori", module_name = "nonebot.adapters.satori" },
]
Expand Down

0 comments on commit fad352f

Please sign in to comment.