Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

🐛 使用 pydantic 兼容性写法 #512

Merged
merged 3 commits into from
Mar 29, 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
61 changes: 61 additions & 0 deletions .github/workflows/pyd1-compat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: pydantic1-compat-test

on:
push:
branches:
- main
- next
paths:
- admin-frontend/**
- docker/**
- nonebot_bison/**
- tests/**
- pyproject.toml
- poetry.lock
- docker.env.prod
- .github/**
pull_request:
paths:
- admin-frontend/**
- docker/**
- nonebot_bison/**
- tests/**
- pyproject.toml
- poetry.lock
- docker.env.prod
- .github/**
types:
- opened
- synchronize
- reopened

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: pydantic1 test
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.11"]
os: [ubuntu-latest]
fail-fast: false
env:
OS: ${{ matrix.os }}
PYTHON_VERSION: ${{ matrix.python-version }}

steps:
- uses: actions/checkout@v4

- name: Setup Python environment
uses: ./.github/actions/setup-python
with:
python-version: ${{ matrix.python-version }}

- name: Install prerequisites
run: poetry add pydantic@^1.10 && poetry install

- name: Run Pytest
run: poetry run pytest -k 'not compare and not render' -n auto
9 changes: 3 additions & 6 deletions nonebot_bison/platform/arknights.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Any
from functools import partial

from yarl import URL
from httpx import AsyncClient
from bs4 import BeautifulSoup as bs
from pydantic import Field, AnyUrl, BaseModel
from pydantic import Field, BaseModel
from nonebot.compat import type_validate_python

from ..post import Post
Expand Down Expand Up @@ -113,11 +114,7 @@ def title_escape(text: str) -> str:
title=title,
nickname="明日方舟游戏内公告",
images=[data.banner_image_url] if data.banner_image_url else None,
url=(
url.unicode_string()
if data.jump_link and (url := AnyUrl(data.jump_link)).scheme.startswith("http")
else None
),
url=(url.human_repr() if (url := URL(data.jump_link)).scheme.startswith("http") else None),
timestamp=data.updated_at,
compress=True,
)
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ target-version = "py310"
[tool.black]
line-length = 120
preview = true
target-version = ["py310", "py311"]
target-version = ["py310", "py311", "py312"]
include = '\.pyi?$'
extend-exclude = '''
'''
Expand Down Expand Up @@ -139,4 +139,4 @@ defineConstant = { PYDANTIC_V2 = true }
[[tool.poetry.source]]
name = "offical-source"
url = "https://pypi.org/simple/"
priority = "primary"
priority = "primary"
30 changes: 17 additions & 13 deletions tests/platforms/test_arknights.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from nonebug.app import App
from httpx import Response, AsyncClient
from nonebot.compat import model_dump, type_validate_python

from .utils import get_file, get_json

Expand Down Expand Up @@ -51,18 +52,21 @@ async def test_url_parse(app: App):
cid_router = respx.get("https://ak-webview.hypergryph.com/api/game/bulletin/1")

def make_bulletin_obj(jump_link: str):
return BulletinData.model_validate({
"cid": "1",
"displayType": 1,
"title": "title",
"category": 1,
"header": "header",
"content": "content",
"jumpLink": jump_link,
"bannerImageUrl": "https://www.baidu.com",
"displayTime": "2021-08-01",
"updatedAt": 1627795200,
})
return type_validate_python(
BulletinData,
{
"cid": "1",
"displayType": 1,
"title": "title",
"category": 1,
"header": "header",
"content": "content",
"jumpLink": jump_link,
"bannerImageUrl": "https://www.baidu.com",
"displayTime": "2021-08-01",
"updatedAt": 1627795200,
},
)

def make_bulletin_list_item_obj():
return BulletinListItem(
Expand All @@ -75,7 +79,7 @@ def make_bulletin_list_item_obj():
)

def make_response(b: BulletinData):
return Response(200, json=ArkBulletinResponse(code=0, msg="", data=b).model_dump(by_alias=True))
return Response(200, json=model_dump(ArkBulletinResponse(code=0, msg="", data=b), by_alias=True))

b1 = make_bulletin_obj("")
assert b1.jump_link == ""
Expand Down
Loading