🔥 Remove support for Python 3.6 (#758)
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 2b29e98 · Browse files · 37 files·+180 added−246 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
37 files+180 added−246 removed
- tests:+9 added−50 removed
- other:+171 added−196 removed
Provenance
- Commit
13404a0- Tree
18d2ed7- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2b29e98
37 files changed+180 added−246 removed
modified:.github/workflows/test.yml+2 added-7 removed
10 unmodified lines111213141515161718196 unmodified lines262728293030323334353132333440353637383954 unmodified lines10 unmodified lines test: runs-on: ubuntu-20.04 strategy: matrix: python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"] python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"] click-version: - click-7 - click-8 fail-fast: false6 unmodified lines python-version: ${{ matrix.python-version }} - name: Install Flit run: pip install flit - name: Install Dependencies if: ${{ matrix.python-version != '3.6' }} run: python -m flit install --symlink - name: Install Dependencies if: ${{ matrix.python-version == '3.6' }} # This doesn't install the editable install, so coverage doesn't get subprocesses run: python -m pip install ".[test]" - name: Install Click 7 if: matrix.click-version == 'click-7' run: pip install "click<8.0.0" - name: Lint if: ${{ matrix.python-version != '3.6' && matrix.click-version == 'click-8' }} if: ${{ matrix.click-version == 'click-8' }} run: bash scripts/lint.sh - run: mkdir coverage - name: Test run: bash scripts/test.sh54 unmodified linesmodified:docs/tutorial/arguments/default.md+4 added-4 removed
4 unmodified lines5 ## An optional *CLI argument* with a default6 7 We can also use `typer.Argument()` to make a *CLI argument* have a default value other than `None`:8 9-=== "Python 3.6+"9+=== "Python 3.7+"10 11 ```Python hl_lines="5"12 {!> ../docs_src/arguments/default/tutorial001_an.py!}13 ```14 15-=== "Python 3.6+ non-Annotated"15+=== "Python 3.7+ non-Annotated"16 17 !!! tip18 Prefer to use the `Annotated` version if possible.19 39 unmodified lines59 ## Dynamic default value60 61 And we can even make the default value be dynamically generated by passing a function as the `default_factory` argument:62 63-=== "Python 3.6+"63+=== "Python 3.7+"64 65 ```Python hl_lines="7-8 11"66 {!> ../docs_src/arguments/default/tutorial002_an.py!}67 ```68 69-=== "Python 3.6+ non-Annotated"69+=== "Python 3.7+ non-Annotated"70 71 !!! tip72 Prefer to use the `Annotated` version if possible.73 47 unmodified linesmodified:docs/tutorial/arguments/envvar.md+6 added-6 removed
1 You can also configure a *CLI argument* to read a value from an environment variable if it is not provided in the command line as a *CLI argument*.2 3 To do that, use the `envvar` parameter for `typer.Argument()`:4 5-=== "Python 3.6+"5+=== "Python 3.7+"6 7 ```Python hl_lines="5"8 {!> ../docs_src/arguments/envvar/tutorial001_an.py!}9 ```10 11-=== "Python 3.6+ non-Annotated"11+=== "Python 3.7+ non-Annotated"12 13 !!! tip14 Prefer to use the `Annotated` version if possible.15 43 unmodified lines59 ## Multiple environment variables60 61 You are not restricted to a single environment variable, you can declare a list of environment variables that could be used to get a value if it was not passed in the command line:62 63-=== "Python 3.6+"63+=== "Python 3.7+"64 65 ```Python hl_lines="6"66 {!> ../docs_src/arguments/envvar/tutorial002_an.py!}67 ```68 69-=== "Python 3.6+ non-Annotated"69+=== "Python 3.7+ non-Annotated"70 71 !!! tip72 Prefer to use the `Annotated` version if possible.73 33 unmodified lines107 ## Hide an env var from the help text108 109 By default, environment variables used will be shown in the help text, but you can disable them with `show_envvar=False`:110 111-=== "Python 3.6+"111+=== "Python 3.7+"112 113 ```Python hl_lines="7"114 {!> ../docs_src/arguments/envvar/tutorial003_an.py!}115 ```116 117-=== "Python 3.6+ non-Annotated"117+=== "Python 3.7+ non-Annotated"118 119 !!! tip120 Prefer to use the `Annotated` version if possible.121 33 unmodified linesmodified:docs/tutorial/arguments/help.md+16 added-16 removed
10 unmodified lines11 ## Add a `help` text for a *CLI argument*12 13 You can use the `help` parameter to add a help text for a *CLI argument*:14 15-=== "Python 3.6+"15+=== "Python 3.7+"16 17 ```Python hl_lines="5"18 {!> ../docs_src/arguments/help/tutorial001_an.py!}19 ```20 21-=== "Python 3.6+ non-Annotated"21+=== "Python 3.7+ non-Annotated"22 23 !!! tip24 Prefer to use the `Annotated` version if possible.25 23 unmodified lines49 ## Combine help text and docstrings50 51 And of course, you can also combine that `help` with the <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>:52 53-=== "Python 3.6+"53+=== "Python 3.7+"54 55 ```Python hl_lines="5-8"56 {!> ../docs_src/arguments/help/tutorial002_an.py!}57 ```58 59-=== "Python 3.6+ non-Annotated"59+=== "Python 3.7+ non-Annotated"60 61 !!! tip62 Prefer to use the `Annotated` version if possible.63 25 unmodified lines89 ## Help with defaults90 91 If you have a *CLI argument* with a default value, like `"World"`:92 93-=== "Python 3.6+"93+=== "Python 3.7+"94 95 ```Python hl_lines="5"96 {!> ../docs_src/arguments/help/tutorial003_an.py!}97 ```98 99-=== "Python 3.6+ non-Annotated"99+=== "Python 3.7+ non-Annotated"100 101 !!! tip102 Prefer to use the `Annotated` version if possible.103 23 unmodified lines127 </div>128 129 But you can disable that if you want to, with `show_default=False`:130 131-=== "Python 3.6+"131+=== "Python 3.7+"132 133 ```Python hl_lines="7"134 {!> ../docs_src/arguments/help/tutorial004_an.py!}135 ```136 137-=== "Python 3.6+ non-Annotated"137+=== "Python 3.7+ non-Annotated"138 139 !!! tip140 Prefer to use the `Annotated` version if possible.141 30 unmodified lines172 ## Custom default string173 174 You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:175 176-=== "Python 3.6+"176+=== "Python 3.7+"177 178 ```Python hl_lines="9"179 {!> ../docs_src/arguments/help/tutorial005_an.py!}180 ```181 182-=== "Python 3.6+ non-Annotated"182+=== "Python 3.7+ non-Annotated"183 184 !!! tip185 Prefer to use the `Annotated` version if possible.186 43 unmodified lines230 But you can customize it with the `metavar` parameter for `typer.Argument()`.231 232 For example, let's say you don't want to have the default of `NAME`, you want to have `username`, in lowercase, and you really want ✨ emojis ✨ everywhere:233 234-=== "Python 3.6+"234+=== "Python 3.7+"235 236 ```Python hl_lines="5"237 {!> ../docs_src/arguments/help/tutorial006_an.py!}238 ```239 240-=== "Python 3.6+ non-Annotated"240+=== "Python 3.7+ non-Annotated"241 242 !!! tip243 Prefer to use the `Annotated` version if possible.244 24 unmodified lines269 You might want to show the help information for *CLI arguments* in different panels when using the `--help` option.270 271 If you have installed Rich as described in the docs for [Printing and Colors](../printing.md){.internal-link target=_blank}, you can set the `rich_help_panel` parameter to the name of the panel where you want this *CLI argument* to be shown:272 273-=== "Python 3.6+"273+=== "Python 3.7+"274 275 ```Python hl_lines="8 12"276 {!> ../docs_src/arguments/help/tutorial007_an.py!}277 ```278 279-=== "Python 3.6+ non-Annotated"279+=== "Python 3.7+ non-Annotated"280 281 !!! tip282 Prefer to use the `Annotated` version if possible.283 41 unmodified lines325 If you want, you can make a *CLI argument* **not** show up in the `Arguments` section in the help text.326 327 You will probably not want to do this normally, but it's possible:328 329-=== "Python 3.6+"329+=== "Python 3.7+"330 331 ```Python hl_lines="5"332 {!> ../docs_src/arguments/help/tutorial008_an.py!}333 ```334 335-=== "Python 3.6+ non-Annotated"335+=== "Python 3.7+ non-Annotated"336 337 !!! tip338 Prefer to use the `Annotated` version if possible.339 49 unmodified linesmodified:docs/tutorial/commands/help.md+9 added-9 removed
1 The same as before, you can add help for the commands in the docstrings and the *CLI options*.2 3 And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:4 5-=== "Python 3.6+"5+=== "Python 3.7+"6 7 ```Python hl_lines="4 9-11 22 26-30 43 47-51 60-62"8 {!> ../docs_src/commands/help/tutorial001_an.py!}9 ```10 11-=== "Python 3.6+ non-Annotated"11+=== "Python 3.7+ non-Annotated"12 13 !!! tip14 Prefer to use the `Annotated` version if possible.15 184 unmodified lines200 ### Rich Markup201 202 If you set `rich_markup_mode="rich"` when creating the `typer.Typer()` app, you will be able to use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> in the docstring, and even in the help for the *CLI arguments* and options:203 204-=== "Python 3.6+"204+=== "Python 3.7+"205 206 ```Python hl_lines="4 10 14-16 21 24 27"207 {!> ../docs_src/commands/help/tutorial004_an.py!}208 ```209 210-=== "Python 3.6+ non-Annotated"210+=== "Python 3.7+ non-Annotated"211 212 !!! tip213 Prefer to use the `Annotated` version if possible.214 61 unmodified lines276 ### Rich Markdown277 278 If you set `rich_markup_mode="markdown"` when creating the `typer.Typer()` app, you will be able to use Markdown in the docstring:279 280-=== "Python 3.6+"280+=== "Python 3.7+"281 282 ```Python hl_lines="4 9 12-20 25 27-28"283 {!> ../docs_src/commands/help/tutorial005_an.py!}284 ```285 286-=== "Python 3.6+ non-Annotated"286+=== "Python 3.7+ non-Annotated"287 288 !!! tip289 Prefer to use the `Annotated` version if possible.290 71 unmodified lines362 ### Help Panels for Commands363 364 To set the panel for a command you can pass the argument `rich_help_panel` with the name of the panel you want to use:365 366-=== "Python 3.6+"366+=== "Python 3.7+"367 368 ```Python hl_lines="22 30 38 46"369 {!> ../docs_src/commands/help/tutorial006.py!}370 ```36 unmodified lines407 The same way, you can configure the panels for *CLI arguments* and *CLI options* with `rich_help_panel`.408 409 And of course, in the same application you can also set the `rich_help_panel` for commands.410 411-=== "Python 3.6+"411+=== "Python 3.7+"412 413 ```Python hl_lines="15 21 27 37"414 {!> ../docs_src/commands/help/tutorial007_an.py!}415 ```416 417-=== "Python 3.6+ non-Annotated"417+=== "Python 3.7+ non-Annotated"418 419 !!! tip420 Prefer to use the `Annotated` version if possible.421 111 unmodified linesmodified:docs/tutorial/commands/options.md+2 added-2 removed
1 Commands can also have their own *CLI options*.2 3 In fact, each command can have different *CLI arguments* and *CLI options*:4 5-=== "Python 3.6+"5+=== "Python 3.7+"6 7 ```Python hl_lines="8 14-17 27-29 38"8 {!> ../docs_src/commands/options/tutorial001_an.py!}9 ```10 11-=== "Python 3.6+ non-Annotated"11+=== "Python 3.7+ non-Annotated"12 13 !!! tip14 Prefer to use the `Annotated` version if possible.15 92 unmodified linesmodified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+2 added-2 removed
29 unmodified lines303132333434353637383940404142434441 unmodified lines29 unmodified lines## *CLI arguments* with tuples
If you want a specific number of values and types, you can use a tuple, and it can even have default values:
=== "Python 3.6+"=== "Python 3.7+"
```Python hl_lines="8-10" {!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py!} ```
=== "Python 3.6+ non-Annotated"=== "Python 3.7+ non-Annotated"
!!! tip Prefer to use the `Annotated` version if possible.
41 unmodified linesmodified:docs/tutorial/multiple-values/multiple-options.md+4 added-4 removed
2 unmodified lines3 For example, let's say you want to accept several users in a single execution.4 5 For this, use the standard Python `typing.List` to declare it as a `list` of `str`:6 7-=== "Python 3.6+"7+=== "Python 3.7+"8 9 ```Python hl_lines="1 7"10 {!> ../docs_src/multiple_values/multiple_options/tutorial001_an.py!}11 ```12 13-=== "Python 3.6+ non-Annotated"13+=== "Python 3.7+ non-Annotated"14 15 !!! tip16 Prefer to use the `Annotated` version if possible.17 32 unmodified lines50 ## Multiple `float`51 52 The same way, you can use other types and they will be converted by **Typer** to their declared type:53 54-=== "Python 3.6+"54+=== "Python 3.7+"55 56 ```Python hl_lines="7"57 {!> ../docs_src/multiple_values/multiple_options/tutorial002_an.py!}58 ```59 60-=== "Python 3.6+ non-Annotated"60+=== "Python 3.7+ non-Annotated"61 62 !!! tip63 Prefer to use the `Annotated` version if possible.64 25 unmodified linesmodified:docs/tutorial/multiple-values/options-with-multiple-values.md+2 added-2 removed
2 unmodified lines3 You can set the number of values and types to anything you want, but it has to be a fixed number of values.4 5 For this, use the standard Python `typing.Tuple`:6 7-=== "Python 3.6+"7+=== "Python 3.7+"8 9 ```Python hl_lines="1 7"10 {!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py!}11 ```12 13-=== "Python 3.6+ non-Annotated"13+=== "Python 3.7+ non-Annotated"14 15 !!! tip16 Prefer to use the `Annotated` version if possible.17 84 unmodified linesmodified:docs/tutorial/options/callback-and-context.md+8 added-8 removed
4 unmodified lines5 ## Validate *CLI parameters*6 7 For example, you could do some validation before the rest of the code is executed.8 9-=== "Python 3.6+"9+=== "Python 3.7+"10 11 ```Python hl_lines="5-8 11"12 {!> ../docs_src/options/callback/tutorial001_an.py!}13 ```14 15-=== "Python 3.6+ non-Annotated"15+=== "Python 3.7+ non-Annotated"16 17 !!! tip18 Prefer to use the `Annotated` version if possible.19 84 unmodified lines104 ### Breaking completion in a callback105 106 Let's say that when the callback is running, we want to show a message saying that it's validating the name:107 108-=== "Python 3.6+"108+=== "Python 3.7+"109 110 ```Python hl_lines="6"111 {!> ../docs_src/options/callback/tutorial002_an.py!}112 ```113 114-=== "Python 3.6+ non-Annotated"114+=== "Python 3.7+ non-Annotated"115 116 !!! tip117 Prefer to use the `Annotated` version if possible.118 33 unmodified lines152 But you can access the context by declaring a function parameter of type `typer.Context`.153 154 The "context" has some additional data about the current execution of your program:155 156-=== "Python 3.6+"156+=== "Python 3.7+"157 158 ```Python hl_lines="5-7"159 {!> ../docs_src/options/callback/tutorial003_an.py!}160 ```161 162-=== "Python 3.6+ non-Annotated"162+=== "Python 3.7+ non-Annotated"163 164 !!! tip165 Prefer to use the `Annotated` version if possible.166 30 unmodified lines197 ## Using the `CallbackParam` object198 199 The same way you can access the `typer.Context` by declaring a function parameter with its value, you can declare another function parameter with type `typer.CallbackParam` to get the specific Click `Parameter` object.200 201-=== "Python 3.6+"201+=== "Python 3.7+"202 203 ```Python hl_lines="5 8"204 {!> ../docs_src/options/callback/tutorial004_an.py!}205 ```206 207-=== "Python 3.6+ non-Annotated"207+=== "Python 3.7+ non-Annotated"208 209 !!! tip210 Prefer to use the `Annotated` version if possible.211 50 unmodified linesmodified:docs/tutorial/options/help.md+8 added-8 removed
1 You already saw how to add a help text for *CLI arguments* with the `help` parameter.2 3 Let's now do the same for *CLI options*:4 5-=== "Python 3.6+"5+=== "Python 3.7+"6 7 ```Python hl_lines="7-8"8 {!> ../docs_src/options/help/tutorial001_an.py!}9 ```10 11-=== "Python 3.6+ non-Annotated"11+=== "Python 3.7+ non-Annotated"12 13 !!! tip14 Prefer to use the `Annotated` version if possible.15 50 unmodified lines66 The same as with *CLI arguments*, you can put the help for some *CLI options* in different panels to be shown with the `--help` option.67 68 If you have installed Rich as described in the docs for [Printing and Colors](../printing.md){.internal-link target=_blank}, you can set the `rich_help_panel` parameter to the name of the panel you want for each *CLI option*:69 70-=== "Python 3.6+"70+=== "Python 3.7+"71 72 ```Python hl_lines="11 17"73 {!> ../docs_src/options/help/tutorial002_an.py!}74 ```75 76-=== "Python 3.6+ non-Annotated"76+=== "Python 3.7+ non-Annotated"77 78 !!! tip79 Prefer to use the `Annotated` version if possible.80 44 unmodified lines125 ## Hide default from help126 127 You can tell Typer to not show the default value in the help text with `show_default=False`:128 129-=== "Python 3.6+"129+=== "Python 3.7+"130 131 ```Python hl_lines="5"132 {!> ../docs_src/options/help/tutorial003_an.py!}133 ```134 135-=== "Python 3.6+ non-Annotated"135+=== "Python 3.7+ non-Annotated"136 137 !!! tip138 Prefer to use the `Annotated` version if possible.139 32 unmodified lines172 ## Custom default string173 174 You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:175 176-=== "Python 3.6+"176+=== "Python 3.7+"177 178 ```Python hl_lines="7"179 {!> ../docs_src/options/help/tutorial004_an.py!}180 ```181 182-=== "Python 3.6+ non-Annotated"182+=== "Python 3.7+ non-Annotated"183 184 !!! tip185 Prefer to use the `Annotated` version if possible.186 26 unmodified linesmodified:docs/tutorial/options/name.md+10 added-10 removed
24 unmodified lines25 Let's say the function parameter name is `user_name` as above, but you want the *CLI option* to be just `--name`.26 27 You can pass the *CLI option* name that you want to have in the following positional argument passed to `typer.Option()`:28 29-=== "Python 3.6+"29+=== "Python 3.7+"30 31 ```Python hl_lines="5"32 {!> ../docs_src/options/name/tutorial001_an.py!}33 ```34 35 Here you are passing the string `"--name"` as the first positional argument to `typer.Option()`.36 37-=== "Python 3.6+ non-Annotated"37+=== "Python 3.7+ non-Annotated"38 39 !!! tip40 Prefer to use the `Annotated` version if possible.41 151 unmodified lines193 You can overwrite the *CLI option* name to use as in the previous example, but you can also declare extra alternatives, including short names.194 195 For example, extending the previous example, let's add a *CLI option* short name `-n`:196 197-=== "Python 3.6+"197+=== "Python 3.7+"198 199 ```Python hl_lines="5"200 {!> ../docs_src/options/name/tutorial002_an.py!}201 ```202 203-=== "Python 3.6+ non-Annotated"203+=== "Python 3.7+ non-Annotated"204 205 !!! tip206 Prefer to use the `Annotated` version if possible.207 29 unmodified lines237 ### *CLI option* only short name238 239 If you only declare a short name like `-n` then that will be the only *CLI option* name. And neither `--name` nor `--user-name` will be available.240 241-=== "Python 3.6+"241+=== "Python 3.7+"242 243 ```Python hl_lines="5"244 {!> ../docs_src/options/name/tutorial003_an.py!}245 ```246 247-=== "Python 3.6+ non-Annotated"247+=== "Python 3.7+ non-Annotated"248 249 !!! tip250 Prefer to use the `Annotated` version if possible.251 26 unmodified lines278 ### *CLI option* short name and default279 280 Continuing with the example above, as **Typer** allows you to declare a *CLI option* as having only a short name, if you want to have the default long name plus a short name, you have to declare both explicitly:281 282-=== "Python 3.6+"282+=== "Python 3.7+"283 284 ```Python hl_lines="5"285 {!> ../docs_src/options/name/tutorial004_an.py!}286 ```287 288-=== "Python 3.6+ non-Annotated"288+=== "Python 3.7+ non-Annotated"289 290 !!! tip291 Prefer to use the `Annotated` version if possible.292 32 unmodified lines325 You can create multiple short names and use them together.326 327 You don't have to do anything special for it to work (apart from declaring those short versions):328 329-=== "Python 3.6+"329+=== "Python 3.7+"330 331 ```Python hl_lines="6-7"332 {!> ../docs_src/options/name/tutorial005_an.py!}333 ```334 335-=== "Python 3.6+ non-Annotated"335+=== "Python 3.7+ non-Annotated"336 337 !!! tip338 Prefer to use the `Annotated` version if possible.339 36 unmodified linesmodified:docs/tutorial/options/password.md+4 added-4 removed
1 Apart from having a prompt, you can make a *CLI option* have a `confirmation_prompt=True`:2 3-=== "Python 3.6+"3+=== "Python 3.7+"4 5 ```Python hl_lines="7"6 {!> ../docs_src/options/password/tutorial001_an.py!}7 ```8 9-=== "Python 3.6+ non-Annotated"9+=== "Python 3.7+ non-Annotated"10 11 !!! tip12 Prefer to use the `Annotated` version if possible.13 26 unmodified lines40 You can achieve the same using `hide_input=True`.41 42 And if you combine it with `confirmation_prompt=True` you can easily receive a password with double confirmation:43 44-=== "Python 3.6+"44+=== "Python 3.7+"45 46 ```Python hl_lines="8"47 {!> ../docs_src/options/password/tutorial002_an.py!}48 ```49 50-=== "Python 3.6+ non-Annotated"50+=== "Python 3.7+ non-Annotated"51 52 !!! tip53 Prefer to use the `Annotated` version if possible.54 21 unmodified linesmodified:docs/tutorial/options/prompt.md+6 added-6 removed
1 It's also possible to, instead of just showing an error, ask for the missing value with `prompt=True`:2 3-=== "Python 3.6+"3+=== "Python 3.7+"4 5 ```Python hl_lines="5"6 {!> ../docs_src/options/prompt/tutorial001_an.py!}7 ```8 9-=== "Python 3.6+ non-Annotated"9+=== "Python 3.7+ non-Annotated"10 11 !!! tip12 Prefer to use the `Annotated` version if possible.13 20 unmodified lines34 ## Customize the prompt35 36 You can also set a custom prompt, passing the string that you want to use instead of just `True`:37 38-=== "Python 3.6+"38+=== "Python 3.7+"39 40 ```Python hl_lines="7"41 {!> ../docs_src/options/prompt/tutorial002_an.py!}42 ```43 44-=== "Python 3.6+ non-Annotated"44+=== "Python 3.7+ non-Annotated"45 46 !!! tip47 Prefer to use the `Annotated` version if possible.48 24 unmodified lines73 You can do it passing the parameter `confirmation_prompt=True`.74 75 Let's say it's a CLI app to delete a project:76 77-=== "Python 3.6+"77+=== "Python 3.7+"78 79 ```Python hl_lines="6"80 {!> ../docs_src/options/prompt/tutorial003_an.py!}81 ```82 83-=== "Python 3.6+ non-Annotated"83+=== "Python 3.7+ non-Annotated"84 85 !!! tip86 Prefer to use the `Annotated` version if possible.87 34 unmodified linesmodified:docs/tutorial/options/required.md+3 added-3 removed
9 unmodified lines10 To make a *CLI option* required, you can put `typer.Option()` inside of `Annotated` and leave the parameter without a default value.11 12 Let's make `--lastname` a required *CLI option*:13 14-=== "Python 3.6+"14+=== "Python 3.7+"15 16 ```Python hl_lines="5"17 {!> ../docs_src/options/required/tutorial001_an.py!}18 ```19 20 The same way as with `typer.Argument()`, the old style of using the function parameter default value is also supported, in that case you would just not pass anything to the `default` parameter.21 22-=== "Python 3.6+ non-Annotated"22+=== "Python 3.7+ non-Annotated"23 24 ```Python hl_lines="4"25 {!> ../docs_src/options/required/tutorial001.py!}26 ```27 28 Or you can explictily pass `...` to `typer.Option(default=...)`:29 30-=== "Python 3.6+ non-Annotated"30+=== "Python 3.7+ non-Annotated"31 32 ```Python hl_lines="4"33 {!> ../docs_src/options/required/tutorial002.py!}34 ```41 unmodified linesmodified:docs/tutorial/options/version.md+6 added-6 removed
4 unmodified lines5 ## First version of `--version`6 7 Let's see a first version of how it could look like:8 9-=== "Python 3.6+"9+=== "Python 3.7+"10 11 ```Python hl_lines="9-12 17-19"12 {!> ../docs_src/options/version/tutorial001_an.py!}13 ```14 15-=== "Python 3.6+ non-Annotated"15+=== "Python 3.7+ non-Annotated"16 17 !!! tip18 Prefer to use the `Annotated` version if possible.19 44 unmodified lines64 ## Previous parameters and `is_eager`65 66 But now let's say that the `--name` *CLI option* that we declared before `--version` is required, and it has a callback that could exit the program:67 68-=== "Python 3.6+"68+=== "Python 3.7+"69 70 ```Python hl_lines="15-17 22-24"71 {!> ../docs_src/options/version/tutorial002_an.py!}72 ```73 74-=== "Python 3.6+ non-Annotated"74+=== "Python 3.7+ non-Annotated"75 76 !!! tip77 Prefer to use the `Annotated` version if possible.78 28 unmodified lines107 For those cases, we can mark a *CLI parameter* (a *CLI option* or *CLI argument*) with `is_eager=True`.108 109 That will tell **Typer** (actually Click) that it should process this *CLI parameter* before the others:110 111-=== "Python 3.6+"111+=== "Python 3.7+"112 113 ```Python hl_lines="23-26"114 {!> ../docs_src/options/version/tutorial003_an.py!}115 ```116 117-=== "Python 3.6+ non-Annotated"117+=== "Python 3.7+ non-Annotated"118 119 !!! tip120 Prefer to use the `Annotated` version if possible.121 16 unmodified linesmodified:docs/tutorial/parameter-types/bool.md+8 added-8 removed
6 unmodified lines7 Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.8 9 We can do that by specifying the exact name we want:10 11-=== "Python 3.6+"11+=== "Python 3.7+"12 13 ```Python hl_lines="5"14 {!> ../docs_src/parameter_types/bool/tutorial001_an.py!}15 ```16 17-=== "Python 3.6+ non-Annotated"17+=== "Python 3.7+ non-Annotated"18 19 !!! tip20 Prefer to use the `Annotated` version if possible.21 46 unmodified lines68 We might want to instead have `--accept` and `--reject`.69 70 We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:71 72-=== "Python 3.6+"72+=== "Python 3.7+"73 74 ```Python hl_lines="7"75 {!> ../docs_src/parameter_types/bool/tutorial002_an.py!}76 ```77 78-=== "Python 3.6+ non-Annotated"78+=== "Python 3.7+ non-Annotated"79 80 !!! tip81 Prefer to use the `Annotated` version if possible.82 39 unmodified lines122 The same way, you can declare short versions of the names for these *CLI options*.123 124 For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:125 126-=== "Python 3.6+"126+=== "Python 3.7+"127 128 ```Python hl_lines="5"129 {!> ../docs_src/parameter_types/bool/tutorial003_an.py!}130 ```131 132-=== "Python 3.6+ non-Annotated"132+=== "Python 3.7+ non-Annotated"133 134 !!! tip135 Prefer to use the `Annotated` version if possible.136 34 unmodified lines171 If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.172 173 To do that, use a space and a single `/` and pass the negative name after:174 175-=== "Python 3.6+"175+=== "Python 3.7+"176 177 ```Python hl_lines="5"178 {!> ../docs_src/parameter_types/bool/tutorial004_an.py!}179 ```180 181-=== "Python 3.6+ non-Annotated"181+=== "Python 3.7+ non-Annotated"182 183 !!! tip184 Prefer to use the `Annotated` version if possible.185 41 unmodified linesmodified:docs/tutorial/parameter-types/custom-types.md+4 added-4 removed
9 unmodified lines10 ## Type Parser11 12 `typer.Argument` and `typer.Option` can create custom parameter types with a `parser` <abbr title="something that can be called like a function">callable</abbr>.13 14-=== "Python 3.6+"14+=== "Python 3.7+"15 16 ```Python hl_lines="13-14 18-19"17 {!> ../docs_src/parameter_types/custom_types/tutorial001_an.py!}18 ```19 20-=== "Python 3.6+ non-Annotated"20+=== "Python 3.7+ non-Annotated"21 22 !!! tip23 Prefer to use the `Annotated` version if possible.24 6 unmodified lines31 ## Click Custom Type32 33 If you already have a <a href="https://click.palletsprojects.com/en/8.1.x/parameters/#implementing-custom-types" class="external-link" target="_blank">Click Custom Type</a>, you can use it in `typer.Argument()` and `typer.Option()` with the `click_type` parameter.34 35-=== "Python 3.6+"35+=== "Python 3.7+"36 37 ```Python hl_lines="14-18 22-25"38 {!> ../docs_src/parameter_types/custom_types/tutorial002_an.py!}39 ```40 41-=== "Python 3.6+ non-Annotated"41+=== "Python 3.7+ non-Annotated"42 43 !!! tip44 Prefer to use the `Annotated` version if possible.45 3 unmodified linesmodified:docs/tutorial/parameter-types/datetime.md+2 added-2 removed
56 unmodified lines57 * separated with "`/`"58 59 ...It's a crazy example, but let's say you also needed that strange format:60 61-=== "Python 3.6+"61+=== "Python 3.7+"62 63 ```Python hl_lines="11"64 {!> ../docs_src/parameter_types/datetime/tutorial002_an.py!}65 ```66 67-=== "Python 3.6+ non-Annotated"67+=== "Python 3.7+ non-Annotated"68 69 !!! tip70 Prefer to use the `Annotated` version if possible.71 24 unmodified linesmodified:docs/tutorial/parameter-types/enum.md+4 added-4 removed
49 unmodified lines50 ### Case insensitive Enum choices51 52 You can make an `Enum` (choice) *CLI parameter* be case-insensitive with the `case_sensitive` parameter:53 54-=== "Python 3.6+"54+=== "Python 3.7+"55 56 ```Python hl_lines="15"57 {!> ../docs_src/parameter_types/enum/tutorial002_an.py!}58 ```59 60-=== "Python 3.6+ non-Annotated"60+=== "Python 3.7+ non-Annotated"61 62 !!! tip63 Prefer to use the `Annotated` version if possible.64 22 unmodified lines87 ### List of Enum values88 89 A *CLI parameter* can also take a list of `Enum` values:90 91-=== "Python 3.6+"91+=== "Python 3.7+"92 93 ```Python hl_lines="14"94 {!> ../docs_src/parameter_types/enum/tutorial003_an.py!}95 ```96 97-=== "Python 3.6+ non-Annotated"97+=== "Python 3.7+ non-Annotated"98 99 !!! tip100 Prefer to use the `Annotated` version if possible.101 35 unmodified linesmodified:docs/tutorial/parameter-types/file.md+10 added-10 removed
40 unmodified lines41 ```42 43 You will get all the correct editor support, attributes, methods, etc for the file-like object:`44 45-=== "Python 3.6+"45+=== "Python 3.7+"46 47 ```Python hl_lines="5"48 {!> ../docs_src/parameter_types/file/tutorial001_an.py!}49 ```50 51-=== "Python 3.6+ non-Annotated"51+=== "Python 3.7+ non-Annotated"52 53 !!! tip54 Prefer to use the `Annotated` version if possible.55 25 unmodified lines81 ## `FileTextWrite`82 83 For writing text, you can use `typer.FileTextWrite`:84 85-=== "Python 3.6+"85+=== "Python 3.7+"86 87 ```Python hl_lines="5-6"88 {!> ../docs_src/parameter_types/file/tutorial002_an.py!}89 ```90 91-=== "Python 3.6+ non-Annotated"91+=== "Python 3.7+ non-Annotated"92 93 !!! tip94 Prefer to use the `Annotated` version if possible.95 39 unmodified lines135 You will receive `bytes` from it.136 137 It's useful for reading binary files like images:138 139-=== "Python 3.6+"139+=== "Python 3.7+"140 141 ```Python hl_lines="5"142 {!> ../docs_src/parameter_types/file/tutorial003_an.py!}143 ```144 145-=== "Python 3.6+ non-Annotated"145+=== "Python 3.7+ non-Annotated"146 147 !!! tip148 Prefer to use the `Annotated` version if possible.149 27 unmodified lines177 Have in mind that you have to pass `bytes` to its `.write()` method, not `str`.178 179 If you have a `str`, you have to encode it first to get `bytes`.180 181-=== "Python 3.6+"181+=== "Python 3.7+"182 183 ```Python hl_lines="5"184 {!> ../docs_src/parameter_types/file/tutorial004_an.py!}185 ```186 187-=== "Python 3.6+ non-Annotated"187+=== "Python 3.7+ non-Annotated"188 189 !!! tip190 Prefer to use the `Annotated` version if possible.191 48 unmodified lines240 You can override the `mode` from the defaults above.241 242 For example, you could use `mode="a"` to write "appending" to the same file:243 244-=== "Python 3.6+"244+=== "Python 3.7+"245 246 ```Python hl_lines="5"247 {!> ../docs_src/parameter_types/file/tutorial005_an.py!}248 ```249 250-=== "Python 3.6+ non-Annotated"250+=== "Python 3.7+ non-Annotated"251 252 !!! tip253 Prefer to use the `Annotated` version if possible.254 49 unmodified linesmodified:docs/tutorial/parameter-types/number.md+6 added-6 removed
1 You can define numeric validations with `max` and `min` values for `int` and `float` *CLI parameters*:2 3-=== "Python 3.6+"3+=== "Python 3.7+"4 5 ```Python hl_lines="6-8"6 {!> ../docs_src/parameter_types/number/tutorial001_an.py!}7 ```8 9-=== "Python 3.6+ non-Annotated"9+=== "Python 3.7+ non-Annotated"10 11 !!! tip12 Prefer to use the `Annotated` version if possible.13 69 unmodified lines83 You might want to, instead of showing an error, use the closest minimum or maximum valid values.84 85 You can do it with the `clamp` parameter:86 87-=== "Python 3.6+"87+=== "Python 3.7+"88 89 ```Python hl_lines="6-8"90 {!> ../docs_src/parameter_types/number/tutorial002_an.py!}91 ```92 93-=== "Python 3.6+ non-Annotated"93+=== "Python 3.7+ non-Annotated"94 95 !!! tip96 Prefer to use the `Annotated` version if possible.97 27 unmodified lines125 ## Counter *CLI options*126 127 You can make a *CLI option* work as a counter with the `counter` parameter:128 129-=== "Python 3.6+"129+=== "Python 3.7+"130 131 ```Python hl_lines="5"132 {!> ../docs_src/parameter_types/number/tutorial003_an.py!}133 ```134 135-=== "Python 3.6+ non-Annotated"135+=== "Python 3.7+ non-Annotated"136 137 !!! tip138 Prefer to use the `Annotated` version if possible.139 43 unmodified linesmodified:docs/tutorial/parameter-types/path.md+4 added-4 removed
1 You can declare a *CLI parameter* to be a standard Python <a href="https://docs.python.org/3/library/pathlib.html#basic-use" class="external-link" target="_blank">`pathlib.Path`</a>.2 3 This is what you would do for directory paths, file paths, etc:4 5-=== "Python 3.6+"5+=== "Python 3.7+"6 7 ```Python hl_lines="1 8"8 {!> ../docs_src/parameter_types/path/tutorial001_an.py!}9 ```10 11-=== "Python 3.6+ non-Annotated"11+=== "Python 3.7+ non-Annotated"12 13 !!! tip14 Prefer to use the `Annotated` version if possible.15 53 unmodified lines69 All these parameters come directly from <a href="https://click.palletsprojects.com/en/7.x/parameters/#parameter-types" class="external-link" target="_blank">Click</a>.70 71 For example:72 73-=== "Python 3.6+"73+=== "Python 3.7+"74 75 ```Python hl_lines="11-16"76 {!> ../docs_src/parameter_types/path/tutorial002_an.py!}77 ```78 79-=== "Python 3.6+ non-Annotated"79+=== "Python 3.7+ non-Annotated"80 81 !!! tip82 Prefer to use the `Annotated` version if possible.83 44 unmodified linesmodified:docs/tutorial/options-autocompletion.md+18 added-18 removed
12 unmodified lines13 To check it quickly without creating a new Python package, install [Typer CLI](../typer-cli.md){.internal-link target=_blank}.14 15 Then let's create small example program:16 17-=== "Python 3.6+"17+=== "Python 3.7+"18 19 ```Python20 {!> ../docs_src/options_autocompletion/tutorial001_an.py!}21 ```22 23-=== "Python 3.6+ non-Annotated"23+=== "Python 3.7+ non-Annotated"24 25 !!! tip26 Prefer to use the `Annotated` version if possible.27 32 unmodified lines60 Right now we get completion for the *CLI option* names, but not for the values.61 62 We can provide completion for the values creating an `autocompletion` function, similar to the `callback` functions from [CLI Option Callback and Context](./options/callback-and-context.md){.internal-link target=_blank}:63 64-=== "Python 3.6+"64+=== "Python 3.7+"65 66 ```Python hl_lines="5-6 15"67 {!> ../docs_src/options_autocompletion/tutorial002_an.py!}68 ```69 70-=== "Python 3.6+ non-Annotated"70+=== "Python 3.7+ non-Annotated"71 72 !!! tip73 Prefer to use the `Annotated` version if possible.74 27 unmodified lines102 Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.103 104 Then we can check and return only the values that start with the incomplete value from the command line:105 106-=== "Python 3.6+"106+=== "Python 3.7+"107 108 ```Python hl_lines="7-12"109 {!> ../docs_src/options_autocompletion/tutorial003_an.py!}110 ```111 112-=== "Python 3.6+ non-Annotated"112+=== "Python 3.7+ non-Annotated"113 114 !!! tip115 Prefer to use the `Annotated` version if possible.116 34 unmodified lines151 In the `complete_name()` function, instead of providing one `str` per completion element, we provide a `tuple` with 2 items. The first item is the actual completion string, and the second item is the help text.152 153 So, in the end, we return a `list` of `tuples` of `str`:154 155-=== "Python 3.6+"155+=== "Python 3.7+"156 157 ```Python hl_lines="4-8 11-17"158 {!> ../docs_src/options_autocompletion/tutorial004_an.py!}159 ```160 161-=== "Python 3.6+ non-Annotated"161+=== "Python 3.7+ non-Annotated"162 163 !!! tip164 Prefer to use the `Annotated` version if possible.165 33 unmodified lines199 Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.200 201 That way our function will be a <a href="https://docs.python.org/3.8/glossary.html#index-19" class="external-link" target="_blank">generator</a> that **Typer** (actually Click) can iterate:202 203-=== "Python 3.6+"203+=== "Python 3.7+"204 205 ```Python hl_lines="11-14"206 {!> ../docs_src/options_autocompletion/tutorial005_an.py!}207 ```208 209-=== "Python 3.6+ non-Annotated"209+=== "Python 3.7+ non-Annotated"210 211 !!! tip212 Prefer to use the `Annotated` version if possible.213 25 unmodified lines239 So, for now, take this as a sneak peek 😉.240 241 For this we use a `List` of `str`:242 243-=== "Python 3.6+"243+=== "Python 3.7+"244 245 ```Python hl_lines="9-14"246 {!> ../docs_src/options_autocompletion/tutorial006_an.py!}247 ```248 249-=== "Python 3.6+ non-Annotated"249+=== "Python 3.7+ non-Annotated"250 251 !!! tip252 Prefer to use the `Annotated` version if possible.253 23 unmodified lines277 But you can access the context by declaring a function parameter of type `typer.Context`.278 279 And from that context you can get the current values for each parameter.280 281-=== "Python 3.6+"281+=== "Python 3.7+"282 283 ```Python hl_lines="13-14 16"284 {!> ../docs_src/options_autocompletion/tutorial007_an.py!}285 ```286 287-=== "Python 3.6+ non-Annotated"287+=== "Python 3.7+ non-Annotated"288 289 !!! tip290 Prefer to use the `Annotated` version if possible.291 65 unmodified lines357 You can print to "standard error" with a **Rich** `Console(stderr=True)`.358 359 Using `stderr=True` tells **Rich** that the output should be shown in "standard error".360 361-=== "Python 3.6+"361+=== "Python 3.7+"362 363 ```Python hl_lines="13 16-17"364 {!> ../docs_src/options_autocompletion/tutorial008_an.py!}365 ```366 367-=== "Python 3.6+ non-Annotated"367+=== "Python 3.7+ non-Annotated"368 369 !!! tip370 Prefer to use the `Annotated` version if possible.371 37 unmodified lines409 ## Getting the Context and the raw *CLI parameters*410 411 Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:412 413-=== "Python 3.6+"413+=== "Python 3.7+"414 415 ```Python hl_lines="16"416 {!> ../docs_src/options_autocompletion/tutorial009_an.py!}417 ```418 419-=== "Python 3.6+ non-Annotated"419+=== "Python 3.7+ non-Annotated"420 421 !!! tip422 Prefer to use the `Annotated` version if possible.423 42 unmodified linesmodified:docs/tutorial/package.md+13 added-13 removed
50 unmodified lines51 ```console52 $ poetry add "typer[all]"53 54 // It creates a virtual environment for your project55-Creating virtualenv rick-portal-gun-w31dJa0b-py3.6 in /home/rick/.cache/pypoetry/virtualenvs55+Creating virtualenv rick-portal-gun-w31dJa0b-py3.10 in /home/rick/.cache/pypoetry/virtualenvs56 Using version ^0.1.0 for typer57 58 Updating dependencies59 Resolving dependencies... (1.2s)22 unmodified lines82 83 // Activate that new virtual environment84 $ poetry shell85 86-Spawning shell within /home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.686+Spawning shell within /home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.1087 88 // Open an editor using this new environment, for example VS Code89 $ code ./90 ```82 unmodified lines173 description = ""174 authors = ["Rick Sanchez <rick@example.com>"]175 176 [tool.poetry.dependencies]177-python = "^3.6"177+python = "^3.10"178 typer = {extras = ["all"], version = "^0.1.0"}179 180 [tool.poetry.dev-dependencies]181 pytest = "^5.2"15 unmodified lines197 authors = ["Rick Sanchez <rick@example.com>"]198 readme = "README.md"199 200 [tool.poetry.dependencies]201-python = "^3.6"201+python = "^3.10"202 typer = {extras = ["all"], version = "^0.1.0"}203 204 [tool.poetry.dev-dependencies]205 pytest = "^5.2"22 unmodified lines228 [tool.poetry.scripts]229 rick-portal-gun = "rick_portal_gun.main:app"230 231 [tool.poetry.dependencies]232-python = "^3.6"232+python = "^3.10"233 typer = {extras = ["all"], version = "^0.1.0"}234 235 [tool.poetry.dev-dependencies]236 pytest = "^5.2"64 unmodified lines301 // You can use the which program to check which rick-portal-gun program is available (if any)302 $ which rick-portal-gun303 304 // You get the one from your environment305-/home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.6/bin/rick-portal-gun305+/home/rick/.cache/pypoetry/virtualenvs/rick-portal-gun-w31dJa0b-py3.10/bin/rick-portal-gun306 307 // Try it308 $ rick-portal-gun309 349 unmodified lines659 Found existing installation: rick-portal-gun 0.1.0660 Uninstalling rick-portal-gun-0.1.0:661 Would remove:662 /home/user/.local/bin/rick-portal-gun663- /home/user/.local/lib/python3.6/site-packages/rick_portal_gun-0.1.0.dist-info/*664- /home/user/.local/lib/python3.6/site-packages/rick_portal_gun/*663+ /home/user/.local/lib/python3.10/site-packages/rick_portal_gun-0.1.0.dist-info/*664+ /home/user/.local/lib/python3.10/site-packages/rick_portal_gun/*665 # Proceed (y/n)? $ y666 Successfully uninstalled rick-portal-gun-0.1.0667 ```668 8 unmodified lines677 678 // Notice that it says "Downloading" 🚀679 Collecting rick-portal-gun680 Downloading rick_portal_gun-0.1.0-py3-none-any.whl (1.8 kB)681-Requirement already satisfied: typer[all]<0.0.12,>=0.0.11 in ./.local/lib/python3.6/site-packages (from rick-portal-gun) (0.0.11)682-Requirement already satisfied: click<7.2.0,>=7.1.1 in ./anaconda3/lib/python3.6/site-packages (from typer[all]<0.0.12,>=0.0.11->rick-portal-gun) (7.1.1)683-Requirement already satisfied: colorama; extra == "all" in ./anaconda3/lib/python3.6/site-packages (from typer[all]<0.0.12,>=0.0.11->rick-portal-gun) (0.4.3)684-Requirement already satisfied: shellingham; extra == "all" in ./anaconda3/lib/python3.6/site-packages (from typer[all]<0.0.12,>=0.0.11->rick-portal-gun) (1.3.1)681+Requirement already satisfied: typer[all]<0.0.12,>=0.0.11 in ./.local/lib/python3.10/site-packages (from rick-portal-gun) (0.0.11)682+Requirement already satisfied: click<7.2.0,>=7.1.1 in ./anaconda3/lib/python3.10/site-packages (from typer[all]<0.0.12,>=0.0.11->rick-portal-gun) (7.1.1)683+Requirement already satisfied: colorama; extra == "all" in ./anaconda3/lib/python3.10/site-packages (from typer[all]<0.0.12,>=0.0.11->rick-portal-gun) (0.4.3)684+Requirement already satisfied: shellingham; extra == "all" in ./anaconda3/lib/python3.10/site-packages (from typer[all]<0.0.12,>=0.0.11->rick-portal-gun) (1.3.1)685 Installing collected packages: rick-portal-gun686 Successfully installed rick-portal-gun-0.1.0687 ```688 49 unmodified lines738 [tool.poetry.scripts]739 rick-portal-gun = "rick_portal_gun.main:app"740 741 [tool.poetry.dependencies]742-python = "^3.6"742+python = "^3.10"743 typer = {extras = ["all"], version = "^0.1.0"}744 745 [tool.poetry.dev-dependencies]746 pytest = "^5.2"54 unmodified linesmodified:docs/tutorial/testing.md+3 added-3 removed
83 unmodified lines84 ```console85 $ pytest86 87 ================ test session starts ================88-platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.188+platform linux -- Python 3.10, pytest-5.3.5, py-1.8.1, pluggy-0.13.189 rootdir: /home/user/code/superawesome-cli/app90 plugins: forked-1.1.3, xdist-1.31.0, cov-2.8.191 collected 1 item92 9 unmodified lines102 ## Testing input103 104 If you have a CLI with prompts, like:105 106-=== "Python 3.6+"106+=== "Python 3.7+"107 108 ```Python hl_lines="8"109 {!> ../docs_src/testing/app02_an/main.py!}110 ```111 112-=== "Python 3.6+ non-Annotated"112+=== "Python 3.7+ non-Annotated"113 114 !!! tip115 Prefer to use the `Annotated` version if possible.116 108 unmodified linesmodified:docs/features.md+1 added-1 removed
6 unmodified lines7 It follows the same design and ideas. If you know **FastAPI**, you already know **Typer**... more or less.8 9 ## Just Modern Python10 11-It's all based on standard **Python 3.6 type** declarations. No new syntax to learn. Just standard modern Python.11+It's all based on standard **Python type** declarations. No new syntax to learn. Just standard modern Python.12 13 If you need a 2 minute refresher of how to use Python types (even if you don't use FastAPI or Typer), check the FastAPI tutorial section: <a href="https://fastapi.tiangolo.com/python-types/" class="external-link" target="_blank">Python types intro</a>.14 15 You will also see a 20 seconds refresher on the section [Tutorial - User Guide: First Steps](tutorial/first-steps.md){.internal-link target=_blank}.70 unmodified linesmodified:docs/index.md+2 added-4 removed
24 unmodified lines25 **Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>26 27 ---28 29-Typer is a library for building <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users will **love using** and developers will **love creating**. Based on Python 3.6+ type hints.29+Typer is a library for building <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users will **love using** and developers will **love creating**. Based on Python type hints.30 31 The key features are:32 33 * **Intuitive to write**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.11 unmodified lines45 And it's intended to be the FastAPI of CLIs.46 47 ## Requirements48 49-Python 3.6+50-49 **Typer** stands on the shoulders of a giant. Its only internal dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.50 51 ## Installation52 221 unmodified lines274 You do that with standard modern Python types.275 276 You don't have to learn a new syntax, the methods or classes of a specific library, etc.277 280-Just standard **Python 3.6+**.278+Just standard **Python**.279 280 For example, for an `int`:281 282 ```Python33 unmodified linesdeleted:scripts/netlify-docs.sh-14 removed
1-#!/usr/bin/env bash2-set -x3-set -e4-# Install pip5-cd /tmp6-curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py7-python3.6 get-pip.py --user8-cd -9-# Install Flit to be able to install all10-python3.6 -m pip install --user flit11-# Install with Flit12-python3.6 -m flit install --user --deps develop13-# Finally, run mkdocs14-python3.6 -m mkdocs buildmodified:tests/test_tutorial/test_exceptions/test_tutorial001.py+2 added-12 removed
21 unmodified lines22 assert "return get_command(self)(*args, **kwargs)" not in result.stderr23 24 assert "typer.run(main)" not in result.stderr25 assert "print(name + 3)" in result.stderr26-27- # TODO: when deprecating Python 3.6, remove second option28- assert (29- 'TypeError: can only concatenate str (not "int") to str' in result.stderr26+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr30- or "TypeError: must be str, not int" in result.stderr31- )27 assert "name = 'morty'" in result.stderr28 29 30 def test_standard_traceback_env_var():8 unmodified lines39 assert "return get_command(self)(*args, **kwargs)" in result.stderr40 41 assert "typer.run(main)" in result.stderr42 assert "print(name + 3)" in result.stderr48-49- # TODO: when deprecating Python 3.6, remove second option50- assert (51- 'TypeError: can only concatenate str (not "int") to str' in result.stderr43+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr52- or "TypeError: must be str, not int" in result.stderr53- )44 assert "name = 'morty'" not in result.stderr45 46 47 def test_script():7 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial002.py+2 added-12 removed
21 unmodified lines22 assert "return get_command(self)(*args, **kwargs)" not in result.stderr23 24 assert "app()" not in result.stderr25 assert "print(password + 3)" in result.stderr26-27- # TODO: when deprecating Python 3.6, remove second option28- assert (29- 'TypeError: can only concatenate str (not "int") to str' in result.stderr26+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr30- or "TypeError: must be str, not int" in result.stderr31- )27 assert "name = 'morty'" not in result.stderr28 29 30 def test_standard_traceback_env_var():8 unmodified lines39 assert "return get_command(self)(*args, **kwargs)" in result.stderr40 41 assert "app()" in result.stderr42 assert "print(password + 3)" in result.stderr48-49- # TODO: when deprecating Python 3.6, remove second option50- assert (51- 'TypeError: can only concatenate str (not "int") to str' in result.stderr43+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr52- or "TypeError: must be str, not int" in result.stderr53- )44 assert "name = 'morty'" not in result.stderr45 46 47 def test_script():7 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial003.py+1 added-6 removed
21 unmodified lines22 assert "return get_command(self)(*args, **kwargs)" not in result.stderr23 24 assert "app()" in result.stderr25 assert "print(name + 3)" in result.stderr26-27- # TODO: when deprecating Python 3.6, remove second option28- assert (29- 'TypeError: can only concatenate str (not "int") to str' in result.stderr26+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr30- or "TypeError: must be str, not int" in result.stderr31- )27 assert "name = 'morty'" in result.stderr28 29 30 def test_script():7 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial004.py+1 added-5 removed
21 unmodified lines22 assert "return get_command(self)(*args, **kwargs)" in result.stderr23 24 assert "app()" in result.stderr25 assert "print(name + 3)" in result.stderr26- # TODO: when deprecating Python 3.6, remove second option27- assert (28- 'TypeError: can only concatenate str (not "int") to str' in result.stderr26+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr29- or "TypeError: must be str, not int" in result.stderr30- )27 28 29 def test_script():30 result = subprocess.run(6 unmodified linesmodified:tests/test_tracebacks.py+3 added-15 removed
15 unmodified lines16 assert "return get_command(self)(*args, **kwargs)" not in result.stderr17 18 assert "typer.run(main)" in result.stderr19 assert "print(name + 3)" in result.stderr20- # TODO: when deprecating Python 3.6, remove second option21- assert (22- 'TypeError: can only concatenate str (not "int") to str' in result.stderr20+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr23- or "TypeError: must be str, not int" in result.stderr24- )21 22 23 def test_traceback_no_rich_short_disable():24 file_path = Path(__file__).parent / "assets/type_error_no_rich_short_disable.py"7 unmodified lines32 assert "return get_command(self)(*args, **kwargs)" not in result.stderr33 34 assert "app()" in result.stderr35 assert "print(name + 3)" in result.stderr40- # TODO: when deprecating Python 3.6, remove second option41- assert (42- 'TypeError: can only concatenate str (not "int") to str' in result.stderr36+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr43- or "TypeError: must be str, not int" in result.stderr44- )37 38 39 def test_unmodified_traceback():40 file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py"10 unmodified lines51 "even after the hook is installed"52 )53 assert "typer.main.get_command(broken_app)()" in result.stderr54 assert "print(name + 3)" in result.stderr63- # TODO: when deprecating Python 3.6, remove second option64- assert (65- 'TypeError: can only concatenate str (not "int") to str' in result.stderr55+ assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr66- or "TypeError: must be str, not int" in result.stderr67- )modified:.gitignore+1 added-2 removed
1 .vscode2 *.pyc3 __pycache__4-env3.75-env3.64+.venv*5 env6 dist7 .mypy_cache8 .idea5 unmodified linesmodified:pyproject.toml+1 added-2 removed
19 unmodified lines20 "Typing :: Typed",21 "Development Status :: 4 - Beta",22 "Intended Audience :: Developers",23 "Programming Language :: Python :: 3 :: Only",24- "Programming Language :: Python :: 3.6",24 "Programming Language :: Python :: 3.7",25 "Programming Language :: Python :: 3.8",26 "Programming Language :: Python :: 3.9",27 "Programming Language :: Python :: 3.10",4 unmodified lines32 "click >= 7.1.1, <9.0.0",33 "typing-extensions >= 3.7.4.3",34 ]35 description-file = "README.md"37-requires-python = ">=3.6"36+requires-python = ">=3.7"37 38 [tool.flit.metadata.urls]39 Documentation = "https://typer.tiangolo.com/"40 55 unmodified linesmodified:README.md+2 added-4 removed
24 unmodified lines25 **Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>26 27 ---28 29-Typer is a library for building <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users will **love using** and developers will **love creating**. Based on Python 3.6+ type hints.29+Typer is a library for building <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users will **love using** and developers will **love creating**. Based on Python type hints.30 31 The key features are:32 33 * **Intuitive to write**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.11 unmodified lines45 And it's intended to be the FastAPI of CLIs.46 47 ## Requirements48 49-Python 3.6+50-49 **Typer** stands on the shoulders of a giant. Its only internal dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.50 51 ## Installation52 221 unmodified lines274 You do that with standard modern Python types.275 276 You don't have to learn a new syntax, the methods or classes of a specific library, etc.277 280-Just standard **Python 3.6+**.278+Just standard **Python**.279 280 For example, for an `int`:281 282 ```Python33 unmodified lines