Repository navigation
Undocumented behavior (or bug) for default option name with metavar #1980
First Check
Example Codefrom typing import Annotated
import typer
def main(
example: Annotated[str, typer.Option(metavar="EXAMPLE")] = None,
):
pass
if __name__ == "__main__":
typer.run(main)DescriptionWhen I write a typer option, I expect the default option name to match the casing of the function parameters. For example, if I have a function parameter named However, in some circumstances, the casing of the option name seems to be inherited from metavar, rather than the function parameter. As best I can tell, this requires all of the following:
For the code snippet above, I'd expect to invoke the "example" option using you get an error I didn't see any mention of this in the option naming docs, so if this is intended behavior, then it would be helpful to mention it there. Operating SystemLinux, Windows Operating System DetailsNo response Project Version0.27.3 Python Version3.14.4 Additional ContextUses rich v15.0.0 |
Replies: 1 comment 1 reply
|
This is intentional since 0.27.0. It came in with PR #1863 ("Update metavar printing"), which stopped Typer from lowercasing or uppercasing parameter names. When no option name is given, the flag is built by def get_default_option_flag_name(name: str, metavar: str | None) -> str:
flag_name = name.replace("_", "-")
if metavar is not None:
meta_flag = metavar.replace("_", "-")
if meta_flag.lower() == flag_name.lower():
return meta_flag
return flag_nameSo when the metavar matches the parameter name case-insensitively, the metavar's spelling wins. You're right that the option naming docs don't mention it. To keep def main(
example: Annotated[str, typer.Option("--example", metavar="EXAMPLE")] = None,
):
...Or use a metavar that isn't a case-insensitive match for the parameter name. A docs PR adding a note to the option name page would probably be welcome. |
This is intentional since 0.27.0. It came in with PR #1863 ("Update metavar printing"), which stopped Typer from lowercasing or uppercasing parameter names. When no option name is given, the flag is built by
get_default_option_flag_nameintyper/main.py:So when the metavar matches the parameter name case-insensitively, the metavar's spelling wins.
tests/test_core.py::test_parameter_name_casingtests this d…