Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_ambiguous_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,21 @@ def cmd(
)
def test_error_rendering(error, message):
assert str(error) == message


def test_keyword_only_without_default_becomes_option():
app = typer.Typer()

@app.command()
def cmd(*, my_param):
print(my_param)

# Check that it works with the keyword
result = runner.invoke(app, ["--my-param", "hello"])
assert result.exit_code == 0, result.output
assert "hello" in result.output

# Check that it fails without the keyword
result = runner.invoke(app, ["hello"])
assert result.exit_code != 0, result.output
assert "Missing option" in result.output, result.output
5 changes: 4 additions & 1 deletion typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,10 @@ def get_click_param(
default_value = parameter_info.default
elif param.default == Required or param.default is param.empty:
required = True
parameter_info = ArgumentInfo()
if param.kind == inspect.Parameter.KEYWORD_ONLY:
parameter_info = OptionInfo()
else:
parameter_info = ArgumentInfo()
else:
default_value = param.default
parameter_info = OptionInfo()
Expand Down
2 changes: 2 additions & 0 deletions typer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,12 @@ def __init__(
name: str,
default: Any = inspect.Parameter.empty,
annotation: Any = inspect.Parameter.empty,
kind: inspect._ParameterKind = inspect.Parameter.POSITIONAL_OR_KEYWORD,
) -> None:
self.name = name
self.default = default
self.annotation = annotation
self.kind = kind


class DeveloperExceptionConfig:
Expand Down
2 changes: 1 addition & 1 deletion typer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ def get_params_from_function(func: Callable[..., Any]) -> Dict[str, ParamMeta]:
default = parameter_info

params[param.name] = ParamMeta(
name=param.name, default=default, annotation=annotation
name=param.name, default=default, annotation=annotation, kind=param.kind
)
return params
Loading