Debian Codegraph @

docs/guide/users/how-to/parse-docstrings.md

raw symbols SCIP
1 # Using Griffe as a docstring-parsing library
2
3 You can use Griffe to parse arbitrary docstrings. You don't have to load anything through the Griffe loader. You just need to import the [`Docstring`][griffe.Docstring] class. Then you can build a `Docstring` instance and call its `parse` method, choosing the parsing-style to use:
4
5 ```python
6 from griffe import Docstring
7
8 text = "Hello I'm a docstring!"
9 docstring = Docstring(text, lineno=1)
10 parsed = docstring.parse("google")
11 ```
12
13 If you want to take advantage of the parsers ability to fetch annotations from the object from which the docstring originates, you can manually create the parent objects and link them to the docstring:
14
15 ```python
16 from griffe import Docstring, Function, Parameters, Parameter, ParameterKind
17
18 function = Function(
19 "func",
20 parameters=Parameters(
21 Parameter("param1", annotation="str", kind=ParameterKind.positional_or_keyword),
22 Parameter("param2", annotation="int", kind=ParameterKind.keyword_only),
23 ),
24 )
25 text = """
26 Hello I'm a docstring!
27
28 Parameters:
29 param1: Description.
30 param2: Description.
31 """
32 docstring = Docstring(text, lineno=1, parent=function)
33 parsed = docstring.parse("google")
34 ```
35
36 With this the parser will fetch the `str` and `int` annotations from the parent function's parameters.