Skip to content

Commit

Permalink
fix: Literal contained inside non-variadic tuple should not imply "ch…
Browse files Browse the repository at this point in the history
…oices".
  • Loading branch information
DanCardin committed Nov 8, 2024
1 parent fd2fb92 commit f1e0318
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/cappa/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions tests/arg/test_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f1e0318

Please sign in to comment.