Skip to content

Commit

Permalink
[OneBot] Fix dynamic segment attributes inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
aicorein committed Dec 11, 2024
1 parent 12b1d17 commit 51f7cbe
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions src/melobot/protocols/onebot/v11/adapter/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@
import re
import warnings
from collections.abc import Mapping
from functools import partial
from itertools import chain, zip_longest
from typing import (
Annotated,
Any,
Generic,
Literal,
Match,
TypeAlias,
cast,
final,
get_args,
overload,
)

from beartype.door import is_subhint
from pydantic import (
Expand All @@ -30,12 +17,25 @@
UrlConstraints,
create_model,
)
from typing_extensions import NotRequired, Self, TypedDict, TypeVar
from typing_extensions import (
Annotated,
Any,
Generic,
Literal,
Match,
NotRequired,
Self,
TypeAlias,
TypedDict,
TypeVar,
cast,
final,
get_args,
overload,
)

from melobot.adapter import content as mbcontent

from ..const import T, V

MediaUrl: TypeAlias = Annotated[
AnyUrl, UrlConstraints(allowed_schemes=["http", "https", "file", "base64"])
]
Expand Down Expand Up @@ -283,8 +283,8 @@ def __init__(self, seg_type: _SegTypeT, **seg_data: Any) -> None:
@classmethod
@final
def add_type(
cls, seg_type_hint: type[T], seg_data_hint: type[V]
) -> type[_CustomSegInterface[T, V]]: # type: ignore[type-var]
cls, seg_type_hint: type[_SegTypeT], seg_data_hint: type[_SegDataT]
) -> type[_CustomSegInterface[_SegTypeT, _SegDataT]]: # type: ignore[type-var]
if cls is not Segment:
raise ValueError(
f"只能使用 {Segment.__name__} 类的 {Segment.add_type.__name__} 方法"
Expand All @@ -306,10 +306,6 @@ def add_type(
if type_classname in {subcls.__name__ for subcls in cls.__subclasses__()}:
raise ValueError(f"类型为 {type_name} 的消息段类型已经存在")

def __custom_init__(self: type, **data: Any) -> None:
model = getattr(self, "Model")(type=getattr(self, "SegTypeVal"), data=data)
setattr(self, "_model", model)

seg_cls = type(
type_classname,
(Segment,),
Expand All @@ -318,21 +314,22 @@ def __custom_init__(self: type, **data: Any) -> None:
type_dataname,
type=(seg_type_hint, ...),
data=(seg_data_hint, ...),
),
"SegTypeVal": type_name,
)
},
)
setattr(
seg_cls,
"__init__",
partial(__custom_init__, seg_cls),
Segment.resolve.__name__,
lambda _, seg_data: seg_cls(type_name, **seg_data),
)
setattr(
return cast(
type[
_CustomSegInterface[ # pylint: disable=unsubscriptable-object
_SegTypeT, _SegDataT
]
],
seg_cls,
Segment.resolve.__name__,
lambda _, seg_data: seg_cls(**seg_data),
)
return cast(type[_CustomSegInterface[T, V]], seg_cls) # type: ignore[type-var]

@property
def type(self) -> _SegTypeT:
Expand Down

0 comments on commit 51f7cbe

Please sign in to comment.