From 72d762da63e68927d464e9d44ea004627abae5ef Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt Date: Tue, 6 Aug 2024 16:03:13 +0800 Subject: [PATCH] :bug: version 1.8.23 fix Arg's type --- CHANGELOG.md | 10 ++++++++++ devtool.py | 8 ++++---- src/arclet/alconna/__init__.py | 2 +- src/arclet/alconna/_internal/_header.py | 2 +- src/arclet/alconna/args.py | 7 +++---- src/arclet/alconna/base.py | 3 ++- src/arclet/alconna/typing.py | 10 +++++----- tests/devtool.py | 8 ++++---- 8 files changed, 30 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81b9b7bb..54ea4cee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # 更新日志 +## 1.8.23 + +### 改进 + +- `Option` 名字设置成 `--` 时不会去除前缀 + +### 修复 + +- `Arg` 等类型参数的构造函数中的类型修复 + ## 1.8.22 ### 修复 diff --git a/devtool.py b/devtool.py index 386c5e29..48b5a6b6 100644 --- a/devtool.py +++ b/devtool.py @@ -46,7 +46,7 @@ def analyse_args( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space) + argv: Argv[DataCollection] = Argv(meta, dev_space) try: argv.enter(kwargs) argv.build(["test"] + command) @@ -69,7 +69,7 @@ def analyse_header( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space, separators=(sep,)) + argv: Argv[DataCollection] = Argv(meta, dev_space, separators=(sep,)) command_header = Header.generate(command_name, headers, compact=compact) try: argv.enter(kwargs) @@ -89,7 +89,7 @@ def analyse_option( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space) + argv: Argv[DataCollection] = Argv(meta, dev_space) _analyser = _DummyAnalyser.__new__(_DummyAnalyser) _analyser.reset() _analyser.command.separators = (" ",) @@ -116,7 +116,7 @@ def analyse_subcommand( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space) + argv: Argv[DataCollection] = Argv(meta, dev_space) _analyser = _DummyAnalyser.__new__(_DummyAnalyser) _analyser.reset() _analyser.command.separators = (" ",) diff --git a/src/arclet/alconna/__init__.py b/src/arclet/alconna/__init__.py index f47b826b..0f6e694f 100644 --- a/src/arclet/alconna/__init__.py +++ b/src/arclet/alconna/__init__.py @@ -52,7 +52,7 @@ from .typing import Up as Up from .typing import StrMulti as StrMulti -__version__ = "1.8.22" +__version__ = "1.8.23" # backward compatibility AnyOne = ANY diff --git a/src/arclet/alconna/_internal/_header.py b/src/arclet/alconna/_internal/_header.py index b9514472..7f36bc68 100644 --- a/src/arclet/alconna/_internal/_header.py +++ b/src/arclet/alconna/_internal/_header.py @@ -307,7 +307,7 @@ def generate( _prefixes: list[tuple[Any, str]] = prefixes # type: ignore return cls( (command, prefixes), - [Pair(h[0], re.compile(re.escape(h[1]) + _cmd) if to_regex else h[1] + _cmd) for h in _prefixes], + [Pair(h[0], re.compile(re.escape(h[1]) + _cmd)) if to_regex else Pair(h[0], h[1] + _cmd) for h in _prefixes], mapping, compact, ) diff --git a/src/arclet/alconna/args.py b/src/arclet/alconna/args.py index f0922568..4ff2bd6b 100644 --- a/src/arclet/alconna/args.py +++ b/src/arclet/alconna/args.py @@ -6,14 +6,14 @@ import sys from enum import Enum from functools import partial -from typing import Any, Callable, Generic, Iterable, List, Sequence, Type, TypeVar, Union, cast -from typing_extensions import Self, TypeAlias +from typing import Any, Callable, Generic, Iterable, List, Sequence, TypeVar, Union, cast +from typing_extensions import Self from nepattern import ANY, NONE, AntiPattern, BasePattern, MatchMode, RawStr, UnionPattern, parser from tarina import Empty, get_signature, lang from .exceptions import InvalidArgs -from .typing import AllParam, KeyWordVar, KWBool, MultiKeyWordVar, MultiVar, UnpackVar +from .typing import TAValue, AllParam, KeyWordVar, KWBool, MultiKeyWordVar, MultiVar, UnpackVar def safe_dcls_kw(**kwargs): @@ -23,7 +23,6 @@ def safe_dcls_kw(**kwargs): _T = TypeVar("_T") -TAValue: TypeAlias = Union[BasePattern[_T, Any, Any], Type[_T], str] class ArgFlag(str, Enum): diff --git a/src/arclet/alconna/base.py b/src/arclet/alconna/base.py index b35af60c..6037d165 100644 --- a/src/arclet/alconna/base.py +++ b/src/arclet/alconna/base.py @@ -112,7 +112,8 @@ def __init__( _handle_default(self) self.separators = (" ",) if separators is None else ((separators,) if isinstance(separators, str) else tuple(separators)) # noqa: E501 self.nargs = len(self.args.argument) - self.dest = (dest or (("_".join(self.requires) + "_") if self.requires else "") + self.name.lstrip("-")).lstrip("-") # noqa: E501 + self.dest = (dest or (("_".join(self.requires) + "_") if self.requires else "") + (self.name.lstrip("-") or self.name)) # noqa: E501 + self.dest = self.dest.lstrip("-") or self.dest self.help_text = help_text or self.dest self._hash = self._calc_hash() diff --git a/src/arclet/alconna/typing.py b/src/arclet/alconna/typing.py index 3b5c7c17..16173c23 100644 --- a/src/arclet/alconna/typing.py +++ b/src/arclet/alconna/typing.py @@ -17,7 +17,7 @@ TypeVar, Union, final, - runtime_checkable, + runtime_checkable, Type, Callable, ) from typing_extensions import NotRequired, TypeAlias @@ -26,7 +26,6 @@ TPrefixes = Union[List[Union[str, object]], List[Tuple[object, str]]] DataUnit = TypeVar("DataUnit", covariant=True) - class _ShortcutRegWrapper(Protocol): def __call__(self, slot: int | str, content: str | None, context: dict[str, Any]) -> Any: ... @@ -145,6 +144,7 @@ class CommandMeta: TDC = TypeVar("TDC", bound=DataCollection[Any]) T = TypeVar("T") +TAValue: TypeAlias = Union[BasePattern[T, Any, Any], Type[T], T, Callable[..., T], Dict[Any, T], List[T]] @final @@ -167,7 +167,7 @@ class KeyWordVar(BasePattern[T, Any, Literal[MatchMode.KEEP]]): base: BasePattern - def __init__(self, value: BasePattern[T, Any, Any] | type[T], sep: str = "="): + def __init__(self, value: TAValue[T], sep: str = "="): """构建一个具名参数 Args: @@ -186,7 +186,7 @@ def __repr__(self): class _Kw: __slots__ = () - def __getitem__(self, item: BasePattern[T, Any, Any] | type[T]): + def __getitem__(self, item: BasePattern[T, Any, Any] | type[T] | Any): return KeyWordVar(item) __matmul__ = __getitem__ @@ -200,7 +200,7 @@ class MultiVar(BasePattern[T, Any, Literal[MatchMode.KEEP]]): flag: Literal["+", "*"] length: int - def __init__(self, value: BasePattern[T, Any, Any] | type[T], flag: int | Literal["+", "*"] = "+"): + def __init__(self, value: TAValue[T], flag: int | Literal["+", "*"] = "+"): """构建一个可变参数 Args: diff --git a/tests/devtool.py b/tests/devtool.py index 386c5e29..48b5a6b6 100644 --- a/tests/devtool.py +++ b/tests/devtool.py @@ -46,7 +46,7 @@ def analyse_args( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space) + argv: Argv[DataCollection] = Argv(meta, dev_space) try: argv.enter(kwargs) argv.build(["test"] + command) @@ -69,7 +69,7 @@ def analyse_header( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space, separators=(sep,)) + argv: Argv[DataCollection] = Argv(meta, dev_space, separators=(sep,)) command_header = Header.generate(command_name, headers, compact=compact) try: argv.enter(kwargs) @@ -89,7 +89,7 @@ def analyse_option( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space) + argv: Argv[DataCollection] = Argv(meta, dev_space) _analyser = _DummyAnalyser.__new__(_DummyAnalyser) _analyser.reset() _analyser.command.separators = (" ",) @@ -116,7 +116,7 @@ def analyse_subcommand( **kwargs ): meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style) - argv = Argv(meta, dev_space) + argv: Argv[DataCollection] = Argv(meta, dev_space) _analyser = _DummyAnalyser.__new__(_DummyAnalyser) _analyser.reset() _analyser.command.separators = (" ",)