Skip to content
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ci:
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
rev: v0.3.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
6 changes: 3 additions & 3 deletions nonebot/internal/adapter/bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import asyncio
from functools import partial
from typing import TYPE_CHECKING, Any, Set, Union, Optional, Protocol
from typing import TYPE_CHECKING, Any, Set, Union, ClassVar, Optional, Protocol

from nonebot.log import logger
from nonebot.config import Config
Expand All @@ -27,9 +27,9 @@ class Bot(abc.ABC):
self_id: 机器人 ID
"""

_calling_api_hook: Set[T_CallingAPIHook] = set()
_calling_api_hook: ClassVar[Set[T_CallingAPIHook]] = set()
"""call_api 时执行的函数"""
_called_api_hook: Set[T_CalledAPIHook] = set()
_called_api_hook: ClassVar[Set[T_CalledAPIHook]] = set()
"""call_api 后执行的函数"""

def __init__(self, adapter: "Adapter", self_id: str):
Expand Down
2 changes: 1 addition & 1 deletion nonebot/internal/adapter/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Event(abc.ABC, BaseModel):

class Config(ConfigDict):
extra = "allow" # type: ignore
json_encoders = {Message: DataclassEncoder}
json_encoders = {Message: DataclassEncoder} # noqa: RUF012

if not PYDANTIC_V2: # pragma: pydantic-v1

Expand Down
2 changes: 1 addition & 1 deletion nonebot/internal/adapter/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
if TYPE_CHECKING:
from .message import Message, MessageSegment

def formatter_field_name_split( # noqa: F811
def formatter_field_name_split(
field_name: str,
) -> Tuple[str, List[Tuple[bool, str]]]: ...

Expand Down
8 changes: 4 additions & 4 deletions nonebot/internal/driver/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
from typing_extensions import TypeAlias
from contextlib import AsyncExitStack, asynccontextmanager
from typing import TYPE_CHECKING, Any, Set, Dict, Type, AsyncGenerator
from typing import TYPE_CHECKING, Any, Set, Dict, Type, ClassVar, AsyncGenerator

from nonebot.log import logger
from nonebot.config import Env, Config
Expand Down Expand Up @@ -36,11 +36,11 @@ class Driver(abc.ABC):
config: 包含配置信息的 Config 对象
"""

_adapters: Dict[str, "Adapter"] = {}
_adapters: ClassVar[Dict[str, "Adapter"]] = {}
"""已注册的适配器列表"""
_bot_connection_hook: Set[Dependent[Any]] = set()
_bot_connection_hook: ClassVar[Set[Dependent[Any]]] = set()
"""Bot 连接建立时执行的函数"""
_bot_disconnection_hook: Set[Dependent[Any]] = set()
_bot_disconnection_hook: ClassVar[Set[Dependent[Any]]] = set()
"""Bot 连接断开时执行的函数"""

def __init__(self, env: Env, config: Config):
Expand Down
16 changes: 8 additions & 8 deletions nonebot/internal/matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Matcher(metaclass=MatcherMeta):
"""事件响应器匹配规则"""
permission: ClassVar[Permission] = Permission()
"""事件响应器触发权限"""
handlers: List[Dependent[Any]] = []
handlers: ClassVar[List[Dependent[Any]]] = []
"""事件响应器拥有的事件处理函数列表"""
priority: ClassVar[int] = 1
"""事件响应器优先级"""
Expand Down Expand Up @@ -171,7 +171,7 @@ class Matcher(metaclass=MatcherMeta):
)

def __init__(self):
self.handlers = self.handlers.copy()
self.remain_handlers: List[Dependent[Any]] = self.handlers.copy()
self.state = self._default_state.copy()

def __repr__(self) -> str:
Expand Down Expand Up @@ -457,7 +457,7 @@ def receive(
parameterless: 非参数类型依赖列表
"""

async def _receive(event: Event, matcher: "Matcher") -> Union[None, NoReturn]:
async def _receive(event: Event, matcher: "Matcher") -> None:
matcher.set_target(RECEIVE_KEY.format(id=id))
if matcher.get_target() == RECEIVE_KEY.format(id=id):
matcher.set_receive(id, event)
Expand Down Expand Up @@ -775,7 +775,7 @@ async def update_permission(

async def resolve_reject(self):
handler = current_handler.get()
self.handlers.insert(0, handler)
self.remain_handlers.insert(0, handler)
if REJECT_CACHE_TARGET in self.state:
self.state[REJECT_TARGET] = self.state[REJECT_CACHE_TARGET]

Expand Down Expand Up @@ -809,8 +809,8 @@ async def simple_run(
# Refresh preprocess state
self.state.update(state)

while self.handlers:
handler = self.handlers.pop(0)
while self.remain_handlers:
handler = self.remain_handlers.pop(0)
current_handler.set(handler)
logger.debug(f"Running handler {handler}")
try:
Expand Down Expand Up @@ -852,7 +852,7 @@ async def run(
type_,
Rule(),
permission,
self.handlers,
self.remain_handlers,
temp=True,
priority=0,
block=True,
Expand All @@ -872,7 +872,7 @@ async def run(
type_,
Rule(),
permission,
self.handlers,
self.remain_handlers,
temp=True,
priority=0,
block=True,
Expand Down
8 changes: 4 additions & 4 deletions nonebot/internal/permission.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import asyncio
from typing_extensions import Self
from contextlib import AsyncExitStack
from typing import Set, Tuple, Union, NoReturn, Optional
from typing import Set, List, Type, Tuple, Union, ClassVar, NoReturn, Optional

from nonebot.dependencies import Dependent
from nonebot.utils import run_coro_with_catch
from nonebot.exception import SkippedException
from nonebot.typing import T_DependencyCache, T_PermissionChecker

from .adapter import Bot, Event
from .params import BotParam, EventParam, DependParam, DefaultParam
from .params import Param, BotParam, EventParam, DependParam, DefaultParam


class Permission:
Expand All @@ -30,7 +30,7 @@ class Permission:

__slots__ = ("checkers",)

HANDLER_PARAM_TYPES = [
HANDLER_PARAM_TYPES: ClassVar[List[Type[Param]]] = [
DependParam,
BotParam,
EventParam,
Expand Down Expand Up @@ -146,7 +146,7 @@ async def __call__(self, bot: Bot, event: Event) -> bool:
@classmethod
def _clean_permission(cls, perm: Permission) -> Optional[Permission]:
if len(perm.checkers) == 1 and isinstance(
user_perm := tuple(perm.checkers)[0].call, cls
user_perm := next(iter(perm.checkers)).call, cls
):
return user_perm.perm
return perm
Expand Down
6 changes: 3 additions & 3 deletions nonebot/internal/rule.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio
from contextlib import AsyncExitStack
from typing import Set, Union, NoReturn, Optional
from typing import Set, List, Type, Union, ClassVar, NoReturn, Optional

from nonebot.dependencies import Dependent
from nonebot.exception import SkippedException
from nonebot.typing import T_State, T_RuleChecker, T_DependencyCache

from .adapter import Bot, Event
from .params import BotParam, EventParam, StateParam, DependParam, DefaultParam
from .params import Param, BotParam, EventParam, StateParam, DependParam, DefaultParam


class Rule:
Expand All @@ -28,7 +28,7 @@ class Rule:

__slots__ = ("checkers",)

HANDLER_PARAM_TYPES = [
HANDLER_PARAM_TYPES: ClassVar[List[Type[Param]]] = [
DependParam,
BotParam,
EventParam,
Expand Down
2 changes: 1 addition & 1 deletion nonebot/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def exec_module(self, module: ModuleType) -> None:
break

# enter plugin context
_plugin_token = _current_plugin_chain.set(parent_plugins + (plugin,))
_plugin_token = _current_plugin_chain.set((*parent_plugins, plugin))

try:
super().exec_module(module)
Expand Down
64 changes: 32 additions & 32 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ uvicorn = { version = ">=0.20.0,<1.0.0", extras = [
], optional = true }

[tool.poetry.group.dev.dependencies]
ruff = "^0.2.0"
ruff = "^0.3.0"
isort = "^5.10.1"
black = "^24.0.0"
nonemoji = "^0.1.2"
Expand Down Expand Up @@ -90,8 +90,27 @@ line-length = 88
target-version = "py38"

[tool.ruff.lint]
select = ["E", "W", "F", "UP", "C", "T", "PYI", "PT", "Q"]
ignore = ["E402", "C901", "UP037"]
select = [
"F", # Pyflakes
"W", # pycodestyle warnings
"E", # pycodestyle errors
"UP", # pyupgrade
"ASYNC", # flake8-async
"C4", # flake8-comprehensions
"T10", # flake8-debugger
"T20", # flake8-print
"PYI", # flake8-pyi
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"RUF", # Ruff-specific rules
]
ignore = [
"E402", # module-import-not-at-top-of-file
"UP037", # quoted-annotation
"RUF001", # ambiguous-unicode-character-string
"RUF002", # ambiguous-unicode-character-docstring
"RUF003", # ambiguous-unicode-character-comment
]

[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/param/param_depend.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async def gen_async():

@dataclass
class ClassDependency:
x: int = Depends(gen_sync)
y: int = Depends(gen_async)
x: int = Depends(gen_sync) # noqa: RUF009
y: int = Depends(gen_async) # noqa: RUF009


class FooBot(Bot): ...
Expand Down
Loading