Debian Codegraph @

docs/guide/users/extending.md

raw symbols SCIP
1 # Extending APIs
2
3 Griffe has an extension system that can be used to enhance or customize the data that Griffe collects. Extensions are written in Python.
4
5 ## Using extensions
6
7 Extensions can be specified both on the command-line (in the terminal), and programmatically (in Python).
8
9 ### On the command-line
10
11 On the command-line, you can specify extensions to use with the `-e`, `--extensions` option. This option accepts a single positional argument which can take two forms:
12
13 - a comma-separated list of extensions
14 - a JSON list of extensions
15
16 Extensions can accept options: the comma-separated list does not allow to specify options, while the JSON list does. See examples below.
17
18 With both forms, each extension refers to one of these three things:
19
20 - the name of a built-in extension's module, for example `dynamic_docstrings` (this is just an example, this built-in extension does not exist)
21 - the Python dotted-path to a module containing one or more extensions, or to an extension directly, for example `package.module` and `package.module.ThisExtension`
22 - the file path to a Python script, and an optional extension name, separated by a colon, for example `scripts/griffe_exts.py` and `scripts/griffe_exts.py:ThisExtension`
23
24 The specified extension modules can contain more than one extension: Griffe will pick up and load every extension declared or imported within the modules. If options are specified for a module that contains multiple extensions, the same options will be passed to all the extensions, so extension writers must make sure that all extensions within a single module accept the same options. If they don't, Griffe will abort with an error.
25
26 To specify options in the JSON form, use a dictionary instead of a string: the dictionary's only key is the extension identifier (built-in name, Python path, file path) and its value is a dictionary of options.
27
28 Some examples:
29
30 ```bash
31 griffe dump griffe -e pydantic,scripts/exts.py:DynamicDocstrings,griffe_attrs
32 ```
33
34 ```bash
35 griffe check --search src griffe -e '[
36 {"pydantic": {"schema": true}},
37 {
38 "scripts/exts.py:DynamicDocstrings": {
39 "paths": ["mypkg.mymod.myobj"]
40 }
41 },
42 "griffe_attrs"
43 ]'
44 ```
45
46 In the above two examples, `pydantic` would be a built-in extension, `scripts/exts.py:DynamicDocstrings` the file path plus name of a local extension, and `griffe_attrs` the name of a third-party package that exposes one or more extensions.
47
48 ### Programmatically
49
50 Within Python code, extensions can be specified with the `extensions` parameter of the [`GriffeLoader` class][griffe.GriffeLoader] or [`load` function][griffe.load].
51
52 The parameter accepts an instance of the [`Extensions` class][griffe.Extensions]. Such an instance is created with the help of the [`load_extensions` function][griffe.load_extensions], which itself accepts a list of strings, dictionaries, extension classes and extension instances.
53
54 Strings and dictionaries are used the same way as [on the command-line](#on-the-command-line). Extension instances are used as such, and extension classes are instantiated without any options.
55
56 Example:
57
58 ```python
59 import griffe
60
61 from mypackage.extensions import ThisExtension, ThisOtherExtension
62
63 extensions = griffe.load_extensions(
64 {"pydantic": {"schema": true}},
65 {"scripts/exts.py:DynamicDocstrings": {"paths": ["mypkg.mymod.myobj"]}},
66 "griffe_attrs",
67 ThisExtension(option="value"),
68 ThisOtherExtension,
69 )
70
71 data = griffe.load("mypackage", extensions=extensions)
72 ```
73
74 ### In MkDocs
75
76 MkDocs and its mkdocstrings plugin can be configured to use Griffe extensions:
77
78 ```yaml title="mkdocs.yml"
79 plugins:
80 - mkdocstrings:
81 handlers:
82 python:
83 options:
84 extensions:
85 - pydantic: {schema: true}
86 - scripts/exts.py:DynamicDocstrings:
87 paths: [mypkg.mymod.myobj]
88 - griffe_attrs
89 ```
90
91 The `extensions` key accepts a list that is passed to the [`load_extensions` function][griffe.load_extensions]. See [how to use extensions programmatically](#programmatically) to learn more.
92
93 ## Writing extensions
94
95 In the next section we give a bit of context on how Griffe works, to show how extensions can integrate into the data collection process. Feel free to skip to the [Events and hooks](#events-and-hooks) section or the [Full example](#full-example) section if you'd prefer to see concrete examples first.
96
97 ### How it works
98
99 To extract information from your Python sources, Griffe tries to build Abstract Syntax Trees by parsing the sources with [`ast`][] utilities.
100
101 If the source code is not available (the modules are built-in or compiled), Griffe imports the modules and builds object trees instead.
102
103 Griffe then follows the [Visitor pattern](https://www.wikiwand.com/en/Visitor_pattern) to walk the tree and extract information. For ASTs, Griffe uses its [Visitor agent][griffe.Visitor] and for object trees, it uses its [Inspector agent][griffe.Inspector].
104
105 Sometimes during the walk through the source or runtime objects, both the visitor and inspector agents will trigger events, called **analysis events**. These events can be hooked on by extensions to alter or enhance Griffe's behavior. Some hooks will be passed just the current node being visited, others will be passed both the node and an instance of an [Object][griffe.Object] subclass, such as a [Module][griffe.Module], a [Class][griffe.Class], a [Function][griffe.Function], an [Attribute][griffe.Attribute], or a [Type Alias][griffe.TypeAlias]. Extensions will therefore be able to modify these instances.
106
107 Once the Griffe tree for a given package has been fully constructed, Griffe will trigger a second set of events, called **load events**, by walking the tree again. **It is safer to use load events as they are triggered only once data is complete for a given package**, contrary to the analysis events which are triggered *while the Griffe tree is still being built*.
108
109 The following flow chart shows an example of an AST visit. The tree is simplified: actual trees have a lot more nodes like `if/elif/else` nodes, `try/except/else/finally` nodes, [and many more][ast.AST].
110
111 ```mermaid
112 flowchart TB
113 M(Module definition) --- C(Class definition) & F(Function definition)
114 C --- m(Function definition) & A(Variable assignment)
115 ```
116
117 The following flow chart shows an example of an object tree inspection. The tree is simplified as well: [many more types of objects are handled][griffe.ObjectKind].
118
119 ```mermaid
120 flowchart TB
121 M(Module) --- C(Class) & F(Function)
122 C --- m(Method) & A(Attribute)
123 ```
124
125 For a more concrete example, let say that we visit (or inspect) an AST (or object tree) for a given module, and that this module contains a single class, which itself contains a single method:
126
127 - the agent (visitor or inspector) will walk through the tree by starting with the module node
128 - it will instantiate a [Module][griffe.Module], then walk through its members, continuing with the class node
129 - it will instantiate a [Class][griffe.Class], then walk through its members, continuing with the function node
130 - it will instantiate a [Function][griffe.Function]
131 - then it will go back up and finish walking since there are no more nodes to walk through
132
133 Every time the agent enters a node, creates an object instance, or finishes handling members of an object, it will trigger an event.
134
135 The flow of events is drawn in the following flowchart:
136
137 ```mermaid
138 flowchart TB
139 visit_mod{{enter module node}}
140 event_mod_node{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_node'><b><code style='color: var(--md-accent-fg-color)'>on_node</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_module_node'><b><code style='color: var(--md-accent-fg-color)'>on_module_node</code></b></a> event"}}
141 create_mod{{create module instance}}
142 event_mod_instance{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_instance'><b><code style='color: var(--md-accent-fg-color)'>on_instance</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_module_instance'><b><code style='color: var(--md-accent-fg-color)'>on_module_instance</code></b></a> event"}}
143 visit_mod_members{{visit module members}}
144 visit_cls{{enter class node}}
145 event_cls_node{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_node'><b><code style='color: var(--md-accent-fg-color)'>on_node</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_class_node'><b><code style='color: var(--md-accent-fg-color)'>on_class_node</code></b></a> event"}}
146 create_cls{{create class instance}}
147 event_cls_instance{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_instance'><b><code style='color: var(--md-accent-fg-color)'>on_instance</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_class_instance'><b><code style='color: var(--md-accent-fg-color)'>on_class_instance</code></b></a> event"}}
148 visit_cls_members{{visit class members}}
149 visit_func{{enter func node}}
150 event_func_node{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_node'><b><code style='color: var(--md-accent-fg-color)'>on_node</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_function_node'><b><code style='color: var(--md-accent-fg-color)'>on_function_node</code></b></a> event"}}
151 create_func{{create function instance}}
152 event_func_instance{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_instance'><b><code style='color: var(--md-accent-fg-color)'>on_instance</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_function_instance'><b><code style='color: var(--md-accent-fg-color)'>on_function_instance</code></b></a> event"}}
153 event_cls_members{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_members'><b><code style='color: var(--md-accent-fg-color)'>on_members</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_class_members'><b><code style='color: var(--md-accent-fg-color)'>on_class_members</code></b></a> event"}}
154 event_mod_members{{"<a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_members'><b><code style='color: var(--md-accent-fg-color)'>on_members</code></b></a> event<br><a href='/griffe/reference/griffe/extensions/#griffe.Extension.on_module_members'><b><code style='color: var(--md-accent-fg-color)'>on_module_members</code></b></a> event"}}
155
156 start{start} --> visit_mod
157 visit_mod --> event_mod_node
158 event_mod_node --> create_mod
159 create_mod --> event_mod_instance
160 event_mod_instance --> visit_mod_members
161 visit_mod_members --1--> visit_cls
162 visit_cls --> event_cls_node
163 event_cls_node --> create_cls
164 create_cls --> event_cls_instance
165 event_cls_instance --> visit_cls_members
166 visit_cls_members --1--> visit_func
167 visit_func --> event_func_node
168 event_func_node --> create_func
169 create_func --> event_func_instance
170 event_func_instance --> visit_cls_members
171 visit_cls_members --2--> event_cls_members
172 event_cls_members --> visit_mod_members
173 visit_mod_members --2--> event_mod_members
174 event_mod_members --> finish{finish}
175
176 class event_mod_node event
177 class event_mod_instance event
178 class event_cls_node event
179 class event_cls_instance event
180 class event_func_node event
181 class event_func_instance event
182 class event_cls_members event
183 class event_mod_members event
184 classDef event stroke:#3cc,stroke-width:2
185 ```
186
187 Hopefully this flowchart gives you a pretty good idea of what happens when Griffe collects data from a Python module. The next section will explain in more details the different events that are triggered, and how to hook onto them in your extensions.
188
189 ### Events and hooks
190
191 There are two kinds of events in Griffe: [**load events**](#load-events) and [**analysis events**](#analysis-events). Load events are scoped to the Griffe loader (triggered once a package is fully loaded). Analysis events are scoped to the visitor and inspector agents (triggered during static and dynamic analysis).
192
193 **Hooks** are methods that are called when a particular event is triggered. To target a specific event, the hook must be named after it. See [Extensions and hooks](#extensions-and-hooks).
194
195 #### Load events
196
197 **Load events** are triggered once the tree for a given package has been fully constructed.
198
199 There is 1 generic **load event**:
200
201 - [`on_object`][griffe.Extension.on_object]: The "on object" event is triggered on any kind of object (except for aliases and packages, so modules, classes, functions, attributes and type aliases), once the tree for the object's package has been fully constructed.
202
203 There are also specific **load events** for each object kind:
204
205 - [`on_module`][griffe.Extension.on_module]: The "on module" event is triggered on modules.
206 - [`on_class`][griffe.Extension.on_class]: The "on class" event is triggered on classes.
207 - [`on_function`][griffe.Extension.on_function]: The "on function" event is triggered on functions.
208 - [`on_attribute`][griffe.Extension.on_attribute]: The "on attribute" event is triggered on attributes.
209 - [`on_type_alias`][griffe.Extension.on_type_alias]: The "on type alias" event is triggered on type aliases.
210 - [`on_alias`][griffe.Extension.on_alias]: The "on alias" event is triggered on aliases (imported/inherited objects).
211 - [`on_package`][griffe.Extension.on_package]: The "on package" event is triggered on top-level modules (packages) only.
212
213 #### Analysis events
214
215 **Analysis events** are triggered while modules are being scanned (with static or dynamic analysis). Data is incomplete when these events are triggered, so we recommend only hooking onto these events if you know what you are doing. In doubt, prefer using **load events** above.
216
217 There are 3 generic **analysis events**:
218
219 - [`on_node`][griffe.Extension.on_node]: The "on node" events are triggered when the agent (visitor or inspector) starts handling a node in the tree (AST or object tree).
220 - [`on_instance`][griffe.Extension.on_instance]: The "on instance" events are triggered when the agent just created an instance of [Module][griffe.Module], [Class][griffe.Class], [Function][griffe.Function], [Attribute][griffe.Attribute], or [Type Alias][griffe.TypeAlias], and added it as a member of its parent. The "on instance" event is **not** triggered when an [Alias][griffe.Alias] is created.
221 - [`on_members`][griffe.Extension.on_members]: The "on members" events are triggered when the agent just finished handling all the members of an object. Functions, attributes and type aliases do not have members, so there are no "on members" events for these kinds.
222
223 There are also specific **analysis events** for each object kind:
224
225 - [`on_module_node`][griffe.Extension.on_module_node]
226 - [`on_module_instance`][griffe.Extension.on_module_instance]
227 - [`on_module_members`][griffe.Extension.on_module_members]
228 - [`on_class_node`][griffe.Extension.on_class_node]
229 - [`on_class_instance`][griffe.Extension.on_class_instance]
230 - [`on_class_members`][griffe.Extension.on_class_members]
231 - [`on_function_node`][griffe.Extension.on_function_node]
232 - [`on_function_instance`][griffe.Extension.on_function_instance]
233 - [`on_attribute_node`][griffe.Extension.on_attribute_node]
234 - [`on_attribute_instance`][griffe.Extension.on_attribute_instance]
235 - [`on_type_alias_node`][griffe.Extension.on_type_alias_node]
236 - [`on_type_alias_instance`][griffe.Extension.on_type_alias_instance]
237 - [`on_alias_instance`][griffe.Extension.on_alias_instance]
238
239 #### Extensions and hooks
240
241 **Extensions** are classes that inherit from [Griffe's Extension base class][griffe.Extension] and define some hooks as methods:
242
243 ```python
244 import griffe
245
246
247 class MyExtension(griffe.Extension):
248 def on_object(
249 self,
250 *,
251 obj: griffe.Object,
252 loader: griffe.GriffeLoader,
253 **kwargs,
254 ) -> None:
255 """Do something with `obj`."""
256 ```
257
258 Hooks are always defined as methods of a class inheriting from [Extension][griffe.Extension], never as standalone functions. IDEs should autocomplete the signature when you start typing `def` followed by a hook name.
259
260 Since hooks are declared in a class, feel free to also declare state variables (or any other variable) in the `__init__` method:
261
262 ```python
263 import ast
264 from griffe import Extension, Object, ObjectNode
265
266
267 class MyExtension(Extension):
268 def __init__(self) -> None:
269 super().__init__()
270 self.state_thingy = "initial stuff"
271 self.list_of_things = []
272
273 def on_object(
274 self,
275 *,
276 obj: griffe.Object,
277 loader: griffe.GriffeLoader,
278 **kwargs,
279 ) -> None:
280 """Do something with `obj`."""
281 ```
282
283 ### Static/dynamic support
284
285 Extensions can support both static and dynamic analysis of modules.
286
287 Objects have an `analysis` attribute whose value will be `"static"` if they were loaded using static analysis, or `"dynamic"` if they were loaded using dynamic analysis. If the value is `None`, it means the object was created manually (for example by another extension).
288
289 To support static analysis, dynamic analysis, or both in your load events, you can therefore check the value of the `analysis` attribute:
290
291 ```python
292 import griffe
293
294
295 class MyExtension(griffe.Extension):
296 def on_object(self, *, obj: griffe.Object, **kwargs) -> None:
297 """Do something with `obj`."""
298 if obj.analysis == "static":
299 ... # Apply logic for static analysis.
300 elif obj.analysis == "dynamic":
301 ... # Apply logic for dynamic analysis.
302 else:
303 ... # Apply logic for manually built objects.
304 ```
305
306 ### Visiting trees
307
308 Extensions provide basic functionality to help you visit trees during analysis of the code:
309
310 - [`visit`][griffe.Extension.visit]: call `self.visit(node)` to start visiting an abstract syntax tree.
311 - [`generic_visit`][griffe.Extension.generic_visit]: call `self.generic_visit(node)` to visit each subnode of a given node.
312 - [`inspect`][griffe.Extension.inspect]: call `self.inspect(node)` to start visiting an object tree. Nodes contain references to the runtime objects, see [`ObjectNode`][griffe.ObjectNode].
313 - [`generic_inspect`][griffe.Extension.generic_inspect]: call `self.generic_inspect(node)` to visit each subnode of a given node.
314
315 Calling `self.visit(node)` or `self.inspect(node)` will do nothing unless you actually implement methods that handle specific types of nodes:
316
317 - for ASTs, methods must be named `visit_<node_type>` where `<node_type>` is replaced with the lowercase name of the node's class. For example, to allow visiting [`ClassDef`][ast.ClassDef] nodes, you must implement the `visit_classdef` method:
318
319 ```python
320 import ast
321 from griffe import Extension
322
323
324 class MyExtension(Extension):
325 def visit_classdef(node: ast.ClassDef) -> None:
326 # Do something with the node...
327 ...
328 # ...then visit the subnodes
329 # (it only makes sense if you implement other methods
330 # such as visit_functiondef or visit_assign for example).
331 self.generic_visit(node)
332 ```
333
334 See the [list of existing AST classes](#ast-nodes) to learn what method you can implement.
335
336 - for object trees, methods must be named `inspect_<node_type>`, where `<node_type>` is replaced with the string value of the node's kind. The different kinds are listed in the [`ObjectKind`][griffe.ObjectKind] enumeration. For example, to allow inspecting coroutine nodes, you must implement the `inspect_coroutine` method:
337
338 ```python
339 from griffe import Extension, ObjectNode
340
341
342 class MyExtension(Extension):
343 def inspect_coroutine(node: ObjectNode) -> None:
344 # Do something with the node...
345 ...
346 # ...then visit the subnodes if it makes sense.
347 self.generic_inspect(node)
348 ```
349
350 ### Triggering other extensions
351
352 If your extension creates new objects, you might want to trigger the other enabled extensions on these object instances. To do this you can use [`agent.extensions.call`][griffe.Extensions.call]:
353
354 ```python
355 import ast
356 import griffe
357
358
359 class MyExtension(griffe.Extension):
360 # Example from within a load event:
361 def on_package(self, *, pkg: griffe.Module, loader: griffe.GriffeLoader, **kwargs) -> None:
362 # New object created for whatever reason.
363 function = griffe.Function(...)
364
365 # Trigger other extensions.
366 loader.extensions.call("on_function", func=function, loader=loader)
367
368 # Example from within an analysis event:
369 def on_node(self, *, node: ast.AST | griffe.ObjectNode, agent: griffe.Visitor | griffe.Inspector, **kwargs) -> None:
370 # New object created for whatever reason.
371 function = griffe.Function(...)
372
373 # Trigger other extensions.
374 agent.extensions.call("on_function_instance", node=node, agent=agent, func=function, **kwargs)
375 ```
376
377 ### Extra data
378
379 All Griffe objects (modules, classes, functions, attributes, type aliases) can store additional (meta)data in their `extra` attribute. This attribute is a dictionary of dictionaries. The first layer is used as namespacing: each extension writes into its own namespace, or integrates with other projects by reading/writing in their namespaces, according to what they support and document.
380
381 ```python
382 import griffe
383
384 self_namespace = "my_extension"
385
386
387 class MyExtension(griffe.Extension):
388 def on_object(self, obj: griffe.Object, **kwargs) -> None:
389 obj.extra[self_namespace]["some_key"] = "some_value"
390 ```
391
392 For example, [mkdocstrings-python](https://mkdocstrings.github.io/python) looks into the `mkdocstrings` namespace for a `template` key. Extensions can therefore provide a custom template value by writing into `extra["mkdocstrings"]["template"]`:
393
394 ```python
395 import griffe
396
397 self_namespace = "my_extension"
398 mkdocstrings_namespace = "mkdocstrings"
399
400
401 class MyExtension(griffe.Extension):
402 def on_class(self, cls: griffe.Class, **kwargs) -> None:
403 cls.extra[mkdocstrings_namespace]["template"] = "my_custom_template"
404 ```
405
406 [Read more about mkdocstrings handler extensions.](https://mkdocstrings.github.io/usage/handlers/#handler-extensions)
407
408 ### Options
409
410 Extensions can be made to support options. These options can then be passed from the [command-line](#on-the-command-line) using JSON, from Python directly, or from other tools like MkDocs, in `mkdocs.yml`.
411
412 ```python
413 import griffe
414
415
416 class MyExtension(griffe.Extension):
417 def __init__(self, option1: str, option2: bool = False) -> None:
418 super().__init__()
419 self.option1 = option1
420 self.option2 = option2
421
422 def on_attribute(self, attr: griffe.Attribute, **kwargs) -> None:
423 if self.option2:
424 ... # Do something.
425 ```
426
427 ### Logging
428
429 To better integrate with Griffe and other tools in the ecosystem (notably MkDocs), use Griffe loggers to log messages:
430
431 ```python
432 import griffe
433
434 logger = griffe.get_logger(__name__)
435
436
437 class MyExtension(griffe.Extension):
438 def on_module(self, mod: griffe.Module, **kwargs) -> None:
439 logger.info("Doing some work on module %s", mod.path)
440 ```
441
442 ### Full example
443
444 The following example shows how one could write a "dynamic docstrings" extension that dynamically imports objects that declare their docstrings dynamically, to improve support for such docstrings. The extension is configurable to run only on user-selected objects.
445
446 Package structure (or just write your extension in a local script):
447
448 ```tree
449 ./
450 pyproject.toml
451 src/
452 dynamic_docstrings/
453 __init__.py
454 extension.py
455 ```
456
457 ```python title="./src/dynamic_docstrings/extension.py"
458 import ast
459 import inspect
460 import griffe
461
462 logger = griffe.get_logger(__name__)
463
464
465 class DynamicDocstrings(griffe.Extension):
466 def __init__(self, object_paths: list[str] | None = None) -> None:
467 self.object_paths = object_paths
468
469 def on_object(
470 self,
471 obj: griffe.Object,
472 loader: griffe.GriffeLoader,
473 **kwargs,
474 ) -> None:
475 if obj.analysis == "dynamic":
476 return # Skip runtime objects, their docstrings are already right.
477
478 if self.object_paths and obj.path not in self.object_paths:
479 return # Skip objects that were not selected.
480
481 # Import object to get its evaluated docstring.
482 try:
483 runtime_obj = griffe.dynamic_import(obj.path)
484 docstring = runtime_obj.__doc__
485 except ImportError:
486 logger.debug(f"Could not get dynamic docstring for {obj.path}")
487 return
488 except AttributeError:
489 logger.debug(f"Object {obj.path} does not have a __doc__ attribute")
490 return
491
492 # Update the object instance with the evaluated docstring.
493 docstring = inspect.cleandoc(docstring)
494 if obj.docstring:
495 obj.docstring.value = docstring
496 else:
497 obj.docstring = griffe.Docstring(
498 docstring,
499 parent=obj,
500 docstring_parser=loader.docstring_parser,
501 docstring_options=loader.docstring_options,
502 )
503 ```
504
505 You can then expose this extension in the top-level module of your package:
506
507 ```python title="./src/dynamic_docstrings/__init__.py"
508 from dynamic_docstrings.extension import DynamicDocstrings
509
510 __all__ = ["DynamicDocstrings"]
511 ```
512
513 This will allow users to load and use this extension by referring to it as `dynamic_docstrings` (your Python package name).
514
515 See [how to use extensions](#using-extensions) to learn more about how to load and use your new extension.
516
517 ## AST nodes
518
519 > <table style="border: none; background-color: unset;"><tbody><tr><td>
520 >
521 > - [`Add`][ast.Add]
522 > - [`alias`][ast.alias]
523 > - [`And`][ast.And]
524 > - [`AnnAssign`][ast.AnnAssign]
525 > - [`arg`][ast.arg]
526 > - [`arguments`][ast.arguments]
527 > - [`Assert`][ast.Assert]
528 > - [`Assign`][ast.Assign]
529 > - [`AsyncFor`][ast.AsyncFor]
530 > - [`AsyncFunctionDef`][ast.AsyncFunctionDef]
531 > - [`AsyncWith`][ast.AsyncWith]
532 > - [`Attribute`][ast.Attribute]
533 > - [`AugAssign`][ast.AugAssign]
534 > - [`Await`][ast.Await]
535 > - [`BinOp`][ast.BinOp]
536 > - [`BitAnd`][ast.BitAnd]
537 > - [`BitOr`][ast.BitOr]
538 > - [`BitXor`][ast.BitXor]
539 > - [`BoolOp`][ast.BoolOp]
540 > - [`Break`][ast.Break]
541 > - `Bytes`[^1]
542 > - [`Call`][ast.Call]
543 > - [`ClassDef`][ast.ClassDef]
544 > - [`Compare`][ast.Compare]
545 > - [`comprehension`][ast.comprehension]
546 > - [`Constant`][ast.Constant]
547 > - [`Continue`][ast.Continue]
548 > - [`Del`][ast.Del]
549 > - [`Delete`][ast.Delete]
550 > - [`Dict`][ast.Dict]
551 >
552 > </td><td>
553 >
554 > - [`DictComp`][ast.DictComp]
555 > - [`Div`][ast.Div]
556 > - `Ellipsis`[^1]
557 > - [`Eq`][ast.Eq]
558 > - [`ExceptHandler`][ast.ExceptHandler]
559 > - [`Expr`][ast.Expr]
560 > - `Expression`[^1]
561 > - `ExtSlice`[^2]
562 > - [`FloorDiv`][ast.FloorDiv]
563 > - [`For`][ast.For]
564 > - [`FormattedValue`][ast.FormattedValue]
565 > - [`FunctionDef`][ast.FunctionDef]
566 > - [`GeneratorExp`][ast.GeneratorExp]
567 > - [`Global`][ast.Global]
568 > - [`Gt`][ast.Gt]
569 > - [`GtE`][ast.GtE]
570 > - [`If`][ast.If]
571 > - [`IfExp`][ast.IfExp]
572 > - [`Import`][ast.Import]
573 > - [`ImportFrom`][ast.ImportFrom]
574 > - [`In`][ast.In]
575 > - `Index`[^2]
576 > - `Interactive`[^3]
577 > - [`Invert`][ast.Invert]
578 > - [`Is`][ast.Is]
579 > - [`IsNot`][ast.IsNot]
580 > - [`JoinedStr`][ast.JoinedStr]
581 > - [`keyword`][ast.keyword]
582 > - [`Lambda`][ast.Lambda]
583 > - [`List`][ast.List]
584 >
585 > </td><td>
586 >
587 > - [`ListComp`][ast.ListComp]
588 > - [`Load`][ast.Load]
589 > - [`LShift`][ast.LShift]
590 > - [`Lt`][ast.Lt]
591 > - [`LtE`][ast.LtE]
592 > - [`Match`][ast.Match]
593 > - [`MatchAs`][ast.MatchAs]
594 > - [`match_case`][ast.match_case]
595 > - [`MatchClass`][ast.MatchClass]
596 > - [`MatchMapping`][ast.MatchMapping]
597 > - [`MatchOr`][ast.MatchOr]
598 > - [`MatchSequence`][ast.MatchSequence]
599 > - [`MatchSingleton`][ast.MatchSingleton]
600 > - [`MatchStar`][ast.MatchStar]
601 > - [`MatchValue`][ast.MatchValue]
602 > - [`MatMult`][ast.MatMult]
603 > - [`Mod`][ast.Mod]
604 > - `Module`[^3]
605 > - [`Mult`][ast.Mult]
606 > - [`Name`][ast.Name]
607 > - `NameConstant`[^1]
608 > - [`NamedExpr`][ast.NamedExpr]
609 > - [`Nonlocal`][ast.Nonlocal]
610 > - [`Not`][ast.Not]
611 > - [`NotEq`][ast.NotEq]
612 > - [`NotIn`][ast.NotIn]
613 > - `Num`[^1]
614 > - [`Or`][ast.Or]
615 > - [`ParamSpec`][ast.ParamSpec]
616 > - [`Pass`][ast.Pass]
617 >
618 > </td><td>
619 >
620 > - `pattern`[^3]
621 > - [`Pow`][ast.Pow]
622 > - `Print`[^4]
623 > - [`Raise`][ast.Raise]
624 > - [`Return`][ast.Return]
625 > - [`RShift`][ast.RShift]
626 > - [`Set`][ast.Set]
627 > - [`SetComp`][ast.SetComp]
628 > - [`Slice`][ast.Slice]
629 > - [`Starred`][ast.Starred]
630 > - [`Store`][ast.Store]
631 > - `Str`[^1]
632 > - [`Sub`][ast.Sub]
633 > - [`Subscript`][ast.Subscript]
634 > - [`Try`][ast.Try]
635 > - `TryExcept`[^5]
636 > - `TryFinally`[^6]
637 > - [`Tuple`][ast.Tuple]
638 > - [`TypeAlias`][ast.TypeAlias]
639 > - [`TypeVar`][ast.TypeVar]
640 > - [`TypeVarTuple`][ast.TypeVarTuple]
641 > - [`UAdd`][ast.UAdd]
642 > - [`UnaryOp`][ast.UnaryOp]
643 > - [`USub`][ast.USub]
644 > - [`While`][ast.While]
645 > - [`With`][ast.With]
646 > - [`withitem`][ast.withitem]
647 > - [`Yield`][ast.Yield]
648 > - [`YieldFrom`][ast.YieldFrom]
649 >
650 > </td></tr></tbody></table>
651
652 [^1]: Deprecated since Python 3.8.
653 [^2]: Deprecated since Python 3.9.
654 [^3]: Not documented.
655 [^4]: `print` became a builtin (instead of a keyword) in Python 3.
656 [^5]: Now `ExceptHandler`, in the `handlers` attribute of `Try` nodes.
657 [^6]: Now a list of expressions in the `finalbody` attribute of `Try` nodes.
658
659 ## Next steps
660
661 Extensions are a powerful mechanism to customize or enhance the data loaded by Griffe. But sometimes, all you need to do to improve the data is to make Griffe happy by following a few conventions. We therefore invite you to read our recommendations on [public APIs](recommendations/public-apis.md), [Python code best practices](recommendations/python-code.md) and [docstrings](recommendations/docstrings.md).