Skip to content

Commit

Permalink
🐛 version 0.7.1
Browse files Browse the repository at this point in the history
fix type_convert logic
  • Loading branch information
RF-Tar-Railt committed Apr 23, 2024
1 parent f2bc481 commit b50cc35
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 63 deletions.
2 changes: 1 addition & 1 deletion nepattern/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__:
Expand Down
69 changes: 39 additions & 30 deletions nepattern/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand Down Expand Up @@ -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_)):
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Loading

0 comments on commit b50cc35

Please sign in to comment.