Skip to content

Commit

Permalink
feat: Add on_wildcard_expansion event
Browse files Browse the repository at this point in the history
Issue-282: #282
  • Loading branch information
pawamoy committed Aug 17, 2024
1 parent a760a8c commit c6bc6fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 3 additions & 4 deletions docs/guide/users/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,10 @@ There are two kinds of events in Griffe: **load events** and **analysis events**

#### Load events

There is only one **load event**:
There are two **load events**:

- [`on_package_loaded`][griffe.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.
- [`on_package_loaded`][griffe.Extension.on_package_loaded]: The "on package loaded" 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.
- [`on_wildcard_expansion`][griffe.Extension.on_wildcard_expansion]: The "on wildcard expansion" event is triggered for each alias that is created by expanding wildcard imports (`from ... import *`).

#### Analysis events

Expand Down
15 changes: 15 additions & 0 deletions src/_griffe/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ def on_package_loaded(self, *, pkg: Module, loader: GriffeLoader, **kwargs: Any)
**kwargs: For forward-compatibility.
"""

def on_wildcard_expansion(
self,
*,
alias: Alias,
loader: GriffeLoader,
**kwargs: Any,
) -> None:
"""Run when wildcard imports are expanded into aliases.
Parameters:
alias: The alias instance.
loader: The loader currently in use.
**kwargs: For forward-compatibility.
"""


LoadableExtensionType = Union[str, Dict[str, Any], Extension, Type[Extension]]
"""All the types that can be passed to `load_extensions`."""
Expand Down
1 change: 1 addition & 0 deletions src/_griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ def expand_wildcards(

# Everything went right (supposedly), we add the alias as a member of the current object.
obj.set_member(new_member.name, alias)
self.extensions.call("on_wildcard_expansion", alias=alias, loader=self)

def resolve_module_aliases(
self,
Expand Down

0 comments on commit c6bc6fa

Please sign in to comment.