Skip to content

Commit 2d529b2

Browse files
svlandegpre-commit-ci-lite[bot]tiangolo
authored
📝 Add reference (code API) docs (#1504)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
1 parent 1db87e0 commit 2d529b2

File tree

12 files changed

+2670
-176
lines changed

12 files changed

+2670
-176
lines changed

docs/reference/context.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Context
2+
3+
When you create a Typer application it uses Click underneath.
4+
And every Click application has a special object called a [`Context`](https://click.palletsprojects.com/en/stable/api/#click.Context) that is normally hidden.
5+
But you can access the context by declaring a function parameter of type `typer.Context`.
6+
7+
The same way you can access the context by declaring a function parameter with its value,
8+
you can declare another function parameter with type `typer.CallbackParam` to get the specific Click [`Parameter`](https://click.palletsprojects.com/en/stable/api/#click.Parameter) object.
9+
10+
```python
11+
from typing import Annotated
12+
13+
import typer
14+
15+
app = typer.Typer()
16+
17+
18+
def name_callback(ctx: typer.Context, param: typer.CallbackParam, value: str):
19+
if ctx.resilient_parsing:
20+
return
21+
print(f"Validating param: {param.name}")
22+
if value != "Rick":
23+
raise typer.BadParameter("Only Rick is allowed")
24+
return value
25+
26+
27+
@app.command()
28+
def main(name: Annotated[str | None, typer.Option(callback=name_callback)] = None):
29+
print(f"Hello {name}")
30+
31+
32+
if __name__ == "__main__":
33+
app()
34+
```
35+
36+
::: typer.Context
37+
38+
::: typer.CallbackParam

docs/reference/file_objects.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# File objects
2+
3+
When you want to declare some types of files, you can use `Path`.
4+
However, in some cases you may need to have access to a file-like object, and then you can use these [special File types](https://typer.tiangolo.com/tutorial/parameter-types/file/).
5+
6+
These objects can be imported from `typer` directly:
7+
8+
```python
9+
from typer import FileBinaryRead, FileBinaryWrite, FileText, FileTextWrite
10+
```
11+
12+
::: typer.FileText
13+
14+
::: typer.FileTextWrite
15+
16+
::: typer.FileBinaryRead
17+
18+
::: typer.FileBinaryWrite

docs/reference/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Reference
2+
3+
Here's the reference or code API, the classes, functions, parameters, attributes, and
4+
all the Typer parts you can use in your applications.
5+
6+
If you want to **learn Typer** you are much better off reading the
7+
[Typer Tutorial](https://typer.tiangolo.com/tutorial/).

docs/reference/parameters.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Parameters
2+
3+
Parameters to our command line interface may be both [CLI Options](https://typer.tiangolo.com/tutorial/options/) and [CLI Arguments](https://typer.tiangolo.com/tutorial/arguments/):
4+
5+
```python
6+
from typing import Annotated
7+
import typer
8+
9+
app = typer.Typer()
10+
11+
@app.command()
12+
def register(
13+
user: Annotated[str, typer.Argument()],
14+
age: Annotated[int, typer.Option(min=18)],
15+
score: Annotated[float, typer.Option(max=100)] = 0,
16+
):
17+
print(f"User is {user}")
18+
print(f"--age is {age}")
19+
print(f"--score is {score}")
20+
21+
if __name__ == "__main__":
22+
app()
23+
```
24+
25+
::: typer.Argument
26+
options:
27+
show_overloads: false
28+
29+
::: typer.Option
30+
options:
31+
show_overloads: false

docs/reference/run_launch.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `run` and `launch`
2+
3+
These two functions can be imported from `typer` directly:
4+
5+
```python
6+
from typer import launch, run
7+
```
8+
9+
::: typer.run
10+
11+
::: typer.launch

docs/reference/typer.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `Typer` class
2+
3+
Here's the reference information for the `Typer` class, with all its parameters, attributes and methods.
4+
5+
You can import the `Typer` class directly from `typer`:
6+
7+
```python
8+
from typer import Typer
9+
```
10+
11+
::: typer.Typer
12+
options:
13+
members:
14+
- callback
15+
- command
16+
- add_typer

mkdocs.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ plugins:
6767
redirects:
6868
redirect_maps:
6969
typer-cli.md: tutorial/typer-command.md
70+
mkdocstrings:
71+
handlers:
72+
python:
73+
options:
74+
extensions:
75+
- griffe_typingdoc
76+
show_root_heading: true
77+
show_if_no_docstring: true
78+
inherited_members: true
79+
members_order: source
80+
separate_signature: true
81+
unwrap_annotated: true
82+
filters:
83+
- '!^_'
84+
merge_init_into_class: true
85+
docstring_section_style: spacy
86+
signature_crossrefs: true
87+
show_symbol_type_heading: true
88+
show_symbol_type_toc: true
7089

7190
nav:
7291
- Typer: index.md
@@ -138,6 +157,13 @@ nav:
138157
- tutorial/exceptions.md
139158
- tutorial/one-file-per-command.md
140159
- tutorial/typer-command.md
160+
- Reference (Code API):
161+
- reference/index.md
162+
- reference/typer.md
163+
- reference/run_launch.md
164+
- reference/parameters.md
165+
- reference/file_objects.md
166+
- reference/context.md
141167
- Resources:
142168
- resources/index.md
143169
- help-typer.md

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ classifiers = [
3535
]
3636
dependencies = [
3737
"click >= 8.0.0",
38+
"annotated-doc >=0.0.2",
3839
]
3940
readme = "README.md"
4041

0 commit comments

Comments
 (0)