diff --git a/README.md b/README.md index 4f64453..fb7b3f8 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,13 @@ ## 简单实例 ```python -from nepattern import BasePattern +from nepattern import Pattern -pat = BasePattern.of(int) -assert pat.validate(13).success -assert not 13.0 >> pat +pat = Pattern(str).accept(int).convert(lambda _, x: str(x)) +assert pat.execute(13).success +assert pat.execute(42).value() == '42' +assert not pat << 13.0 ``` ## 特点 diff --git a/nepattern/core.py b/nepattern/core.py index c48f4b5..4f8e2b4 100644 --- a/nepattern/core.py +++ b/nepattern/core.py @@ -208,7 +208,7 @@ def __repr__(self): def copy(self) -> Self: return deepcopy(self) - def __rrshift__(self, other): # pragma: no cover + def __lshift__(self, other): # pragma: no cover return self.execute(other) def __rmatmul__(self, other) -> Self: # pragma: no cover diff --git a/nepattern/main.py b/nepattern/main.py index 9d639d5..2e113e1 100644 --- a/nepattern/main.py +++ b/nepattern/main.py @@ -51,7 +51,7 @@ def _generic_parser(item: GenericAlias, extra: str) -> Pattern: # type: ignore if origin in _Contents: _args = {parser(t, extra) for t in get_args(item)} return (_args.pop() if len(_args) == 1 else UnionPattern(*_args)) if _args else ANY - if origin in (list, tuple, set, dict): + if origin in (list, tuple, set, dict, type, frozenset): item = origin[get_args(item)] return Pattern(origin=item, alias=f"{repr(item).split('.')[-1]}").accept(item) diff --git a/pyproject.toml b/pyproject.toml index 1b7a31c..3448216 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nepattern" -version = "0.7.6" +version = "0.8.0" 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 498efcf..82a12be 100644 --- a/test.py +++ b/test.py @@ -237,7 +237,7 @@ def test_pattern_post_validator(): def test_parser(): - from typing import Literal, Protocol, Type, TypeVar + from typing import Literal, Protocol, Type, TypeVar, Sequence from typing_extensions import Annotated pat11 = parser(int) @@ -247,7 +247,7 @@ def test_parser(): pat11_2 = parser(int) assert pat11_2 == pat11 assert isinstance(parser(Literal["a", "b"]), UnionPattern) - assert parser(Type[int]).origin is type + assert parser(Type[int]).origin == type[int] assert parser(complex) != Pattern(complex) assert isinstance(parser("a|b|c"), UnionPattern) assert isinstance(parser("re:a|b|c"), Pattern) @@ -288,6 +288,10 @@ def __setitem__(self): ... assert pat11_7.execute("abc").success assert pat11_7.execute([]).failed + pat11_8 = parser(Sequence[int]) + assert pat11_8.execute([1, 2, 3]).success + assert pat11_8.execute((1, 2, 3)).success + def test_union_pattern(): from typing import List, Optional, Union, Annotated