Skip to content

💥 Update metavar printing#1863

Open
svlandeg wants to merge 50 commits into
fastapi:masterfrom
svlandeg:feat/metavar
Open

💥 Update metavar printing#1863
svlandeg wants to merge 50 commits into
fastapi:masterfrom
svlandeg:feat/metavar

Conversation

@svlandeg

@svlandeg svlandeg commented Jun 27, 2026

Copy link
Copy Markdown
Member

Description

We've decided to change a bit the "metavar" printing functionality of Typer. In short, the new rules are:

  • consistently use <> for displaying the type (including enum choices etc)
  • [] for displaying something optional in the usage string
  • {} for referencing an object (i.e. not a literal string) in the usage string if neither of the other 2 apply
  • AVOID ALL CAPS

Breaking changes

  • metavar types now show up as the actual Python type and in <>, so "<str>" instead of "TEXT" and "<int>" instead of "INTEGER" etc.
  • Choices follow the same format so <simple|conv|lstm> instead of [simple|conv|lstm] or {simple|conv|lstm>}. Check the new test test_param_type_help_metavar for an overview.
  • parameter names are not magically lowercased or uppercased, they are just kept as declared in the Python code. cf. new test test_parameter_name_casing.
  • In the help usage strings, parameter/metavar names are now put between brackets{}, unless they're already surrounded with the "optional" denoting brackets [].
  • In the list of "Arguments:", no brackets are printed around the names (on master, optional ones were enclosed with [] which felt inconsistent with optional options)

Edits in custom.js

When updating the documentation to show e.g. <str> instead of TEXT, this would be treated as an HTML tag and not actually displayed in our tutorial pages 😭 Hence the fixes to custom.js.

Tangiantel fixes

  • While updating all docs, I realised that we didn't update them when we merged the fix to not display Default: None in the help text, PR 1120. I updated those as part of the documentation update here. I could also do this in a separate PR if preferred, but it would cause a lot of merge conflicts and duplicate work as I've now been going through all the docs in detail anyway.
  • Similarly, some docs still needed updating after PR 944 :hide-the-pain-emoji:
  • I found some other issues with the docs while going through everything in detail, and decided to fix those in this PR as well. Things like defaults not displayed in the example output, or the Arguments list missing, etc. I'm guessing these were mostly manual copy/paste errors from the past 🤷‍♀️

How to review this PR

Please mostly look at the test/documentation changes, and not so much the actual code changes in the Python files.

The Python edits will become obsolete with the Pydantic refactor that is in the works. In fact, this PR is a "spinoff" of the Pydantic refactor (#1831, WIP) in an attempt to make that one more "reviewable". Only the docs & tests changes will be brought over from here - the code changes from this PR will be subsumed by PR 1831.

AI Disclaimer

Created through pair-programming with Cursor, manually reviewed in detail. Also went through all the Typer docs manually & in detail.

Checklist

  • I added tests for the change.
  • The new or updated tests fail on the main branch and pass on this PR.
  • Coverage stays at 100%.
  • The documentation/tutorial has been updated to reflect the new help formatting

@svlandeg svlandeg self-assigned this Jun 27, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

📝 Docs preview

Last commit 00e76e0 at: https://57e738c4.typertiangolo.pages.dev

Modified Pages

@app.command()
def main(
id: Annotated[int, typer.Argument(min=0, max=1000)],
ID: Annotated[int, typer.Argument(min=0, max=1000)],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I made this one uppercase on purpose to show it's possible and to have as part of our test suite. And because seeing it displayed as "ID" instead of "id" is what you'd expect.

@svlandeg svlandeg marked this pull request as ready for review July 14, 2026 18:46
@svlandeg svlandeg removed their assignment Jul 14, 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.

2 participants