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
27 changes: 27 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,30 @@ def main(names: list[str] = typer.Option(None)):
result = runner.invoke(app, [], default_map={"names": "not-a-list"})
assert result.exit_code == 2
assert "Invalid value" in result.output


def test_count_option_help_no_metavar() -> None:
"""Regression test for https://github.com/fastapi/typer/issues/1869.

count=True options should not show INTEGER in help since they don't take a value.
"""
app = typer.Typer()

@app.command()
def main(verbose: int = typer.Option(0, count=True)):
"""Count verbosity."""
typer.echo(f"verbose={verbose}")

result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
# The help must not show INTEGER for a count option
assert "INTEGER" not in result.output
assert "--verbose" in result.output

# Behaviour: --verbose increments
assert runner.invoke(app, ["--verbose"]).exit_code == 0

# Behaviour: --verbose 3 is rejected (count doesn't accept a value)
result = runner.invoke(app, ["--verbose", "3"])
assert result.exit_code == 2
assert "unexpected extra argument" in result.output
2 changes: 2 additions & 0 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ def _extract_default_help_str(
return _extract_default_help_str(self, ctx=ctx)

def make_metavar(self, ctx: _click.Context) -> str:
if self.count:
return ""
return super().make_metavar(ctx=ctx)

def get_help_record(self, ctx: _click.Context) -> tuple[str, str] | None:
Expand Down
3 changes: 2 additions & 1 deletion typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,10 @@ def _print_options_panel(
types_data = Text(style=STYLE_TYPES, overflow="fold")

# Fetch type
is_count_option: bool = isinstance(param, TyperOption) and param.count
if metavar_type and metavar_type != "BOOLEAN":
types_data.append(metavar_type)
else:
elif not is_count_option:
type_str = param.type.name.upper()
if type_str != "BOOLEAN":
types_data.append(type_str)
Expand Down
Loading