Skip to content

fix: show the default for options whose default is an empty string - #1987

Closed
Eapp1e wants to merge 2 commits into
fastapi:masterfrom
Eapp1e:fix/show-default-empty-string
Closed

Eapp1e wants to merge 2 commits into
fastapi:masterfrom
Eapp1e:fix/show-default-empty-string

Conversation

@Eapp1e

@Eapp1e Eapp1e commented Oct 25, 2026

Copy link
Copy Markdown

What

typer.core._get_default_string is a copy of Click's default-string formatting with the parts Click special-cases for Typer. The empty string case is missing, so an option whose default is deliberately "" prints no default at all:

@app.command()
def main(name: str = typer.Option("", help="Name to greet", show_default=True)) -> None:
    ...

Click 8 renders Name to greet [default: ""], Typer renders Name to greet. An empty default is indistinguishable from "this option has no default to show", which is exactly the case a help string usually needs to spell out — the reader cannot tell whether --name will be empty or absent.

How

Add the branch Click has, before the None check:

    elif default_value == "":
        default_string = '""'
    elif default_value is None:
        default_string = ""

Both the Rich help formatter (rich_utils._get_parameter_help) and the plain Click one read this helper, so the two stay consistent.

Tests

tests/test_rich_utils.py::test_rich_utils_show_default_empty_string renders the command above and asserts [default: ""] appears in the help output. It fails on the current implementation and passes with the change; the rest of the file still passes (13 passed).

Prepared with the help of an AI assistant.

Eapp1e and others added 2 commits October 25, 2026 11:57
_get_default_string is a copy of Click's formatting with the cases Click
special-cases for Typer. The empty string branch was not carried over, so
Option("", show_default=True) printed no default at all and an option whose
default is deliberately empty looked like an option without one.

Restore the branch so the help output matches Click, and cover it with a
test.
@Eapp1e

Eapp1e commented Oct 25, 2026

Copy link
Copy Markdown
Author

Closing this to avoid leaving a stale pull request open. The change itself is small and the branch stays in my fork, so it can be revived if maintainers want it later.

@Eapp1e Eapp1e closed this Oct 25, 2026
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.

2 participants