From b50cc35eec4e900fb82a45b3ce8b1c0ae43e4a05 Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Tue, 23 Apr 2024 17:39:49 +0800 Subject: [PATCH] :bug: version 0.7.1 fix type_convert logic --- nepattern/base.py | 2 +- nepattern/core.py | 69 ++++++++------- nepattern/core.pyi | 212 ++++++++++++++++++++++++++++++++++++++------- nepattern/main.py | 4 +- pyproject.toml | 2 +- test.py | 1 + 6 files changed, 227 insertions(+), 63 deletions(-) diff --git a/nepattern/base.py b/nepattern/base.py index b99708f..2924437 100644 --- a/nepattern/base.py +++ b/nepattern/base.py @@ -376,7 +376,7 @@ def __calc_eq__(self, other): # pragma: no cover class ForwardRefPattern(BasePattern[Any, Any, Literal[MatchMode.TYPE_CONVERT]]): def __init__(self, ref: ForwardRef): self.ref = ref - super().__init__(mode=MatchMode.TYPE_CONVERT, origin=Any, alias=ref.__forward_arg__) + super().__init__(mode=MatchMode.TYPE_CONVERT, origin=Any, converter=lambda _, x: eval(x), alias=ref.__forward_arg__) def match(self, input_: Any): if isinstance(input_, str) and input_ == self.ref.__forward_arg__: diff --git a/nepattern/core.py b/nepattern/core.py index 0931e6c..673b935 100644 --- a/nepattern/core.py +++ b/nepattern/core.py @@ -50,9 +50,6 @@ class ResultFlag(str, Enum): class ValidateResult(Generic[TVOrigin, TVRF]): """参数表达式验证结果""" - def __new__(cls, *args, **kwargs): - return object.__new__(cls) - def __init__( self, value: TVOrigin | type[Empty] = Empty, @@ -334,14 +331,14 @@ def _type_convert_no_previous_accepts_any(self: BasePattern[TOrigin, Any, Litera def _type_convert_no_previous_accepts_other(self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], input_: Any) -> TOrigin: - if generic_isinstance(input_, self.origin): - return input_ if not self.accept(input_): raise MatchFailed( lang.require("nepattern", "type_error").format( type=input_.__class__, target=input_, expected=self.alias ) ) + if generic_isinstance(input_, self.origin): + return input_ if (res := self.converter(self, input_)) is None: raise MatchFailed( lang.require("nepattern", "content_error").format(target=input_, expected=self.alias) @@ -352,8 +349,7 @@ def _type_convert_no_previous_accepts_other(self: BasePattern[TOrigin, Any, Lite def _type_convert_type_no_accepts_any(self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], input_: Any) -> TOrigin: if TYPE_CHECKING: assert self.previous - res = self.converter(self, input_) - if res is None and not generic_isinstance(res := self.converter(self, self.previous.match(input_)), self.origin): + if (res := self.converter(self, input_)) is None and (res := self.converter(self, self.previous.match(input_))) is None: raise MatchFailed( lang.require("nepattern", "content_error").format(target=input_, expected=self.alias) ) @@ -365,8 +361,7 @@ def _type_convert_type_no_accepts_other(self: BasePattern[TOrigin, Any, Literal[ assert self.previous if generic_isinstance(input_, self.origin): return input_ - res = self.converter(self, input_) - if res is None and not generic_isinstance(res := self.converter(self, self.previous.match(input_)), self.origin): + if (res := self.converter(self, input_)) is None and (res := self.converter(self, self.previous.match(input_))) is None: raise MatchFailed( lang.require("nepattern", "content_error").format(target=input_, expected=self.alias) ) @@ -382,8 +377,7 @@ def _type_convert_type_accepts_any(self: BasePattern[TOrigin, Any, Literal[Match type=input_.__class__, target=input_, expected=self.alias ) ) - res = self.converter(self, input_) - if res is None and not generic_isinstance(res := self.converter(self, self.previous.match(input_)), self.origin): + if (res := self.converter(self, input_)) is None and (res := self.converter(self, self.previous.match(input_))) is None: raise MatchFailed( lang.require("nepattern", "content_error").format(target=input_, expected=self.alias) ) @@ -393,16 +387,15 @@ def _type_convert_type_accepts_any(self: BasePattern[TOrigin, Any, Literal[Match def _type_convert_type_accepts_other(self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], input_: Any) -> TOrigin: if TYPE_CHECKING: assert self.previous - if generic_isinstance(input_, self.origin): - return input_ if not self.accept(input_) and not self.accept(input_ := self.previous.match(input_)): raise MatchFailed( lang.require("nepattern", "type_error").format( type=input_.__class__, target=input_, expected=self.alias ) ) - res = self.converter(self, input_) - if res is None and not generic_isinstance(res := self.converter(self, self.previous.match(input_)), self.origin): + if generic_isinstance(input_, self.origin): + return input_ + if (res := self.converter(self, input_)) is None and (res := self.converter(self, self.previous.match(input_))) is None: raise MatchFailed( lang.require("nepattern", "content_error").format(target=input_, expected=self.alias) ) @@ -454,8 +447,6 @@ def _type_convert_value_accepts_any(self: BasePattern[TOrigin, Any, Literal[Matc def _type_convert_value_accepts_other(self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], input_: Any) -> TOrigin: if TYPE_CHECKING: assert self.previous - if generic_isinstance(input_, self.origin): - return input_ if self.accept(input_): input_ = self.previous.match(input_) elif not self.accept(input_ := self.previous.match(input_)): @@ -464,6 +455,8 @@ def _type_convert_value_accepts_other(self: BasePattern[TOrigin, Any, Literal[Ma type=input_.__class__, target=input_, expected=self.alias ) ) + if generic_isinstance(input_, self.origin): + return input_ if (res := self.converter(self, input_)) is None: raise MatchFailed( lang.require("nepattern", "content_error").format(target=input_, expected=self.alias) @@ -578,9 +571,9 @@ def __new__(cls, *args, **kwargs): def __init__( self, - pattern: str = ".+", - mode: TMM = MatchMode.REGEX_MATCH, - origin: type[TOrigin] = str, + pattern: str | None = None, + mode: TMM | None = None, + origin: type[TOrigin] | None = None, converter: Callable[[BasePattern, Any], TOrigin | None] | None = None, alias: str | None = None, previous: BasePattern | None = None, @@ -591,17 +584,33 @@ def __init__( """ 初始化参数匹配表达式 """ - if pattern.startswith("^") or pattern.endswith("$"): - raise ValueError(lang.require("nepattern", "pattern_head_or_tail_error").format(target=pattern)) - self.pattern = pattern - self.regex_pattern = re.compile(f"^{pattern}$") - self.mode = MatchMode(mode) - self.origin = origin + if pattern is None: + _origin = origin or Any + self.mode = MatchMode(mode or MatchMode.KEEP) + self.pattern = "" + self.regex_pattern = re.compile("") + else: + if pattern.startswith("^") or pattern.endswith("$"): + raise ValueError(lang.require("nepattern", "pattern_head_or_tail_error").format(target=pattern)) + self.pattern = pattern + self.regex_pattern = re.compile(f"^{pattern}$") + self.mode = MatchMode(mode or MatchMode.REGEX_MATCH) + _origin = origin or str + self.origin: type[TOrigin] = _origin # type: ignore self.alias = alias self.previous = previous - self.converter = converter or ( - lambda _, x: (get_origin(origin) or origin)(x) if mode == MatchMode.TYPE_CONVERT else eval(x[0]) - ) + if TYPE_CHECKING: + assert self.origin is not None + if self.mode == MatchMode.TYPE_CONVERT: + if not converter and (not origin or origin is Any): + raise ValueError(origin) + self.converter = converter or (lambda _, x: (get_origin(self.origin) or self.origin)(x)) + elif self.mode == MatchMode.VALUE_OPERATE: + if not converter: + raise ValueError(converter) + self.converter = converter + else: + self.converter = converter or (lambda _, x: eval(x[0])) self.validators = validators or [] if accepts is Any or not accepts: _accepts = Any @@ -709,7 +718,7 @@ def validate(self, input_: Any, default: TDefault | Empty = Empty) -> ValidateRe return ValidateResult(error=e, flag=ResultFlag.ERROR) return ValidateResult(default, flag=ResultFlag.DEFAULT) # type: ignore - def copy(self): + def copy(self) -> Self: return deepcopy(self) def __rrshift__(self, other): diff --git a/nepattern/core.pyi b/nepattern/core.pyi index c08a349..f858de8 100644 --- a/nepattern/core.pyi +++ b/nepattern/core.pyi @@ -40,6 +40,7 @@ TInput = TypeVar("TInput") TInput1 = TypeVar("TInput1") TInput2 = TypeVar("TInput2") TInput3 = TypeVar("TInput3") +TInput4 = TypeVar("TInput4") TOrigin = TypeVar("TOrigin") TOrigin1 = TypeVar("TOrigin1") TVOrigin = TypeVar("TVOrigin") @@ -138,7 +139,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): *, mode: Literal[MatchMode.KEEP], accepts: type[TInput], - origin: None = None, + origin: Any = None, alias: str | None = None, previous: None = None, addition_accepts: None = None, @@ -150,7 +151,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): *, mode: Literal[MatchMode.KEEP], addition_accepts: BasePattern[Any, TInput1, Any], - origin: None = None, + origin: Any = None, alias: str | None = None, previous: None = None, accepts: None = None, @@ -163,7 +164,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): mode: Literal[MatchMode.KEEP], accepts: type[TInput1], addition_accepts: BasePattern[Any, TInput2, Any], - origin: None = None, + origin: Any = None, alias: str | None = None, previous: None = None, validators: list[Callable[[TInput1 | TInput2], bool]] | None = None, @@ -175,7 +176,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): mode: Literal[MatchMode.KEEP], accepts: type[TInput1], previous: BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], - origin: None = None, + origin: Any = None, alias: str | None = None, addition_accepts: None = None, validators: list[Callable[[TInput1], bool]] | None = None, @@ -187,7 +188,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): mode: Literal[MatchMode.KEEP], previous: BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], addition_accepts: BasePattern[Any, TInput1, Any], - origin: None = None, + origin: Any = None, alias: str | None = None, accepts: None = None, validators: list[Callable[[TInput1], bool]] | None = None, @@ -200,11 +201,10 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): accepts: type[TInput1], previous: BasePattern[TInput1 | TInput2, TInput1 | TInput2, Literal[MatchMode.VALUE_OPERATE]], addition_accepts: BasePattern[Any, TInput2, Any], - origin: None = None, + origin: Any = None, alias: str | None = None, validators: list[Callable[[TInput1 | TInput2], bool]] | None = None, ): ... - @overload def __init__( self: BasePattern[str, str, Literal[MatchMode.REGEX_MATCH]], @@ -244,7 +244,6 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): addition_accepts: None = None, validators: list[Callable[[str], bool]] | None = None, ): ... - @overload def __init__( self: BasePattern[TOrigin, str | TOrigin, Literal[MatchMode.REGEX_CONVERT]], @@ -287,14 +286,13 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): addition_accepts: None = None, validators: list[Callable[[TOrigin], bool]] | None = None, ): ... - @overload def __init__( self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], *, mode: Literal[MatchMode.TYPE_CONVERT], - origin: type[TOrigin], - converter: Callable[[BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], Any], TOrigin | None] | None = None, + converter: Callable[[BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], Any], TOrigin | None], + origin: None = None, alias: str | None = None, previous: None = None, accepts: None = None, @@ -303,38 +301,64 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): ): ... @overload def __init__( - self: BasePattern[TOrigin, TInput1 | Any, Literal[MatchMode.TYPE_CONVERT]], + self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], *, mode: Literal[MatchMode.TYPE_CONVERT], - origin: type[TOrigin], - previous: BasePattern[Any, TInput1, Literal[MatchMode.TYPE_CONVERT]], - converter: Callable[[BasePattern[TOrigin, TInput1 | Any, Literal[MatchMode.TYPE_CONVERT]], Any], TOrigin | None] | None = None, + accepts: type[TInput1], + converter: Callable[[BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None], + origin: None = None, alias: str | None = None, - accepts: None = None, + previous: None = None, addition_accepts: None = None, validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload def __init__( - self: BasePattern[TOrigin, TInput1 | Any, Literal[MatchMode.TYPE_CONVERT]], + self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + addition_accepts: BasePattern[Any, TInput1, Any], + converter: Callable[[BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None], + origin: None = None, + alias: str | None = None, + previous: None = None, + accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + accepts: type[TInput1], + addition_accepts: BasePattern[Any, TInput2, Any], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput2], TOrigin | None], + origin: None = None, + alias: str | None = None, + previous: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], *, mode: Literal[MatchMode.TYPE_CONVERT], origin: type[TOrigin], - previous: BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], - converter: Callable[[BasePattern[TOrigin, TInput1 | Any, Literal[MatchMode.TYPE_CONVERT]], TInput1 | Any], TOrigin | None] | None = None, + converter: Callable[[BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], Any], TOrigin | None] | None = None, alias: str | None = None, + previous: None = None, accepts: None = None, addition_accepts: None = None, validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload def __init__( - self: BasePattern[TOrigin, TInput, Literal[MatchMode.TYPE_CONVERT]], + self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], *, mode: Literal[MatchMode.TYPE_CONVERT], origin: type[TOrigin], - accepts: type[TInput], - converter: Callable[[BasePattern[TOrigin, TInput, Literal[MatchMode.REGEX_CONVERT]], TInput], TOrigin | None] | None = None, + accepts: type[TInput1], + converter: Callable[[BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None] | None = None, alias: str | None = None, previous: None = None, addition_accepts: None = None, @@ -368,15 +392,93 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload + def __init__( + self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + previous: BasePattern[TInput1, Any, Literal[MatchMode.TYPE_CONVERT]], + converter: Callable[[BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], TInput1 | Any], TOrigin | None], + alias: str | None = None, + origin: None = None, + accepts: None = None, + addition_accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + origin: type[TOrigin], + previous: BasePattern[TInput1, Any, Literal[MatchMode.TYPE_CONVERT]], + converter: Callable[[BasePattern[TOrigin, Any, Literal[MatchMode.TYPE_CONVERT]], TInput1 | Any], TOrigin | None] | None = None, + alias: str | None = None, + accepts: None = None, + addition_accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + previous: BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], + converter: Callable[[BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None], + alias: str | None = None, + origin: None = None, + accepts: None = None, + addition_accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, TOrigin | TInput1, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + origin: type[TOrigin], + previous: BasePattern[TOrigin | TInput1, TOrigin | TInput1, Literal[MatchMode.VALUE_OPERATE]] | BasePattern[TOrigin, TOrigin, Literal[MatchMode.VALUE_OPERATE]] | BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], + converter: Callable[[BasePattern[TOrigin, TOrigin | TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None] | None = None, + alias: str | None = None, + accepts: None = None, + addition_accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + accepts: type[TInput1], + previous: BasePattern[TInput4 | TInput1, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput4], TOrigin | None], + alias: str | None = None, + origin: None = None, + addition_accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload def __init__( self: BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], *, mode: Literal[MatchMode.TYPE_CONVERT], origin: type[TOrigin], accepts: type[TInput1], - previous: BasePattern[TInput1, TInput2, Literal[MatchMode.TYPE_CONVERT]], - converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None] | None = None, + previous: BasePattern[TInput4 | TInput1, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput4], TOrigin | None] | None = None, + alias: str | None = None, + addition_accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload + def __init__( + self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + accepts: type[TInput1], + previous: BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], + converter: Callable[[BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None], alias: str | None = None, + origin: None = None, addition_accepts: None = None, validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @@ -394,19 +496,45 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload + def __init__( + self: BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + addition_accepts: BasePattern[Any, TInput1, Any], + previous: BasePattern[TInput4 | TInput1, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput4], TOrigin | None], + alias: str | None = None, + origin: None = None, + accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload def __init__( self: BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], *, mode: Literal[MatchMode.TYPE_CONVERT], origin: type[TOrigin], addition_accepts: BasePattern[Any, TInput1, Any], - previous: BasePattern[TInput1, TInput2, Literal[MatchMode.TYPE_CONVERT]], - converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None] | None = None, + previous: BasePattern[TInput4 | TInput1, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput4], TOrigin | None] | None = None, alias: str | None = None, accepts: None = None, validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload + def __init__( + self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + addition_accepts: BasePattern[Any, TInput1, Any], + previous: BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], + converter: Callable[[BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], TInput1], TOrigin | None], + alias: str | None = None, + origin: None = None, + accepts: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload def __init__( self: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], *, @@ -420,6 +548,19 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload + def __init__( + self: BasePattern[TOrigin, TInput1 | TInput2 | TInput3, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + accepts: type[TInput1], + previous: BasePattern[TInput1 | TInput3, TInput2, Literal[MatchMode.TYPE_CONVERT]] | BasePattern[TInput1, TInput2, Literal[MatchMode.TYPE_CONVERT]] | BasePattern[TInput3, TInput2, Literal[MatchMode.TYPE_CONVERT]], + addition_accepts: BasePattern[Any, TInput3, Any], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput2 | TInput3, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput3], TOrigin | None], + alias: str | None = None, + origin: None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload def __init__( self: BasePattern[TOrigin, TInput1 | TInput2 | TInput3, Literal[MatchMode.TYPE_CONVERT]], *, @@ -434,6 +575,19 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): validators: list[Callable[[TOrigin], bool]] | None = None, ): ... @overload + def __init__( + self: BasePattern[TOrigin, TInput1 | TInput3, Literal[MatchMode.TYPE_CONVERT]], + *, + mode: Literal[MatchMode.TYPE_CONVERT], + accepts: type[TInput1], + previous: BasePattern[TInput1 | TInput3, TInput1 | TInput3, Literal[MatchMode.VALUE_OPERATE]] | BasePattern[TInput3, TInput3, Literal[MatchMode.VALUE_OPERATE]] | BasePattern[TInput1, TInput1, Literal[MatchMode.VALUE_OPERATE]], + addition_accepts: BasePattern[Any, TInput3, Any], + converter: Callable[[BasePattern[TOrigin, TInput1 | TInput3, Literal[MatchMode.TYPE_CONVERT]], TInput1 | TInput3], TOrigin | None], + origin: None = None, + alias: str | None = None, + validators: list[Callable[[TOrigin], bool]] | None = None, + ): ... + @overload def __init__( self: BasePattern[TOrigin, TInput1 | TInput3, Literal[MatchMode.TYPE_CONVERT]], *, @@ -453,7 +607,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): *, mode: Literal[MatchMode.VALUE_OPERATE], origin: type[TOrigin], - converter: Callable[[BasePattern[TOrigin, TOrigin, Literal[MatchMode.VALUE_OPERATE]], TOrigin], TOrigin | None] | None = None, + converter: Callable[[BasePattern[TOrigin, TOrigin, Literal[MatchMode.VALUE_OPERATE]], TOrigin], TOrigin | None], alias: str | None = None, previous: None = None, accepts: None = None, @@ -467,7 +621,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): mode: Literal[MatchMode.VALUE_OPERATE], origin: type[TOrigin], previous: BasePattern[TOrigin, TInput1, Literal[MatchMode.TYPE_CONVERT]], - converter: Callable[[BasePattern[TOrigin, TOrigin | TInput1, Literal[MatchMode.VALUE_OPERATE]], TOrigin], TOrigin | None] | None = None, + converter: Callable[[BasePattern[TOrigin, TOrigin | TInput1, Literal[MatchMode.VALUE_OPERATE]], TOrigin], TOrigin | None], alias: str | None = None, accepts: None = None, addition_accepts: None = None, @@ -480,7 +634,7 @@ class BasePattern(Generic[TOrigin, TInput, TMM]): mode: Literal[MatchMode.VALUE_OPERATE], origin: type[TOrigin], previous: BasePattern[TOrigin, TOrigin, Literal[MatchMode.VALUE_OPERATE]], - converter: Callable[[BasePattern[TOrigin, TOrigin, Literal[MatchMode.VALUE_OPERATE]], TOrigin], TOrigin | None] | None = None, + converter: Callable[[BasePattern[TOrigin, TOrigin, Literal[MatchMode.VALUE_OPERATE]], TOrigin], TOrigin | None], alias: str | None = None, accepts: None = None, addition_accepts: None = None, diff --git a/nepattern/main.py b/nepattern/main.py index 8386e3a..b4413e3 100644 --- a/nepattern/main.py +++ b/nepattern/main.py @@ -91,8 +91,8 @@ def parser(item: Any, extra: str = "allow") -> BasePattern: raise TypeError(f"{item} can only accept 1 or 2 argument") anno = list(sig.parameters.values())[-1].annotation return BasePattern( - accepts=Any if anno == inspect.Signature.empty else anno, - origin=(Any if sig.return_annotation == inspect.Signature.empty else sig.return_annotation), + accepts=Any if anno == inspect.Signature.empty else anno, # type: ignore + origin=(Any if sig.return_annotation == inspect.Signature.empty else sig.return_annotation), # type: ignore converter=item if len(sig.parameters) == 2 else lambda _, x: item(x), mode=MatchMode.TYPE_CONVERT, ) diff --git a/pyproject.toml b/pyproject.toml index 4841882..a151677 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nepattern" -version = "0.7.0" +version = "0.7.1" description = "a complex pattern, support typing" authors = [ {name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com"}, diff --git a/test.py b/test.py index f593adc..d60b2af 100644 --- a/test.py +++ b/test.py @@ -176,6 +176,7 @@ def convert(self, content): pat5_4 = BasePattern( mode=MatchMode.TYPE_CONVERT, origin=int, + accepts=str, converter=convert, previous=prev, )