Debian Codegraph @

docs/source/vs_typer/docstring/README.rst

raw
1 .. _Typer Docstring Parsing:
2
3 =================
4 Docstring Parsing
5 =================
6 Typer performs no docstring parsing.
7 Frequently, Typer's Argument/Option is only used to provide a ``help`` string.
8 However, this ``help`` string commonly mirrors the function's docstring.
9
10 Consider the following Typer program:
11
12 .. code-block:: python
13
14 import typer
15
16 typer_app = typer.Typer()
17
18 @typer_app.callback()
19 def dummy():
20 # So that ``foo`` is considered a command.
21 pass
22
23 @typer_app.command()
24 def foo(bar):
25 """Foo Docstring.
26
27 Parameters
28 ----------
29 bar: str
30 Bar parameter docstring.
31 """
32
33 typer_app()
34
35 .. code-block:: console
36
37 $ my-script --help
38 ╭─ Commands ────────────────────────────────────────────────────────────╮
39 │ foo Foo Docstring. │
40 ╰───────────────────────────────────────────────────────────────────────╯
41
42 $ my-script foo --help
43 Foo Docstring.
44 Parameters ---------- bar: str Bar parameter docstring.
45
46 ╭─ Arguments ───────────────────────────────────────────────────────────╮
47 │ * bar TEXT [default: None] [required] │
48 ╰───────────────────────────────────────────────────────────────────────╯
49
50 The ``foo`` command's short description was properly parsed from the docstring.
51 However, it mangles the Numpy-style docstring (or any docstring format for that matter) and doesn't correctly display ``bar``'s help.
52 Typer just displays the entire docstring.
53
54 To achieve the desired result with Typer, we have to explicitly annotate the parameter ``bar``:
55
56 .. code-block:: python
57
58 @typer_app.command()
59 def foo(bar: Annotated[str, Argument(help="Bar parameter docstring.")]):
60 ...
61
62 For any serious application, this means that every function parameter must be annotated this way, significantly bloating the function signature.
63
64 Compare this to Cyclopts:
65
66 .. code-block:: python
67
68 import cyclopts
69
70 cyclopts_app = cyclopts.App()
71
72 @cyclopts_app.command()
73 def foo(bar):
74 """Foo Docstring.
75
76 Parameters
77 ----------
78 bar: str
79 Bar parameter docstring.
80 """
81
82 cyclopts_app()
83
84 .. code-block:: console
85
86 $ my-script --help
87 ╭─ Commands ────────────────────────────────────────────────────────────╮
88 │ foo Foo Docstring. │
89 ╰───────────────────────────────────────────────────────────────────────╯
90
91 $ my-script foo --help
92
93 Foo Docstring.
94
95 ╭─ Parameters ──────────────────────────────────────────────────────────╮
96 │ * BAR,--bar Bar parameter docstring. [required] │
97 ╰───────────────────────────────────────────────────────────────────────╯
98
99 Cyclopts did not mangle the docstring into the long description, and it correctly parsed ``bar``'s help.
100 This ends up significantly simplifying function signatures in the common situation where just a help string needs to be added.
101 The common case in Cyclopts does not require the lengthy ``Annotated[str, Parameter(help="Bar parameter docstring")]``.
102
103 Internally, Cyclopts uses the excellent `docstring_parser`_ library for parsing docstrings. Check their project out!
104
105 .. _docstring_parser: https://github.com/rr-/docstring_parser