Skip to content

Commit

Permalink
feat: Add on_package_loaded event
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Nov 10, 2023
1 parent 2bfe206 commit a5cf654
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
25 changes: 22 additions & 3 deletions docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ contains a single class, which itself contains a single method:
- then it will go back up and finish walking since there are
no more nodes to walk through


Every time the agent enters a node,
creates an object instance,
or finish handling members of an object,
Expand Down Expand Up @@ -256,13 +255,33 @@ and how to hook onto them in your extensions.

### Events and hooks

There are 3 generic **events**:
There are two kinds of events in Griffe:
**load events** and **analysis events**.
Load events are scoped to the Griffe loader.
Analysis events are scoped to the visitor and inspector agents
(triggered during static and dynamic analysis).

#### Load events

There is only one **load event**:

- [`on_package_loaded`][griffe.extensions.base.Extension.on_package_loaded]

This event is triggered when the loader has finished loading a package entirely,
i.e. when all its submodules were scanned and loaded.
This event can be hooked by extensions which require the whole package to be loaded,
to be able to navigate the object tree without raising lookup errors
or alias resolution errors.

#### Analysis events

There are 3 generic **analysis events**:

- [`on_node`][griffe.extensions.base.Extension.on_node]
- [`on_instance`][griffe.extensions.base.Extension.on_instance]
- [`on_members`][griffe.extensions.base.Extension.on_members]

There are also specific **events** for each object kind:
There are also specific **analysis events** for each object kind:

- [`on_module_node`][griffe.extensions.base.Extension.on_module_node]
- [`on_module_instance`][griffe.extensions.base.Extension.on_module_instance]
Expand Down
14 changes: 10 additions & 4 deletions src/griffe/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ def on_attribute_instance(self, *, node: ast.AST | ObjectNode, attr: Attribute)
attr: The attribute instance.
"""

def on_package_loaded(self, *, pkg: Module) -> None:
"""Run when a package has been completely loaded.
Parameters:
pkg: The package (Module) instance.
"""


ExtensionType = Union[VisitorExtension, InspectorExtension, Extension]

Expand Down Expand Up @@ -326,16 +333,15 @@ def after_inspection(self) -> list[InspectorExtension]:
"""The inspectors that run after the inspection."""
return self._inspectors[When.after_all]

def call(self, event: str, *, node: ast.AST | ObjectNode, **kwargs: Any) -> None:
def call(self, event: str, **kwargs: Any) -> None:
"""Call the extension hook for the given event.
Parameters:
event: The trigerred event.
node: The AST or Object node.
**kwargs: Additional arguments like a Griffe object.
**kwargs: Arguments passed to the hook.
"""
for extension in self._extensions:
getattr(extension, event)(node=node, **kwargs)
getattr(extension, event)(**kwargs)


builtin_extensions: set[str] = {
Expand Down

0 comments on commit a5cf654

Please sign in to comment.