Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature: 升级到 Pydantic v2 #74

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
687 changes: 400 additions & 287 deletions poetry.lock

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ Jinja2 = "^3.0.0"
unidiff = "^0.7.0"
humanize = "^4.0.0"
asyncpg = "^0.29.0"
pydantic = "^1.10.10"
pydantic = "^2.6.3"
playwright = "^1.17.2"
mdit-py-emoji = "^0.1.0"
nonebot-plugin-orm = "^0.6.2"
nonebot-adapter-qq = "^1.3.4"
nonebot-plugin-sentry = "^1.0.1"
nonebot-adapter-github = "^0.3.0"
nonebot-adapter-onebot = "^2.1.4"
nonebot-plugin-orm = "^0.7.0"
nonebot-adapter-qq = "^1.4.2"
nonebot-plugin-sentry = "^1.1.0"
nonebot-plugin-status = "^0.8.1"
nonebot-adapter-github = "^0.4.0"
nonebot-adapter-onebot = "^2.4.2"
redis = { version = "^5.0.0", extras = ["hiredis"] }
markdown-it-py = { version = "^3.0.0", extras = ["linkify", "plugins"] }
nonebot2 = { version = "^2.1.0", extras = ["httpx", "websockets", "fastapi"] }
nonebot2 = { version = "^2.2.0", extras = ["httpx", "websockets", "fastapi"] }

[tool.poetry.group.dev.dependencies]
ruff = "^0.2.0"
ruff = "^0.3.0"
isort = "^5.9.3"
black = "^24.0.0"
nonemoji = "^0.1.2"
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/github/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2020-09-20 23:59:20
@LastEditors : yanyongyu
@LastEditTime : 2023-11-29 14:27:38
@LastEditTime : 2024-03-05 14:43:47
@Description : GitHub Main Plugin
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,11 +12,12 @@
from pathlib import Path

import nonebot
from nonebot import get_plugin_config

from .config import Config

# load all github plugin config from global config
config = Config.parse_obj(nonebot.get_driver().config)
config = get_plugin_config(Config)

# load all github subplugins
_sub_plugins = set()
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/github/cache/message_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2022-09-13 15:56:01
@LastEditors : yanyongyu
@LastEditTime : 2023-10-06 16:01:09
@LastEditTime : 2024-03-05 14:48:21
@Description : Message tag cache
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,7 +12,7 @@
from datetime import timedelta
from typing import Literal, Annotated

from pydantic import Field, BaseModel, parse_raw_as
from pydantic import Field, BaseModel, TypeAdapter

from src.providers.redis import redis_client
from src.providers.platform import MessageInfo
Expand Down Expand Up @@ -100,4 +100,4 @@ async def get_message_tag(message: MessageInfo) -> Tag | None:
)
)
) is not None:
return parse_raw_as(Tag, data)
return TypeAdapter(Tag).validate_json(data)
6 changes: 3 additions & 3 deletions src/plugins/github/cache/user_auth_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2022-09-05 11:06:43
@LastEditors : yanyongyu
@LastEditTime : 2023-10-06 16:02:47
@LastEditTime : 2024-03-05 14:49:05
@Description : OAuth state cache
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,7 +12,7 @@
from uuid import uuid4
from datetime import timedelta

from pydantic import parse_raw_as
from pydantic import TypeAdapter

from src.providers.platform import UserInfo
from src.providers.redis import redis_client
Expand Down Expand Up @@ -49,7 +49,7 @@ async def get_state(state_id: str) -> UserInfo | None:
Existing state data
"""
if data := await redis_client.get(STATE_CACHE_KEY.format(state_id=state_id)):
return parse_raw_as(UserInfo, data)
return TypeAdapter(UserInfo).validate_json(data)


async def delete_state(state_id: str) -> None:
Expand Down
21 changes: 8 additions & 13 deletions src/plugins/github/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2020-09-21 19:05:28
@LastEditors : yanyongyu
@LastEditTime : 2023-03-30 20:36:55
@LastEditTime : 2024-03-05 14:46:53
@Description : Config for github plugin
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,7 +12,7 @@
from typing import Any, Literal

from nonebot.adapters.github.config import OAuthApp, GitHubApp
from pydantic import Extra, BaseModel, validator, parse_obj_as, root_validator
from pydantic import Field, BaseModel, TypeAdapter, model_validator


class GitHubAPP(GitHubApp):
Expand All @@ -22,14 +22,15 @@ class GitHubAPP(GitHubApp):
client_secret: str


class Config(BaseModel, extra=Extra.ignore):
class Config(BaseModel):
github_app: GitHubAPP
oauth_app: OAuthApp | None = None
github_theme: Literal["light", "dark"] = "light"
github_webhook_priority: int = 1
github_command_priority: int = 50
github_webhook_priority: int = Field(1, gt=0)
github_command_priority: int = Field(50, gt=0)

@root_validator(pre=True)
@model_validator(mode="before")
@classmethod
def validate_app(cls, values: dict[str, Any]) -> dict[str, Any]:
"""Auto get app from github adapter config"""

Expand All @@ -38,7 +39,7 @@ def validate_app(cls, values: dict[str, Any]) -> dict[str, Any]:
"A GitHub App must be provided to use the bot. "
"See https://github.com/nonebot/adapter-github for more information."
)
apps = parse_obj_as(list[GitHubApp | OAuthApp], apps)
apps = TypeAdapter(list[GitHubApp | OAuthApp]).validate_python(apps)
if not (
github_app := next(
(app for app in apps if isinstance(app, GitHubApp)), None
Expand All @@ -52,9 +53,3 @@ def validate_app(cls, values: dict[str, Any]) -> dict[str, Any]:
if oauth_app := next((app for app in apps if isinstance(app, OAuthApp)), None):
values.setdefault("oauth_app", oauth_app)
return values

@validator("github_webhook_priority", "github_command_priority")
def validate_priority(cls, v: int) -> int:
if v < 1:
raise ValueError("`priority` must be greater than 0")
return v
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:46
@LastEditors : yanyongyu
@LastEditTime : 2023-12-10 17:35:08
@LastEditTime : 2024-03-05 14:30:05
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,11 +12,11 @@
from typing import Annotated, TypeAlias

from nonebot import logger
from githubkit.rest import Commit
from nonebot.adapters import Event
from nonebot.params import Depends
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from githubkit.versions.latest.models import Commit
from nonebot.adapters.github import ActionFailed, ActionTimeout

from .github import GITHUB_PUBLIC_CONTEXT
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-08 16:14:49
@LastEditors : yanyongyu
@LastEditTime : 2023-12-10 17:44:59
@LastEditTime : 2024-03-05 14:30:20
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -15,7 +15,7 @@
from nonebot.params import Depends
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from githubkit.rest import Installation
from githubkit.versions.latest.models import Installation
from nonebot.adapters.github import ActionFailed, ActionTimeout

from src.plugins.github.utils import get_github_bot
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:46
@LastEditors : yanyongyu
@LastEditTime : 2023-10-07 17:16:46
@LastEditTime : 2024-03-05 14:30:43
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,10 +12,10 @@
from typing import Annotated, TypeAlias

from nonebot import logger
from githubkit.rest import Issue
from nonebot.params import Depends
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from githubkit.versions.latest.models import Issue
from nonebot.adapters.github import ActionFailed, ActionTimeout

from .github import GITHUB_PUBLIC_CONTEXT
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:46
@LastEditors : yanyongyu
@LastEditTime : 2023-11-27 13:05:13
@LastEditTime : 2024-03-05 14:31:05
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -12,11 +12,11 @@
from typing import Annotated, TypeAlias

from nonebot import logger
from githubkit.rest import Release
from nonebot.adapters import Event
from nonebot.params import Depends
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from githubkit.versions.latest.models import Release
from nonebot.adapters.github import ActionFailed, ActionTimeout

from .github import GITHUB_PUBLIC_CONTEXT
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:16:55
@LastEditors : yanyongyu
@LastEditTime : 2023-11-27 13:07:50
@LastEditTime : 2024-03-05 14:31:17
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -16,7 +16,7 @@
from nonebot.params import Depends
from nonebot.typing import T_State
from nonebot.matcher import Matcher
from githubkit.rest import FullRepository
from githubkit.versions.latest.models import FullRepository
from nonebot.adapters.github import ActionFailed, ActionTimeout

from .github import GITHUB_PUBLIC_CONTEXT
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/github/dependencies/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2023-10-07 17:17:06
@LastEditors : yanyongyu
@LastEditTime : 2023-11-25 19:23:32
@LastEditTime : 2024-03-05 14:31:44
@Description : None
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -14,8 +14,8 @@
from nonebot import logger
from nonebot.params import Depends
from nonebot.matcher import Matcher
from githubkit.rest import PublicUser, PrivateUser
from nonebot.adapters.github import ActionFailed, ActionTimeout
from githubkit.versions.latest.models import PublicUser, PrivateUser

from src.plugins.github.models import User
from src.providers.platform import USER_INFO
Expand Down
30 changes: 18 additions & 12 deletions src/plugins/github/libs/renderer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
@Author : yanyongyu
@Date : 2021-03-09 16:45:25
@LastEditors : yanyongyu
@LastEditTime : 2023-12-01 16:00:49
@LastEditTime : 2024-03-05 14:41:18
@Description : GitHub image renderer
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"


from githubkit import rest, webhooks
from githubkit.versions.latest import models
from nonebot.adapters.github import OAuthBot, GitHubBot

from src.plugins.github import config
Expand All @@ -28,7 +28,7 @@

async def readme_to_image(
bot: GitHubBot | OAuthBot,
repo: rest.FullRepository,
repo: models.FullRepository,
readme: str,
width: int = 800,
height: int = 300,
Expand All @@ -40,7 +40,7 @@ async def readme_to_image(

async def issue_to_image(
bot: GitHubBot | OAuthBot,
issue: rest.Issue,
issue: models.Issue,
highlight_comment: int | None = None,
width: int = 800,
height: int = 300,
Expand All @@ -52,7 +52,7 @@ async def issue_to_image(

async def pr_diff_to_image(
bot: GitHubBot | OAuthBot,
issue: rest.Issue,
issue: models.Issue,
width: int = 800,
height: int = 300,
) -> bytes:
Expand All @@ -63,8 +63,11 @@ async def pr_diff_to_image(

async def issue_opened_to_image(
bot: GitHubBot | OAuthBot,
repo: webhooks.Repository,
issue: webhooks.IssuesOpenedPropIssue | webhooks.PullRequestOpenedPropPullRequest,
repo: models.RepositoryWebhooks,
issue: (
models.WebhookIssuesOpenedPropIssue
| models.WebhookPullRequestOpenedPropPullRequest
),
width: int = 800,
height: int = 300,
) -> bytes:
Expand All @@ -75,9 +78,9 @@ async def issue_opened_to_image(

async def issue_commented_to_image(
bot: GitHubBot | OAuthBot,
repo: webhooks.Repository,
issue: webhooks.IssueCommentCreatedPropIssue,
comment: webhooks.IssueComment,
repo: models.RepositoryWebhooks,
issue: models.WebhookIssueCommentCreatedPropIssue,
comment: models.WebhookIssueCommentCreatedPropComment,
width: int = 800,
height: int = 300,
) -> bytes:
Expand All @@ -90,8 +93,11 @@ async def issue_commented_to_image(

async def issue_closed_to_image(
bot: GitHubBot | OAuthBot,
repo: webhooks.Repository,
issue: webhooks.IssuesClosedPropIssue | webhooks.PullRequestClosedPropPullRequest,
repo: models.RepositoryWebhooks,
issue: (
models.WebhookIssuesClosedPropIssue
| models.WebhookPullRequestClosedPropPullRequest
),
width: int = 800,
height: int = 300,
) -> bytes:
Expand Down
Loading
Loading