From f1e03182ede3fa132c895bef761f7a95d9e88e2e Mon Sep 17 00:00:00 2001 From: DanCardin Date: Fri, 8 Nov 2024 18:34:12 -0500 Subject: [PATCH] fix: Literal contained inside non-variadic tuple should not imply "choices". --- CHANGELOG.md | 4 ++++ src/cappa/typing.py | 2 +- tests/arg/test_choices.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 745b6b9..a741da2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.24 +### 0.24.2 + +- fix: Literal contained inside non-variadic tuple should not imply "choices". + ### 0.24.1 - feat: Add Group.section to enable ordering of groups separately from the items within them. diff --git a/src/cappa/typing.py b/src/cappa/typing.py index f2c0fe6..95dfcb0 100644 --- a/src/cappa/typing.py +++ b/src/cappa/typing.py @@ -56,7 +56,7 @@ def detect_choices(type_view: TypeView) -> list[str] | None: if type_view.is_subclass_of(enum.Enum): return [v.value for v in type_view.annotation] - if type_view.is_subclass_of((tuple, list, set)): + if type_view.is_subclass_of((list, set)) or type_view.is_variadic_tuple: type_view = type_view.inner_types[0] if type_view.is_union: diff --git a/tests/arg/test_choices.py b/tests/arg/test_choices.py index a7bed18..b9b70fc 100644 --- a/tests/arg/test_choices.py +++ b/tests/arg/test_choices.py @@ -42,3 +42,33 @@ class ArgTest: result = capsys.readouterr().out assert "Valid options: one, two." in result + + +@backends +def test_variadic_tuple_choices(backend, capsys): + @dataclass + class ArgTest: + choice: Annotated[ + tuple[Literal["one", "two"], ...] | None, cappa.Arg(short=True) + ] = None + + with pytest.raises(cappa.HelpExit): + parse(ArgTest, "--help", backend=backend) + + result = capsys.readouterr().out + assert "Valid options: one, two." in result + + +@backends +def test_tuple_choices(backend, capsys): + @dataclass + class ArgTest: + choice: Annotated[ + tuple[Literal["one", "two"], int] | None, cappa.Arg(short=True) + ] = None + + with pytest.raises(cappa.HelpExit): + parse(ArgTest, "--help", backend=backend) + + result = capsys.readouterr().out + assert "Valid options: one, two." not in result