Skip to content
Myr

📝 Update and simplify usage of admonitions (#1755)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 8c47e39 · Browse files · 20 files+30 added−42 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

20 files+30 added−42 removed

  • other:+30 added−42 removed

20 files+30 added−42 removed

modified:docs/tutorial/arguments/help.md+1 added-1 removed
248 unmodified lines
249```250251</div>252253/// info253/// note254255Have in mind that the *CLI argument* will still show up in the first line with `Usage`.256257But it won't show up in the main help text under the `Arguments` section.
10 unmodified lines
modified:docs/tutorial/arguments/optional.md+2 added-2 removed
43 unmodified lines
44Or, using an explicit `Typer()` instance creation:4546{* docs_src/arguments/optional/tutorial001_an_py310.py hl[9] *}4748/// info48/// note4950Typer added support for `Annotated` (and started recommending it) in version 0.9.0.5152If you have an older version, you would get errors when trying to use `Annotated`.
155 unmodified lines
208```Python209name: str = typer.Argument(default=...)210```211212/// info212/// note213214If you hadn't seen that `...` before: it is a special single value, it is <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">part of Python and is called "Ellipsis"</a>.215216///
22 unmodified lines
modified:docs/tutorial/commands/help.md+2 added-2 removed
202 unmodified lines
203## Rich Markdown and Markup204205Typer installs **Rich** to allow for more formatting in the docstrings and the `help` parameter for *CLI arguments* and *CLI options*. You will see more about it below. 👇206207/// info207/// note208209You can disable rich text formatting by setting `rich_markup_mode` to `None` for your specific app.210Alternatively, you can disable it globally using an environmental variable `TYPER_USE_RICH` set to `False` or `0`.211
125 unmodified lines
337```338339</div>340341/// info341/// note342343Notice that in Markdown you cannot define colors. For colors you might prefer to use Rich markup.344345///
159 unmodified lines
modified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+1 added-1 removed
modified:docs/tutorial/options/name.md+1 added-1 removed
28 unmodified lines
29You can pass the *CLI option* name that you want to have in the following positional argument passed to `typer.Option()`:3031{* docs_src/options/name/tutorial001_an_py310.py hl[9] *}3233/// info33/// note3435"<a href="https://docs.python.org/3.8/glossary.html#term-argument" class="external-link" target="_blank">Positional</a>" means that it's not a function argument with a keyword name.3637For example `show_default=True` is a keyword argument. "`show_default`" is the keyword.
280 unmodified lines
modified:docs/tutorial/options/required.md+1 added-1 removed
modified:docs/tutorial/options/version.md+1 added-1 removed
82 unmodified lines
83`typer.BadParameter` prints the error to "standard error", not to "standard output", and because the completion system only reads from "standard output", it won't break completion.8485///8687/// info87/// note8889If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](../printing.md#standard-output-and-standard-error){.internal-link target=_blank}.9091///
21 unmodified lines
modified:docs/tutorial/parameter-types/file.md+1 added-1 removed
100 unmodified lines
101```102103</div>104105/// info | Technical Details105/// note | Technical Details106107`typer.FileTextWrite` is a just a convenience class.108109It's the same as using `typer.FileText` and setting `mode="w"`. You will learn about `mode` later below.
125 unmodified lines
modified:docs/tutorial/parameter-types/uuid.md+1 added-1 removed
1# UUID23/// info3/// note45A UUID is a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier" class="external-link" target="_blank">"Universally Unique Identifier"</a>.67It's a standard format for identifiers, like passport numbers, but for anything, not just people in countries.
42 unmodified lines
modified:docs/tutorial/subcommands/name-and-help.md+1 added-1 removed
303 unmodified lines
304You can set it when creating a new `typer.Typer()`:305306{* docs_src/subcommands/name_help/tutorial006_py310.py hl[12] *}307308/// info308/// note309310The rest of the callbacks and overrides are there only to show you that they don't affect the name and help text when you set it explicitly.311312///
147 unmodified lines
modified:docs/tutorial/options-autocompletion.md+3 added-3 removed
126 unmodified lines
127In the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.128129///130131/// info131/// note132133The help text will be visible in Zsh, Fish, and PowerShell.134135Bash doesn't support showing the help text, but completion will still work the same.
32 unmodified lines
168In the end, that's just to save us a couple of lines of code.169170///171172/// info172/// note173174The function can use `yield`, so it doesn't have to return strictly a `list`, it just has to be <a href="https://docs.python.org/3.8/glossary.html#term-iterable" class="external-link" target="_blank">iterable</a>.175176But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).
118 unmodified lines
295Using `stderr=True` tells **Rich** that the output should be shown in "standard error".296297{* docs_src/options_autocompletion/tutorial008_an_py310.py hl[12,15:16] *}298299/// info299/// note300301If you have disabled Rich, you can also use `print(lastname, file=sys.stderr)` or `typer.echo("some text", err=True)` instead.302303///
81 unmodified lines
modified:docs/tutorial/printing.md+1 added-1 removed
128 unmodified lines
129And there's another "**virtual file**" called "**standard error**" that is normally only used for errors.130131But we can also "print" to "standard error". And both are shown on the terminal to the users.132133/// info133/// note134135If you use PowerShell it's quite possible that what you print to "standard error" won't be shown in the terminal.136137In PowerShell, to see "standard error" you would have to check the variable `$Error`.
135 unmodified lines
modified:docs/tutorial/progressbar.md+1 added-1 removed
121 unmodified lines
122Notice that there are 2 levels of code blocks. One for the `with` statement and one for the `for` statement.123124///125126/// info126/// note127128This is mostly useful for operations that take some time.129130In the example above we are faking it with `time.sleep()`.
102 unmodified lines
modified:docs/tutorial/testing.md+2 added-2 removed
73 unmodified lines
74You could also check the output sent to "standard error" (`stderr`) or "standard output" (`stdout`) independently by accessing `result.stdout` and `result.stderr` in your tests.7576///7778/// info78/// note7980If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](printing.md#standard-output-and-standard-error){.internal-link target=_blank}.8182///
45 unmodified lines
128You can test the input typed in the terminal using `input="camila@example.com\n"`.129130This is because what you type in the terminal goes to "**standard input**" and is handled by the operating system as if it was a "virtual file".131132/// info132/// note133134If you need a refresher about what is "standard output", "standard error", and "standard input" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](printing.md#standard-output-and-standard-error){.internal-link target=_blank}.135136///
78 unmodified lines
modified:docs/tutorial/typer-app.md+1 added-1 removed
18 unmodified lines
19* Create a new `typer.Typer()` "application".20* Create a new "`command`" with your function.21* Call the same "application" as if it was a function with "`app()`".2223/// info | `@decorator` Info23/// note | `@decorator` Info2425That `@something` syntax in Python is called a "decorator".2627You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).
91 unmodified lines
modified:docs/alternatives.md+6 added-6 removed
14 unmodified lines
15`argparse` is the Python standard library's module to write CLIs.1617It provides a better alternative than reading the *CLI Parameters* as a `list` of `str` and parsing everything by hand.1819/// check | Inspired **Typer** to19/// tip | Inspired **Typer** to2021Provide a better development experience than just reading *CLI Parameters* by hand.2223///
3 unmodified lines
27Hug is a library to create APIs and CLIs, it uses parameters in functions to declare the required data.2829It inspired a lot of the ideas in **FastAPI** and **Typer**.3031/// check | Inspired **Typer** to31/// tip | Inspired **Typer** to3233Use function parameters to declare *CLI arguments* and *CLI options* as it simplifies a lot the development experience.3435///
1 unmodified line
37### <a href="https://plac.readthedocs.io/en/latest/" class="external-link" target="_blank">Plac</a>3839Plac is another library to create CLIs using parameters in functions, similar to Hug.4041/// check | Inspired **Typer** to41/// tip | Inspired **Typer** to4243Provide a simple way to use a function as a command line app, without having to create a complete app, with `typer.run(some_function)`.4445///
5 unmodified lines
51It powers **FastAPI** underneath.5253It is not used by **Typer**, but it inspired a lot of the design (through **FastAPI**).5455/// check | Inspired **Typer** to55/// tip | Inspired **Typer** to5657Use standard Python type annotations to declare types instead of library-specific types or classes and use them for data validation and documentation.5859///
9 unmodified lines
69It uses decorators on top of functions to modify the actual value of those functions, converting them to instances of a specific class. This is a clever trick, but code editors can't provide great support for autocompletion that way.7071It was built with some great ideas and design using the features available in the language at the time (Python 2.x).7273/// check | **Typer** uses it for73/// tip | **Typer** uses it for7475Everything. 🚀7677**Typer** mainly adds a layer on top of Click, making the code simpler and easier to use, with autocompletion everywhere, etc, but providing all the powerful features of Click underneath.
9 unmodified lines
87Previous versions of **Typer** had deep integrations with `click-completion` and used it as an optional dependency. But now all the completion logic is implemented internally in **Typer** itself, the internal logic was heavily inspired and using some parts of `click-completion`.8889And now **Typer** improved it to have new features, tests, some bug fixes (for issues in plain `click-completion` and Click), and better support for shells, including modern versions of PowerShell (e.g. the default versions that come with Windows 10).9091/// check | Inspired **Typer** to91/// tip | Inspired **Typer** to9293Provide auto completion for all the shells.9495///
8 unmodified lines
modified:docs/contributing.md+1 added-1 removed
117 unmodified lines
118```console119$ typer --install-completion120```121122/// info122/// note123124In `pwsh` you will probably get a warning of:125126```plaintext
211 unmodified lines
modified:docs/help-typer.md+1 added-1 removed
155 unmodified lines
156* Check and read the code, see if it makes sense, **run it locally** and see if it actually solves the problem.157158* Then **comment** saying that you did that, that's how I will know you really checked it.159160/// info160/// note161162Unfortunately, I can't simply trust PRs that just have several approvals.163164Several times it has happened that there are PRs with 3, 5 or more approvals, probably because the description is appealing, but when I check the PRs, they are actually broken, have a bug, or don't solve the problem they claim to solve. 😅
88 unmodified lines
modified:docs/virtual-environments.md+2 added-2 removed
1# Virtual Environments23When you work in Python projects you probably should use a **virtual environment** (or a similar mechanism) to isolate the packages you install for each project.45/// info5/// note67If you already know about virtual environments, how to create them and use them, you might want to skip this section. 🤓89///
7 unmodified lines
17A **virtual environment** is a directory with some files in it.1819///2021/// info21/// note2223This page will teach you how to use **virtual environments** and how they work.2425If you are ready to adopt a **tool that manages everything** for you (including installing Python), try <a href="https://github.com/astral-sh/uv" class="external-link" target="_blank">uv</a>.
819 unmodified lines
modified:mkdocs.yml-12 removed
197 unmodified lines
198 pymdownx.tilde:199200 # pymdownx blocks201 pymdownx.blocks.admonition:202 types:203 - note204 - attention205 - caution206 - danger207 - error208 - tip209 - hint210 - warning211 # Custom types212 - info213 - check202 pymdownx.blocks.details:203 pymdownx.blocks.tab:204 alternate_style: True205
27 unmodified lines