🔧 Add Typer Library Skill for Agents (#1620)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sofie Van Landeghem · committed by GitHub · parent ba6cc2c · Browse files · 1 file·+266 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Diff stats
1 file+266 added−0 removed
- other:+266 added−0 removed
Provenance
- Commit
a437469- Tree
e692b06- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ba6cc2c
1 file changed+266 added−0 removed
added:typer/.agents/skills/typer/SKILL.md+266 added
1+---2+name: typer3+description: Typer best practices and conventions. Use when working with Typer CLIs. Keeps Typer code clean and up to date with the latest features and patterns, updated with new versions. Write new code or refactor and update old code.4+---5+6+# Typer7+8+Official Typer skill to write code with best practices, keeping up to date with new versions and features.9+10+## Installing typer11+12+In a virtual environment, `pip install typer` (with pip) or `uv pip install typer` (with uv). For your library/project, add `typer` to the dependencies in `pyproject.toml`.13+14+Do not install `typer-slim` or `typer-cli`, they are both deprecated and will now simply install `typer`.15+16+Typer supports Python 3.10 and above.17+18+## Use an explicit `Typer` app19+20+For maximum generalizability, create an explicit Typer app and register subcommand(s), instead of using `typer.run`:21+22+```python23+import typer24+25+app = typer.Typer()26+27+28+@app.command()29+def hello():30+ print(f"Hello World")31+32+33+if __name__ == "__main__":34+ app()35+```36+37+instead of:38+39+```python40+# DO NOT DO THIS: Not extensible. Use Typer() instead.41+import typer42+43+44+def main():45+ print(f"Hello World")46+47+48+if __name__ == "__main__":49+ typer.run(main)50+```51+52+## Execute the app53+54+To execute the app in the terminal, run55+56+```bash57+python main.py58+```59+60+When multiple commands are registered to the Typer app, you have to add the command name:61+62+```bash63+python main.py hello64+```65+66+To see the automatically generated help documentation, run67+68+```bash69+python main.py --help70+```71+72+or set `no_args_is_help` to `True` when creating the `Typer()` add to automatically show the help when running a command without any arguments.73+74+## Use `Annotated`75+76+Always prefer the `Annotated` style for declarations of CLI arguments and options.77+78+It allows us to pass additional metadata that can be used by Typer.79+80+```python81+from typing import Annotated82+83+import typer84+85+app = typer.Typer()86+87+88+@app.command()89+def hello(name: Annotated[str, typer.Argument()] = "World"):90+ # Note that name is an optional Argument, as a default is provided91+ print(f"Hello {name}")92+93+94+if __name__ == "__main__":95+ app()96+```97+98+This program can be run as-is, or can provide a specific name:99+100+```bash101+python main.py102+python main.py Rick103+```104+105+An older way of setting a default value is this:106+```python107+# DO NOT DO THIS: old style. Use Annotated instead.108+109+@app.command()110+def main(name: str = typer.Argument(default="World")):111+ # Note that name is an optional Argument, as a default is provided112+ print(f"Hello {name}")113+```114+115+Similarly, the old style could use ellipsis (...) to explicitely mark an argument as required.116+117+```python118+# DO NOT DO THIS: old style. Use Annotated without a default value instead.119+120+@app.command()121+def main(name: str = typer.Argument(default=...)):122+ # Note that name is now a required Argument123+ print(f"Hello {name}")124+```125+126+## CLI Options127+128+CLI options are declared in a similar fashion as arguments, but will be called on the CLI with a single dash (single letter) or 2 dashes (full name):129+130+```python131+from typing import Annotated132+133+import typer134+135+app = typer.Typer()136+137+138+@app.command()139+def main(user_name: Annotated[str, typer.Option("--name", "-n")]):140+ # On the CLI, the required user name can be specified with -n or --name141+ print(f"Hello {user_name}")142+143+144+if __name__ == "__main__":145+ app()146+```147+148+You can run this program as such:149+150+```bash151+python main.py -n "Rick"152+python main.py --name "Morty"153+```154+155+### CLI options with multiple values156+157+By declaring a CLI option as a list, it can receive multiple values:158+159+```python160+from typing import Annotated161+162+import typer163+164+app = typer.Typer()165+166+167+@app.command()168+def main(user: Annotated[list[str] | None, typer.Option()] = None):169+ if not user:170+ print(f"No users provided!")171+ raise typer.Abort()172+ for u in user:173+ print(f"Processing user: {u}")174+175+176+if __name__ == "__main__":177+ app()178+```179+180+This can be executed like so:181+182+```bash183+python main.py --user Rick --user Morty --user Summer184+```185+186+## Rich187+188+By default, Rich can be used with its custom markup syntax to set colors and styles, e.g.189+190+```python191+from rich import print192+193+print("[bold red]Alert![/bold red] [green]Portal gun[/green] shooting! :boom:")194+```195+196+Typer supports using Rich formatting in the docstrings and the help messages of CLI arguments and CLI options.197+198+199+```python200+from typing import Annotated201+202+import typer203+204+app = typer.Typer(rich_markup_mode="rich")205+206+207+@app.command()208+def create(209+ username: Annotated[210+ str, typer.Argument(help="The username to be [green]created[/green]")211+ ],212+):213+ """214+ [bold green]Create[/bold green] a new [italic]shiny[/italic] user. :sparkles:215+216+ This requires a [underline]username[/underline].217+ """218+ print(f"Creating user: {username}")219+220+221+@app.command(help="[bold red]Delete[/bold red] a user with [italic]USERNAME[/italic].")222+def delete(223+ username: Annotated[224+ str, typer.Argument(help="The username to be [red]deleted[/red]")225+ ],226+):227+ """228+ Some internal utility function to delete.229+ """230+ print(f"Deleting user: {username}")231+232+233+if __name__ == "__main__":234+ app()235+```236+237+To disable Rich formatting, set `rich_markup_mode` to `None` when creating a `Typer()` app. By default (when no value is given), Rich formatting is enabled.238+239+### Rich markdown240+241+You can also set `rich_markup_mode` to `"markdown"` to use Markdown in the docstring:242+243+```python244+from typing import Annotated245+246+import typer247+248+app = typer.Typer(rich_markup_mode="markdown")249+250+@app.command(help="**Delete** a user with *USERNAME*.")251+def delete(252+ username: Annotated[str, typer.Argument(help="The username to be **deleted** :boom:")]253+):254+ print(f"Deleting user: {username}")255+256+if __name__ == "__main__":257+ app()258+```259+260+## Click261+262+Originally, Typer was built on Click. However, going forward Typer will vendor Click. As such, Click extensions should not be used anymore.263+264+Other settings of `Option` and `Argument` that came from Click but shouldn't be used in Typer anymore, include: `expose_value`, `shell_complete`, `show_choices`, `errors`, `prompt_required`, `is_flag`, `flag_value` and `allow_from_autoenv`.265+266+Code bases using these should be refactored to use pure Typer functionality.