⬆️ upgrade Pydantic to V2

This commit is contained in:
omg-xtao 2024-11-30 22:11:02 +08:00 committed by GitHub
parent 9bf0c60c3b
commit 839d23972e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 582 additions and 455 deletions

View File

@ -1,7 +1,6 @@
import datetime
from typing import List
from pytz import timezone
from simnet.models.zzz.diary import ZZZDiary
from simnet.models.zzz.chronicle.challenge import ZZZChallenge
@ -26,14 +25,6 @@ __all__ = (
"HistoryDataLedgerServices",
)
TZ = timezone("Asia/Shanghai")
def json_encoder(value):
if isinstance(value, datetime.datetime):
return value.astimezone(TZ).strftime("%Y-%m-%d %H:%M:%S")
return value
class HistoryDataAbyssServices(BaseService, HistoryDataBaseServices):
DATA_TYPE = HistoryDataTypeEnum.ABYSS.value
@ -46,7 +37,7 @@ class HistoryDataAbyssServices(BaseService, HistoryDataBaseServices):
@staticmethod
def create(user_id: int, abyss_data: ZZZChallenge):
data = HistoryDataAbyss(abyss_data=abyss_data)
json_data = data.json(by_alias=True, encoder=json_encoder)
json_data = data.model_dump_json(by_alias=True)
return HistoryData(
user_id=user_id,
data_id=abyss_data.season,
@ -62,7 +53,7 @@ class HistoryDataLedgerServices(BaseService, HistoryDataBaseServices):
@staticmethod
def create(user_id: int, diary_data: ZZZDiary):
data = HistoryDataLedger(diary_data=diary_data)
json_data = data.json(by_alias=True, encoder=json_encoder)
json_data = data.model_dump_json(by_alias=True)
return HistoryData(
user_id=user_id,
data_id=diary_data.data_id,

View File

@ -51,7 +51,7 @@ class WeaponEntry(BaseEntry):
class WeaponsEntry(BaseModel):
data: Optional[List[WeaponEntry]]
data: Optional[List[WeaponEntry]] = None
class StrategyEntry(BaseEntry):
@ -70,4 +70,4 @@ class StrategyEntry(BaseEntry):
class StrategyEntryList(BaseModel):
data: Optional[List[StrategyEntry]]
data: Optional[List[StrategyEntry]] = None

@ -1 +1 @@
Subproject commit 112b2e92d8492df17dbae23024fa805e3510a56e
Subproject commit bf5b153001defd150d4dcc17b9baf06a3769adf0

View File

@ -1,6 +1,6 @@
from typing import List, Optional, Any
from pydantic import BaseModel, validator
from pydantic import field_validator, BaseModel
__all__ = ("Member", "TeamRate", "FullTeamRate", "TeamRateResult")
@ -14,9 +14,10 @@ class Member(BaseModel):
class TeamRate(BaseModel):
rate: float
formation: List[Member]
owner_num: Optional[int]
owner_num: Optional[int] = None
@validator("rate", pre=True)
@field_validator("rate", mode="before")
@classmethod
def str2float(cls, v): # pylint: disable=R0201
return float(v.replace("%", "")) / 100.0 if isinstance(v, str) else v
@ -24,8 +25,8 @@ class TeamRate(BaseModel):
class FullTeamRate(BaseModel):
up: TeamRate
down: TeamRate
owner_num: Optional[int]
nice: Optional[float]
owner_num: Optional[int] = None
nice: Optional[float] = None
@property
def rate(self) -> float:

View File

@ -90,7 +90,7 @@ class AkashaLeaderboardArtifactSet(BaseModel):
class AkashaLeaderboardOwner(BaseModel):
nickname: str
adventureRank: float
profilePicture: Any
profilePicture: Any = None
nameCard: str
patreon: Dict[str, Any]
region: str
@ -121,7 +121,7 @@ class AkashaLeaderboardStats(BaseModel):
healingBonus: AkashaLeaderboardStatsValue
critRate: AkashaLeaderboardStatsValue
critDamage: AkashaLeaderboardStatsValue
electroDamageBonus: Optional[AkashaLeaderboardStatsValue]
electroDamageBonus: Optional[AkashaLeaderboardStatsValue] = None
class AkashaLeaderboardWeaponInfo(BaseModel):

View File

@ -1,6 +1,6 @@
from datetime import datetime
from pydantic import BaseModel, validator
from pydantic import field_validator, BaseModel
__all__ = ("GachaInfo",)
@ -12,6 +12,7 @@ class GachaInfo(BaseModel):
gacha_name: str
gacha_type: int
@validator("begin_time", "end_time", pre=True, allow_reuse=True)
@field_validator("begin_time", "end_time", mode="before")
@classmethod
def validate_time(cls, v):
return datetime.strptime(v, "%Y-%m-%d %H:%M:%S")

View File

@ -2,7 +2,7 @@ import datetime
from enum import Enum
from typing import Any, Dict, List, Union
from pydantic import BaseModel, validator
from pydantic import field_validator, BaseModel
from metadata.shortname import not_real_roles, roleToId, weaponToId, buddyToId
from modules.gacha_log.const import ZZZGF_VERSION
@ -42,26 +42,30 @@ class GachaItem(BaseModel):
rank_type: str
time: datetime.datetime
@validator("name")
@field_validator("name")
@classmethod
def name_validator(cls, v):
if item_id := (roleToId(v) or weaponToId(v) or buddyToId(v)):
if item_id not in not_real_roles:
return v
raise ValueError(f"Invalid name {v}")
@validator("gacha_type")
@field_validator("gacha_type")
@classmethod
def check_gacha_type(cls, v):
if v not in {"1", "2", "3", "5"}:
raise ValueError(f"gacha_type must be 1, 2, 3 or 5, invalid value: {v}")
return v
@validator("item_type")
@field_validator("item_type")
@classmethod
def check_item_type(cls, item):
if item not in {"代理人", "音擎", "邦布"}:
raise ValueError(f"error item type {item}")
return item
@validator("rank_type")
@field_validator("rank_type")
@classmethod
def check_rank_type(cls, rank):
if rank not in {"5", "4", "3"}:
raise ValueError(f"error rank type {rank}")

View File

@ -7,7 +7,7 @@ from httpx import HTTPError
from telegram import InlineKeyboardMarkup, InlineKeyboardButton
from telegram.helpers import create_deep_linked_url
from gram_core.basemodel import Settings
from gram_core.basemodel import Settings, SettingsConfigDict
from modules.gacha_log.error import GachaLogWebNotConfigError, GachaLogWebUploadError, GachaLogNotFound
@ -17,8 +17,7 @@ class GachaLogWebConfig(Settings):
url: Optional[str] = ""
token: Optional[str] = ""
class Config(Settings.Config):
env_prefix = "gacha_log_web_"
model_config = SettingsConfigDict(env_prefix="gacha_log_web_")
gacha_log_web_config = GachaLogWebConfig()

View File

@ -14,8 +14,8 @@ class GenshinBannerType(Enum):
class GachaBanner(BaseModel):
weight4 = ((1, 510), (8, 510), (10, 10000))
weight5 = ((1, 60), (73, 60), (90, 10000))
weight4: tuple[tuple[int, int]] = ((1, 510), (8, 510), (10, 10000))
weight5: tuple[tuple[int, int]] = ((1, 60), (73, 60), (90, 10000))
fallback_items3: List[int] = [
11301,
11302,

View File

@ -25,7 +25,7 @@ from telegram.helpers import escape_markdown
from core.config import config
from core.plugin import Plugin, conversation, handler
from gram_core.basemodel import Settings
from gram_core.basemodel import Settings, SettingsConfigDict
from gram_core.dependence.redisdb import RedisDB
from modules.apihelper.client.components.hoyolab import Hoyolab
from modules.apihelper.client.components.hyperion import Hyperion, HyperionBase
@ -54,8 +54,7 @@ class PostConfig(Settings):
chat_id: Optional[int] = 0
class Config(Settings.Config):
env_prefix = "post_"
model_config = SettingsConfigDict(env_prefix="post_")
CHECK_POST, SEND_POST, CHECK_COMMAND, GTE_DELETE_PHOTO = range(10900, 10904)

View File

@ -11,7 +11,7 @@ from utils.log import logger
class WebAppData(BaseModel):
path: str
data: Optional[dict]
data: Optional[dict] = None
code: int
message: str

View File

@ -2,7 +2,7 @@ import base64
from datetime import datetime
from typing import TYPE_CHECKING, List, Optional, Union
from pydantic import BaseModel, validator
from pydantic import field_validator, BaseModel
from simnet.errors import BadRequest as SimnetBadRequest, InvalidCookies, TimedOut as SimnetTimedOut
from simnet.models.zzz.chronicle.notes import ZZZNoteVhsSaleState
from sqlalchemy.orm.exc import StaleDataError
@ -30,7 +30,8 @@ class TaskDataBase(BaseModel):
class ResinData(TaskDataBase):
notice_num: Optional[int] = 140
@validator("notice_num")
@field_validator("notice_num")
@classmethod
def notice_num_validator(cls, v):
if v < 60 or v > 200:
raise ValueError("电量提醒数值必须在 60 ~ 200 之间")
@ -44,7 +45,8 @@ class ExpeditionData(TaskDataBase):
class DailyData(TaskDataBase):
notice_hour: Optional[int] = 22
@validator("notice_hour")
@field_validator("notice_hour")
@classmethod
def notice_hour_validator(cls, v):
if v < 0 or v > 23:
raise ValueError("每日任务提醒时间必须在 0 ~ 23 之间")
@ -55,9 +57,9 @@ class WebAppData(BaseModel):
user_id: int
player_id: int
resin: Optional[ResinData]
expedition: Optional[ExpeditionData]
daily: Optional[DailyData]
resin: Optional[ResinData] = None
expedition: Optional[ExpeditionData] = None
daily: Optional[DailyData] = None
class DailyNoteTaskUser:

View File

@ -7,7 +7,6 @@ from functools import lru_cache, partial
from typing import Any, List, Optional, Tuple, Union, TYPE_CHECKING
from arkowrapper import ArkoWrapper
from pytz import timezone
from simnet.models.zzz.chronicle.challenge import ZZZChallenge
from telegram import Message, Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ChatAction, ParseMode
@ -38,7 +37,6 @@ if TYPE_CHECKING:
from simnet import ZZZClient
TZ = timezone("Asia/Shanghai")
cmd_pattern = r"(?i)^/challenge(?:@[\w]+)?\s*((?:\d+)|(?:all))?\s*(pre)?"
msg_pattern = r"^防卫战数据((?:查询)|(?:总览))(上期)?\D?(\d*)?.*?$"
MAX_FLOOR = 7
@ -214,7 +212,7 @@ class ChallengePlugin(Plugin):
render_data = {
"floor": floor_data,
"floor_time": floor_data.floor_challenge_time.datetime.astimezone(TZ).strftime("%Y-%m-%d %H:%M:%S"),
"floor_time": floor_data.floor_challenge_time.datetime.strftime("%Y-%m-%d %H:%M:%S"),
"floor_nodes": [floor_data.node_1, floor_data.node_2],
"floor_num": floor,
"character_icons": character_icons,
@ -263,8 +261,8 @@ class ChallengePlugin(Plugin):
if not abyss_data.has_data:
raise AbyssUnlocked()
start_time = abyss_data.begin_time.datetime.astimezone(TZ).strftime("%m月%d%H:%M")
end_time = abyss_data.end_time.datetime.astimezone(TZ).strftime("%m月%d%H:%M")
start_time = abyss_data.begin_time.datetime.strftime("%m月%d%H:%M")
end_time = abyss_data.end_time.datetime.strftime("%m月%d%H:%M")
dura = self.from_seconds_to_hours(abyss_data.fast_layer_time)
max_floor_map = {1: "", 2: "", 3: "", 4: "", 5: "", 6: "", 7: ""}
max_floor = f"{max_floor_map.get(abyss_data.max_layer, abyss_data.max_layer)}防线"
@ -362,7 +360,7 @@ class ChallengePlugin(Plugin):
@staticmethod
def get_season_data_name(data: "HistoryDataAbyss"):
last_battles = data.abyss_data.floors[0]
start_time = last_battles.floor_challenge_time.datetime.astimezone(TZ)
start_time = last_battles.floor_challenge_time.datetime
time = start_time.strftime("%Y.%m.%d")
name = ""
if "" in last_battles.zone_name:

View File

@ -13,7 +13,7 @@ authors = [
{name = "SiHuaN"},
]
dependencies = [
"httpx<1.0.0,>=0.25.0",
"httpx<1.0.0,>=0.28.0",
"ujson<6.0.0,>=5.9.0",
"Jinja2<4.0.0,>=3.1.2",
"python-telegram-bot[ext,rate-limiter]<22.0,>=21.7",
@ -44,7 +44,9 @@ dependencies = [
"aiosqlite<1.0.0,>=0.20.0",
"simnet @ git+https://github.com/PaiGramTeam/SIMNet",
"psutil<7.0.0,>=6.0.0",
"influxdb-client[async,ciso]>=1.43.0",
"influxdb-client[async,ciso]>=1.48.0",
"pydantic>=2.0.0,<3.0.0",
"pydantic-settings>=2.6.1",
]
requires-python = "<4.0,>=3.9"
readme = "README.md"

View File

@ -3,16 +3,17 @@
aiocsv==1.3.2
aiofiles==24.1.0
aiohappyeyeballs==2.4.3
aiohttp==3.10.10
aiohttp==3.11.8
aiolimiter==1.1.0
aiosignal==1.3.1
aiosqlite==0.20.0
alembic==1.14.0
annotated-types==0.7.0
anyio==4.6.2.post1
apscheduler==3.10.4
arko-wrapper==0.3.0
async-lru==2.0.4
async-timeout==4.0.3 ; python_full_version < '3.11.3'
async-timeout==5.0.1 ; python_full_version < '3.11.3'
asyncmy==0.2.9
attrs==24.2.0
beautifulsoup4==4.12.3
@ -25,22 +26,22 @@ click==8.1.7
colorama==0.4.6 ; sys_platform == 'win32' or platform_system == 'Windows'
colorlog==6.9.0
cryptography==43.0.3
enkanetwork-py @ git+https://github.com/PaiGramTeam/EnkaNetwork.py@0889dc2de8f216a0bcbe983bcc4ed71cd7917d6a
enkanetwork-py @ git+https://github.com/PaiGramTeam/EnkaNetwork.py@28ca9bc889589699b543782a82c17584b33e537d
et-xmlfile==2.0.0
exceptiongroup==1.2.2 ; python_full_version < '3.11'
fakeredis==2.26.1
fastapi==0.115.4
fastapi==0.115.5
flaky==3.8.1
frozenlist==1.5.0
gitdb==4.0.11
gitpython==3.1.43
greenlet==3.1.1
h11==0.14.0
httpcore==1.0.6
httpcore==1.0.7
httptools==0.6.4
httpx==0.27.2
httpx==0.28.0
idna==3.10
influxdb-client==1.47.0
influxdb-client==1.48.0
iniconfig==2.0.0
jinja2==3.1.4
lxml==5.3.0
@ -61,7 +62,9 @@ propcache==0.2.0
psutil==6.1.0
pyaes==1.6.1
pycparser==2.22 ; platform_python_implementation != 'PyPy'
pydantic==1.10.19
pydantic==2.10.2
pydantic-core==2.27.1
pydantic-settings==2.6.1
pyee==12.0.0
pygments==2.18.0
pyrogram==2.0.106
@ -78,9 +81,9 @@ rapidfuzz==3.10.1
reactivex==4.0.4
redis==5.2.0
rich==13.9.4
sentry-sdk==2.18.0
setuptools==75.3.0
simnet @ git+https://github.com/PaiGramTeam/SIMNet@745000612682f7b346d92b21f4d60502b92f477c
sentry-sdk==2.19.0
setuptools==75.6.0
simnet @ git+https://github.com/PaiGramTeam/SIMNet@d7756addb558356adc65e7e14dc86e0a3cb5d8bd
six==1.16.0
smmap==5.0.1
sniffio==1.3.1
@ -88,18 +91,18 @@ sortedcontainers==2.4.0
soupsieve==2.6
sqlalchemy==2.0.36
sqlmodel==0.0.22
starlette==0.41.2
starlette==0.41.3
tgcrypto==1.2.5
thefuzz==0.22.1
tomli==2.0.2 ; python_full_version < '3.11'
tornado==6.4.1
tomli==2.2.1 ; python_full_version < '3.11'
tornado==6.4.2
typing-extensions==4.12.2
tzdata==2024.2 ; platform_system == 'Windows'
tzlocal==5.2
ujson==5.10.0
urllib3==2.2.3
uvicorn==0.32.0
uvicorn==0.32.1
uvloop==0.21.0 ; platform_python_implementation != 'PyPy' and sys_platform != 'cygwin' and sys_platform != 'win32'
watchfiles==0.24.0
websockets==13.1
yarl==1.17.1
watchfiles==1.0.0
websockets==14.1
yarl==1.18.0

View File

@ -1,8 +1,8 @@
from multiprocessing import RLock as Lock
from pathlib import Path
from typing import List, Literal, Optional, Union
from typing import List, Literal, Optional, Union, ClassVar
from pydantic import BaseSettings
from pydantic_settings import BaseSettings
from utils.const import PROJECT_ROOT
@ -10,13 +10,13 @@ __all__ = ("LoggerConfig",)
class LoggerConfig(BaseSettings):
_lock = Lock()
_instance: Optional["LoggerConfig"] = None
_lock: ClassVar[Lock] = Lock()
_instance: ClassVar[Optional["LoggerConfig"]] = None
def __new__(cls, *args, **kwargs) -> "LoggerConfig":
with cls._lock:
if cls._instance is None:
cls.update_forward_refs()
cls.model_rebuild()
result = super(LoggerConfig, cls).__new__(cls) # pylint: disable=E1120
result.__init__(*args, **kwargs)
cls._instance = result

889
uv.lock

File diff suppressed because it is too large Load Diff