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

Feature: 带参数的 RegexStr() #2499

Merged
merged 1 commit into from
Jan 11, 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
45 changes: 40 additions & 5 deletions nonebot/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
description: nonebot.params 模块
"""

from typing import Any, Dict, List, Match, Tuple, Union, Optional
from typing import (
Any,
Dict,
List,
Match,
Tuple,
Union,
Literal,
Callable,
Optional,
overload,
)

from nonebot.typing import T_State
from nonebot.matcher import Matcher
Expand Down Expand Up @@ -147,13 +158,37 @@ def RegexMatched() -> Match[str]:
return Depends(_regex_matched, use_cache=False)


def _regex_str(state: T_State) -> str:
return _regex_matched(state).group()
def _regex_str(
groups: Tuple[Union[str, int], ...]
) -> Callable[[T_State], Union[str, Tuple[Union[str, Any], ...], Any]]:
def _regex_str_dependency(
state: T_State,
) -> Union[str, Tuple[Union[str, Any], ...], Any]:
return _regex_matched(state).group(*groups)

return _regex_str_dependency


@overload
def RegexStr(__group: Literal[0] = 0) -> str:
...


@overload
def RegexStr(__group: Union[str, int]) -> Union[str, Any]:
...


@overload
def RegexStr(
__group1: Union[str, int], __group2: Union[str, int], *groups: Union[str, int]
) -> Tuple[Union[str, Any], ...]:
...
yanyongyu marked this conversation as resolved.
Show resolved Hide resolved


def RegexStr() -> str:
def RegexStr(*groups: Union[str, int]) -> Union[str, Tuple[Union[str, Any], ...], Any]:
"""正则匹配结果文本"""
return Depends(_regex_str, use_cache=False)
return Depends(_regex_str(groups), use_cache=False)


def _regex_group(state: T_State) -> Tuple[Any, ...]:
Expand Down
9 changes: 7 additions & 2 deletions tests/plugins/param/param_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ async def regex_matched(regex_matched: Match[str] = RegexMatched()) -> Match[str
return regex_matched


async def regex_str(regex_str: str = RegexStr()) -> str:
return regex_str
async def regex_str(
entire: str = RegexStr(),
type_: str = RegexStr("type"),
second: str = RegexStr(2),
groups: Tuple[str, ...] = RegexStr(1, "arg"),
) -> Tuple[str, str, str, Tuple[str, ...]]:
return entire, type_, second, groups


async def startswith(startswith: str = Startswith()) -> str:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ async def test_state(app: App):
regex_str, allow_types=[StateParam, DependParam]
) as ctx:
ctx.pass_params(state=fake_state)
ctx.should_return("[cq:test,arg=value]")
ctx.should_return(
("[cq:test,arg=value]", "test", "arg=value", ("test", "arg=value"))
)

async with app.test_dependent(
regex_group, allow_types=[StateParam, DependParam]
Expand Down
Loading