You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/users/extending.md
+43-73Lines changed: 43 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,7 +188,9 @@ Hopefully this flowchart gives you a pretty good idea of what happens when Griff
188
188
189
189
### Events and hooks
190
190
191
-
There are two kinds of events in Griffe: **load events** and **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).
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).
192
194
193
195
#### Load events
194
196
@@ -234,26 +236,23 @@ There are also specific **analysis events** for each object kind:
**Hooks** are methods that are called when a particular event is triggered. To target a specific event, the hook must be named after it.
239
+
#### Extensions and hooks
240
240
241
241
**Extensions** are classes that inherit from [Griffe's Extension base class][griffe.Extension] and define some hooks as methods:
242
242
243
243
```python
244
-
import ast
245
244
import griffe
246
245
247
246
248
247
class MyExtension(griffe.Extension):
249
-
def on_instance(
248
+
def on_object(
250
249
self,
251
-
node: ast.AST | griffe.ObjectNode,
250
+
*,
252
251
obj: griffe.Object,
253
-
agent: griffe.Visitor | griffe.Inspector,
252
+
loader: griffe.GriffeLoader,
254
253
**kwargs,
255
254
) -> None:
256
-
"""Do something with `node` and/or `obj`."""
255
+
"""Do something with `obj`."""
257
256
```
258
257
259
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.
@@ -271,79 +270,42 @@ class MyExtension(Extension):
271
270
self.state_thingy ="initial stuff"
272
271
self.list_of_things = []
273
272
274
-
defon_instance(
273
+
defon_object(
275
274
self,
276
-
node: ast.AST| griffe.ObjectNode,
275
+
*,
277
276
obj: griffe.Object,
278
-
agent: griffe.Visitor | griffe.Inspector,
277
+
loader: griffe.GriffeLoader,
279
278
**kwargs,
280
279
) -> None:
281
-
"""Do something with `node` and/or `obj`."""
280
+
"""Do something with `obj`."""
282
281
```
283
282
284
283
### Static/dynamic support
285
284
286
-
Extensions can support both static and dynamic analysis of modules. If a module is scanned statically, your extension hooks will receive AST nodes (from the [ast][] module of the standard library). If the module is scanned dynamically, your extension hooks will receive [object nodes][griffe.ObjectNode]. Similarly, your hooks will receive a reference to the analysis agent that calls them, either a [Visitor][griffe.Visitor] or an [Inspector][griffe.Inspector].
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).
287
288
288
-
To support static analysis, dynamic analysis, or both, you can therefore check the type of the received node or agent:
289
+
To support static analysis, dynamic analysis, or both in your load events, you can therefore check the value of the `analysis` attribute:
The preferred method is to check the type of the received node rather than the agent.
331
-
332
-
Since hooks also receive instantiated modules, classes, functions, attributes and type aliases, most of the time you will not need to use the `node` argument other than for checking its type and deciding what to do based on the result. And since we always add `**kwargs` to the hooks' signatures, you can drop any parameter you don't use from the signature:
0 commit comments