Skip to content

fix: Improve metavar display consistency in Rich help output#1607

Closed
by22Jy wants to merge 2 commits intofastapi:masterfrom
by22Jy:fix/metavar-rich-consistency
Closed

fix: Improve metavar display consistency in Rich help output#1607
by22Jy wants to merge 2 commits intofastapi:masterfrom
by22Jy:fix/metavar-rich-consistency

Conversation

@by22Jy
Copy link

@by22Jy by22Jy commented Feb 24, 2026

Summary

Fixes #1156

Improves the consistency of metavar display in Rich help output for Arguments with custom metavar.

Problem

When using Rich help output, Arguments with custom metavar show inconsistent behavior:

# Example with custom metavar="user"
@app.command()
def show(user: str = typer.Argument(..., metavar="user")):
    ...

Before (confusing):

Arguments:
  *    user      MY_ARG  [required]
       ^^^^      ^^^^^^^
       looks like param name  but is metavar

The metavar user appears in the parameter name column, making it look like a type annotation.

Solution

Smart detection of custom vs default metavar:

  • For Arguments with custom metavar: Use metavar as the parameter name, show the actual type in the type column
  • For Arguments without custom metavar: Preserve original behavior unchanged
  • Non-breaking: Only affects Arguments with custom metavar

After (correct):

Arguments:
  *    MY_ARG      TEXT  [required]
       ^^^^^^      ^^^^
       metavar as name  type shown correctly

Changes

  • Modified typer/rich_utils.py to detect custom metavar for Arguments
  • For Arguments with custom metavar: display metavar in name column, type in type column
  • Preserves all existing behavior for Options and Arguments without custom metavar

Testing

  • ✅ All 210 related tests pass
  • ✅ Tested with both required and optional arguments
  • ✅ Verified no regression for Options
  • ✅ Verified no regression for Arguments without custom metavar

Related

Checklist

  • Code follows project style guidelines
  • All tests pass
  • Non-breaking change
  • Minimal, focused changes

by22Jy and others added 2 commits February 24, 2026 13:14
Issue: fastapi#1156

Problem:
When using Rich help output, Arguments with custom metavar show inconsistent
behavior. The metavar appears in the parameter name column instead of being
displayed as the parameter name, making it look like a type annotation.

Solution:
- Smart detection of custom vs default metavar for Arguments
- For Arguments with custom metavar: use metavar as name, show type in type column
- For Arguments without custom metavar: preserve original behavior
- Non-breaking: only affects Arguments with custom metavar

Testing:
- All 210 related tests pass
- Verified with both required and optional arguments

Example:
Before (confusing):
  Arguments:
    *    user      MY_ARG  [required]  <- metavar looks like type

After (correct):
  Arguments:
    *    MY_ARG      TEXT  [required]  <- metavar as name, type shown correctly
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clarify how/why this differs from #1410, and include a meaningful unit test to showcase behaviour, thanks!

Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to close this. Feel free to review #1410 if you think something is wrong with that approach. That PR contains a lot more background information, examples, and unit tests, and was already recorded as a solution to #1156. Reviewing different PRs (especially when they're not complete, i.e. lacking unit tests) for the same issue creates too much maintenance burden.

@svlandeg svlandeg closed this Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Argument(metavar="MY_ARG") has different meaning for the arguments panel compared to the usage text or the non-rich help output

2 participants