Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support sub-extensions with custom hooks #315

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/_griffe/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from importlib.util import module_from_spec, spec_from_file_location
from inspect import isclass
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Type, Union
from typing import TYPE_CHECKING, Any, Dict, Self, Type, Union

from _griffe.agents.nodes.ast import ast_children, ast_kind
from _griffe.exceptions import ExtensionNotLoadedError
Expand All @@ -28,6 +28,8 @@
class Extension:
"""Base class for Griffe extensions."""

sub_extensions: tuple[SubExtension, ...] = ()

def visit(self, node: ast.AST) -> None:
"""Visit a node.

Expand Down Expand Up @@ -277,6 +279,17 @@ def on_wildcard_expansion(
"""


class SubExtension:
"""Base class for Griffe sub-extensions."""

namespace: str

def __init_subclass__(cls) -> None:
if not hasattr(cls, "namespace") or not cls.namespace:
cls.namespace = cls.__name__



LoadableExtensionType = Union[str, Dict[str, Any], Extension, Type[Extension]]
"""All the types that can be passed to `load_extensions`."""

Expand Down Expand Up @@ -312,6 +325,17 @@ def call(self, event: str, **kwargs: Any) -> None:
for extension in self._extensions:
getattr(extension, event)(**kwargs)

def subcall(self, namespace: str, event: str, **kwargs: Any) -> None:
"""Call the sub-extension hook for the given event.

Parameters:
event: The triggered event.
**kwargs: Arguments passed to the hook.
"""
for extension in self._extensions:
for sub_extension in extension.sub_extensions:
if sub_extension.namespace == namespace:
getattr(sub_extension, event)(**kwargs)

builtin_extensions: set[str] = {
"dataclasses",
Expand Down
Loading