Skip to content

Commit be30028

Browse files
committed
Treat kwarg_only as OptionInfo
1 parent 3b17788 commit be30028

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

tests/test_ambiguous_params.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,21 @@ def cmd(
229229
)
230230
def test_error_rendering(error, message):
231231
assert str(error) == message
232+
233+
234+
def test_keyword_only_without_default_becomes_option():
235+
app = typer.Typer()
236+
237+
@app.command()
238+
def cmd(*, my_param):
239+
print(my_param)
240+
241+
# Check that it works with the keyword
242+
result = runner.invoke(app, ["--my-param", "hello"])
243+
assert result.exit_code == 0, result.output
244+
assert "hello" in result.output
245+
246+
# Check that it fails without the keyword
247+
result = runner.invoke(app, ["hello"])
248+
assert result.exit_code != 0, result.output
249+
assert "Missing option" in result.output, result.output

typer/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,10 @@ def get_click_param(
823823
default_value = parameter_info.default
824824
elif param.default == Required or param.default is param.empty:
825825
required = True
826-
parameter_info = ArgumentInfo()
826+
if param.kind == inspect.Parameter.KEYWORD_ONLY:
827+
parameter_info = OptionInfo()
828+
else:
829+
parameter_info = ArgumentInfo()
827830
else:
828831
default_value = param.default
829832
parameter_info = OptionInfo()

typer/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,12 @@ def __init__(
514514
name: str,
515515
default: Any = inspect.Parameter.empty,
516516
annotation: Any = inspect.Parameter.empty,
517+
kind: inspect.Parameter = inspect.Parameter.POSITIONAL_OR_KEYWORD,
517518
) -> None:
518519
self.name = name
519520
self.default = default
520521
self.annotation = annotation
522+
self.kind = kind
521523

522524

523525
class DeveloperExceptionConfig:

typer/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ def get_params_from_function(func: Callable[..., Any]) -> Dict[str, ParamMeta]:
185185
default = parameter_info
186186

187187
params[param.name] = ParamMeta(
188-
name=param.name, default=default, annotation=annotation
188+
name=param.name, default=default, annotation=annotation, kind=param.kind
189189
)
190190
return params

0 commit comments

Comments
 (0)