mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 19:11:00 +00:00
💥 Remove: 移除 Python 3.8 支持 (#2641)
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import abc
|
||||
from typing import Any
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, Dict, AsyncGenerator
|
||||
|
||||
from nonebot.config import Config
|
||||
from nonebot.internal.driver._lifespan import LIFESPAN_FUNC
|
||||
@ -32,7 +33,7 @@ class Adapter(abc.ABC):
|
||||
def __init__(self, driver: Driver, **kwargs: Any):
|
||||
self.driver: Driver = driver
|
||||
"""{ref}`nonebot.drivers.Driver` 实例"""
|
||||
self.bots: Dict[str, Bot] = {}
|
||||
self.bots: dict[str, Bot] = {}
|
||||
"""本协议适配器已建立连接的 {ref}`nonebot.adapters.Bot` 实例"""
|
||||
|
||||
def __repr__(self) -> str:
|
||||
|
@ -1,7 +1,7 @@
|
||||
import abc
|
||||
import asyncio
|
||||
from functools import partial
|
||||
from typing import TYPE_CHECKING, Any, Set, Union, ClassVar, Optional, Protocol
|
||||
from typing import TYPE_CHECKING, Any, Union, ClassVar, Optional, Protocol
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.config import Config
|
||||
@ -27,9 +27,9 @@ class Bot(abc.ABC):
|
||||
self_id: 机器人 ID
|
||||
"""
|
||||
|
||||
_calling_api_hook: ClassVar[Set[T_CallingAPIHook]] = set()
|
||||
_calling_api_hook: ClassVar[set[T_CallingAPIHook]] = set()
|
||||
"""call_api 时执行的函数"""
|
||||
_called_api_hook: ClassVar[Set[T_CalledAPIHook]] = set()
|
||||
_called_api_hook: ClassVar[set[T_CalledAPIHook]] = set()
|
||||
"""call_api 后执行的函数"""
|
||||
|
||||
def __init__(self, adapter: "Adapter", self_id: str):
|
||||
|
@ -1,5 +1,5 @@
|
||||
import abc
|
||||
from typing import Any, Type, TypeVar
|
||||
from typing import Any, TypeVar
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
@ -25,7 +25,7 @@ class Event(abc.ABC, BaseModel):
|
||||
if not PYDANTIC_V2: # pragma: pydantic-v1
|
||||
|
||||
@classmethod
|
||||
def validate(cls: Type["E"], value: Any) -> "E":
|
||||
def validate(cls: type["E"], value: Any) -> "E":
|
||||
if isinstance(value, Event) and not isinstance(value, cls):
|
||||
raise TypeError(f"{value} is incompatible with Event type {cls}")
|
||||
return super().validate(value)
|
||||
|
@ -1,17 +1,14 @@
|
||||
import abc
|
||||
from copy import deepcopy
|
||||
from typing_extensions import Self
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import field, asdict, dataclass
|
||||
from typing import (
|
||||
from typing import ( # noqa: UP035
|
||||
Any,
|
||||
Dict,
|
||||
List,
|
||||
Type,
|
||||
Tuple,
|
||||
Union,
|
||||
Generic,
|
||||
TypeVar,
|
||||
Iterable,
|
||||
Optional,
|
||||
SupportsIndex,
|
||||
overload,
|
||||
@ -32,12 +29,12 @@ class MessageSegment(abc.ABC, Generic[TM]):
|
||||
|
||||
type: str
|
||||
"""消息段类型"""
|
||||
data: Dict[str, Any] = field(default_factory=dict)
|
||||
data: dict[str, Any] = field(default_factory=dict)
|
||||
"""消息段数据"""
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_message_class(cls) -> Type[TM]:
|
||||
def get_message_class(cls) -> Type[TM]: # noqa: UP006
|
||||
"""获取消息数组类型"""
|
||||
raise NotImplementedError
|
||||
|
||||
@ -49,7 +46,9 @@ class MessageSegment(abc.ABC, Generic[TM]):
|
||||
def __len__(self) -> int:
|
||||
return len(str(self))
|
||||
|
||||
def __ne__(self, other: Self) -> bool:
|
||||
def __ne__( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self, other: Self
|
||||
) -> bool:
|
||||
return not self == other
|
||||
|
||||
def __add__(self: TMS, other: Union[str, TMS, Iterable[TMS]]) -> TM:
|
||||
@ -101,7 +100,7 @@ class MessageSegment(abc.ABC, Generic[TM]):
|
||||
|
||||
|
||||
@custom_validation
|
||||
class Message(List[TMS], abc.ABC):
|
||||
class Message(list[TMS], abc.ABC):
|
||||
"""消息序列
|
||||
|
||||
参数:
|
||||
@ -142,7 +141,7 @@ class Message(List[TMS], abc.ABC):
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_segment_class(cls) -> Type[TMS]:
|
||||
def get_segment_class(cls) -> type[TMS]:
|
||||
"""获取消息段类型"""
|
||||
raise NotImplementedError
|
||||
|
||||
@ -177,7 +176,9 @@ class Message(List[TMS], abc.ABC):
|
||||
"""构造消息数组"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __add__(self, other: Union[str, TMS, Iterable[TMS]]) -> Self:
|
||||
def __add__( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self, other: Union[str, TMS, Iterable[TMS]]
|
||||
) -> Self:
|
||||
result = self.copy()
|
||||
result += other
|
||||
return result
|
||||
@ -209,7 +210,7 @@ class Message(List[TMS], abc.ABC):
|
||||
"""
|
||||
|
||||
@overload
|
||||
def __getitem__(self, args: Tuple[str, int]) -> TMS:
|
||||
def __getitem__(self, args: tuple[str, int]) -> TMS:
|
||||
"""索引指定类型的消息段
|
||||
|
||||
参数:
|
||||
@ -220,7 +221,7 @@ class Message(List[TMS], abc.ABC):
|
||||
"""
|
||||
|
||||
@overload
|
||||
def __getitem__(self, args: Tuple[str, slice]) -> Self:
|
||||
def __getitem__(self, args: tuple[str, slice]) -> Self:
|
||||
"""切片指定类型的消息段
|
||||
|
||||
参数:
|
||||
@ -252,12 +253,12 @@ class Message(List[TMS], abc.ABC):
|
||||
消息切片 `args`
|
||||
"""
|
||||
|
||||
def __getitem__(
|
||||
def __getitem__( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self,
|
||||
args: Union[
|
||||
str,
|
||||
Tuple[str, int],
|
||||
Tuple[str, slice],
|
||||
tuple[str, int],
|
||||
tuple[str, slice],
|
||||
int,
|
||||
slice,
|
||||
],
|
||||
@ -276,7 +277,9 @@ class Message(List[TMS], abc.ABC):
|
||||
else:
|
||||
raise ValueError("Incorrect arguments to slice") # pragma: no cover
|
||||
|
||||
def __contains__(self, value: Union[TMS, str]) -> bool:
|
||||
def __contains__( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self, value: Union[TMS, str]
|
||||
) -> bool:
|
||||
"""检查消息段是否存在
|
||||
|
||||
参数:
|
||||
@ -359,7 +362,9 @@ class Message(List[TMS], abc.ABC):
|
||||
return all(seg.type == value for seg in self)
|
||||
return all(seg == value for seg in self)
|
||||
|
||||
def append(self, obj: Union[str, TMS]) -> Self:
|
||||
def append( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self, obj: Union[str, TMS]
|
||||
) -> Self:
|
||||
"""添加一个消息段到消息数组末尾。
|
||||
|
||||
参数:
|
||||
@ -373,7 +378,9 @@ class Message(List[TMS], abc.ABC):
|
||||
raise ValueError(f"Unexpected type: {type(obj)} {obj}") # pragma: no cover
|
||||
return self
|
||||
|
||||
def extend(self, obj: Union[Self, Iterable[TMS]]) -> Self:
|
||||
def extend( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self, obj: Union[Self, Iterable[TMS]]
|
||||
) -> Self:
|
||||
"""拼接一个消息数组或多个消息段到消息数组末尾。
|
||||
|
||||
参数:
|
||||
|
@ -1,21 +1,15 @@
|
||||
import functools
|
||||
from string import Formatter
|
||||
from typing_extensions import TypeAlias
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Set,
|
||||
Dict,
|
||||
List,
|
||||
Type,
|
||||
Tuple,
|
||||
Union,
|
||||
Generic,
|
||||
Mapping,
|
||||
TypeVar,
|
||||
Callable,
|
||||
Optional,
|
||||
Sequence,
|
||||
cast,
|
||||
overload,
|
||||
)
|
||||
@ -27,7 +21,7 @@ if TYPE_CHECKING:
|
||||
|
||||
def formatter_field_name_split(
|
||||
field_name: str,
|
||||
) -> Tuple[str, List[Tuple[bool, str]]]: ...
|
||||
) -> tuple[str, list[tuple[bool, str]]]: ...
|
||||
|
||||
|
||||
TM = TypeVar("TM", bound="Message")
|
||||
@ -50,7 +44,7 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
def __init__(
|
||||
self: "MessageTemplate[str]",
|
||||
template: str,
|
||||
factory: Type[str] = str,
|
||||
factory: type[str] = str,
|
||||
private_getattr: bool = False,
|
||||
) -> None: ...
|
||||
|
||||
@ -58,19 +52,19 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
def __init__(
|
||||
self: "MessageTemplate[TM]",
|
||||
template: Union[str, TM],
|
||||
factory: Type[TM],
|
||||
factory: type[TM],
|
||||
private_getattr: bool = False,
|
||||
) -> None: ...
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
template: Union[str, TM],
|
||||
factory: Union[Type[str], Type[TM]] = str,
|
||||
factory: Union[type[str], type[TM]] = str,
|
||||
private_getattr: bool = False,
|
||||
) -> None:
|
||||
self.template: TF = template # type: ignore
|
||||
self.factory: Type[TF] = factory # type: ignore
|
||||
self.format_specs: Dict[str, FormatSpecFunc] = {}
|
||||
self.factory: type[TF] = factory # type: ignore
|
||||
self.format_specs: dict[str, FormatSpecFunc] = {}
|
||||
self.private_getattr = private_getattr
|
||||
|
||||
def __repr__(self) -> str:
|
||||
@ -85,7 +79,9 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
self.format_specs[name] = spec
|
||||
return spec
|
||||
|
||||
def format(self, *args, **kwargs):
|
||||
def format( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self, *args, **kwargs
|
||||
) -> TF:
|
||||
"""根据传入参数和模板生成消息对象"""
|
||||
return self._format(args, kwargs)
|
||||
|
||||
@ -118,7 +114,7 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
self.check_unused_args(used_args, args, kwargs)
|
||||
return cast(TF, full_message)
|
||||
|
||||
def vformat(
|
||||
def vformat( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self,
|
||||
format_string: str,
|
||||
args: Sequence[Any],
|
||||
@ -126,15 +122,15 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
) -> TF:
|
||||
raise NotImplementedError("`vformat` has merged into `_format`")
|
||||
|
||||
def _vformat(
|
||||
def _vformat( # pyright: ignore[reportIncompatibleMethodOverride]
|
||||
self,
|
||||
format_string: str,
|
||||
args: Sequence[Any],
|
||||
kwargs: Mapping[str, Any],
|
||||
used_args: Set[Union[int, str]],
|
||||
used_args: set[Union[int, str]],
|
||||
auto_arg_index: int = 0,
|
||||
) -> Tuple[TF, int]:
|
||||
results: List[Any] = [self.factory()]
|
||||
) -> tuple[TF, int]:
|
||||
results: list[Any] = [self.factory()]
|
||||
|
||||
for literal_text, field_name, format_spec, conversion in self.parse(
|
||||
format_string
|
||||
@ -185,7 +181,7 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
|
||||
def get_field(
|
||||
self, field_name: str, args: Sequence[Any], kwargs: Mapping[str, Any]
|
||||
) -> Tuple[Any, Union[int, str]]:
|
||||
) -> tuple[Any, Union[int, str]]:
|
||||
first, rest = formatter_field_name_split(field_name)
|
||||
obj = self.get_value(first, args, kwargs)
|
||||
|
||||
@ -199,7 +195,7 @@ class MessageTemplate(Formatter, Generic[TF]):
|
||||
def format_field(self, value: Any, format_spec: str) -> Any:
|
||||
formatter: Optional[FormatSpecFunc] = self.format_specs.get(format_spec)
|
||||
if formatter is None and not issubclass(self.factory, str):
|
||||
segment_class: Type["MessageSegment"] = self.factory.get_segment_class()
|
||||
segment_class: type["MessageSegment"] = self.factory.get_segment_class()
|
||||
method = getattr(segment_class, format_spec, None)
|
||||
if callable(method) and not cast(str, method.__name__).startswith("_"):
|
||||
formatter = getattr(segment_class, format_spec)
|
||||
|
Reference in New Issue
Block a user