Skip to content

Show Literal choices in --help inside Optional and tuple (#375) - #381

Open
Eliot5566 wants to merge 1 commit into
lebrice:masterfrom
Eliot5566:fix/literal-choices-metavar
Open

Eliot5566 wants to merge 1 commit into
lebrice:masterfrom
Eliot5566:fix/literal-choices-metavar

Conversation

@Eliot5566

Copy link
Copy Markdown

Fixes #375.

get_metavar() had no case for Literal, so a Literal nested in Optional, tuple[..., ...] or a Union showed up in --help as Literal. It now returns the choices in argparse's {a,b,c} form, with Enum members by name as FieldWrapper.choices does. The Optional, tuple and Union branches already call get_metavar() on their arguments, so they pick it up:

--maybe_color [{red,green,blue}]
--colors {red,green,blue} [{red,green,blue}, ...]
--shade {light,dark}|{auto}

A plain Literal field is unchanged, since that path drops the metavar and lets argparse print the choices. list[Literal[...]] still shows list; the issue says that one is reported separately.

The new test in test_literal.py fails on master and passes with the change. On Python 3.11 the full suite gives 1267 passed and one failure, test_bool_args_in_help, which fails on master for me too.

get_metavar() had no case for Literal, so `Literal[...] | None` and
`tuple[Literal[...], ...]` showed the type name "Literal" in --help.
It now returns the choices in argparse's "{a,b,c}" form (Enum members by
name), and the Optional and tuple cases reuse it through recursion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--help doesn't list a Literal's choices inside Optional or tuple

1 participant