Skip to content

Commit 5177ccb

Browse files
chore: review comment and test
1 parent e8827ae commit 5177ccb

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

cmd2/annotated.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ def _resolve_parameters(
635635
if name in skip_params:
636636
continue
637637

638+
if param.kind == inspect.Parameter.POSITIONAL_ONLY:
639+
raise TypeError(
640+
f"Parameter {name!r} in {func.__qualname__} is positional-only, "
641+
"which is not supported by @with_annotated because parameters are passed as keyword arguments."
642+
)
643+
638644
if name in _RESERVED_PARAM_NAMES:
639645
raise ValueError(
640646
f"Parameter name {name!r} in {func.__qualname__} is reserved by argparse "

tests/test_annotated.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def _func_grouped(
117117
) -> None: ...
118118

119119

120+
def _func_positional_only(self, name: str, /) -> None: ...
121+
122+
120123
def _provider(cmd: cmd2.Cmd):
121124
return []
122125

@@ -315,6 +318,10 @@ def func(self, subcommand: str) -> None: ...
315318
with pytest.raises(ValueError, match="subcommand"):
316319
build_parser_from_function(func)
317320

321+
def test_with_annotated_positional_only_param_raises(self) -> None:
322+
with pytest.raises(TypeError, match="positional-only"):
323+
build_parser_from_function(_func_positional_only)
324+
318325
def test_optional_annotated_outside_raises(self) -> None:
319326
with pytest.raises(TypeError, match="Annotated"):
320327
build_parser_from_function(_func_optional_annotated_outside)

0 commit comments

Comments
 (0)