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

feature: Allow extensions to access the current agent (visitor/inspector) #312

Closed
pawamoy opened this issue Aug 10, 2024 · 1 comment
Closed
Assignees
Labels
feature New feature or request

Comments

@pawamoy
Copy link
Member

pawamoy commented Aug 10, 2024

Is your feature request related to a problem? Please describe.

When working with docstrings, we often need to pass down the docstring parser options selected by the user. The only way to access those is through the agent, but we don't have any reference to the agent from extension hooks.

Describe the solution you'd like

Pass the agent reference to extension hooks. This will require inspecting the signature of hooks first (in v0). For forward-compatibility, we could add **kwargs to all hooks, so that users remember to add it to their own hooks (IDEs will help by copying the signature).

def on_attribute_instance(self, *, node: AST | griffe.ObjectNode, attr: griffe.Attribute, agent: griffe.Visitor | griffe.Inspector, **kwargs: Any) -> None:
    ...

Accepting **kwargs means users could also avoid listing node: ast.AST | griffe.ObjectNode in their hook parameters (not often used), or any other parameter, simplifying signatures and imports.

def on_attribute_instance(self, *, attr: griffe.Attribute, **kwargs: Any) -> None:
    ...

Describe alternatives you've considered

Attaching the agent reference to the extension itself, in an attribute. But that's ugly and error prone (state to manage).
Or always inspecting the hook signature to know which arguments to pass, to avoid type errors (even in v1).

Additional context

/

@pawamoy pawamoy added the feature New feature or request label Aug 10, 2024
@pawamoy pawamoy self-assigned this Aug 10, 2024
@pawamoy
Copy link
Member Author

pawamoy commented Aug 10, 2024

By the way, this could allow extensions to trigger other extensions when they create objects. For example:

class MyExtension(griffe.Extension):
    def on_attribute_node(self, *, node, agent, **kwargs):
        ...
        attr = Attribute(...)
        agent.extensions.call("on_attribute_instance", node=node, attr=attr, agent=agent, **kwargs)

pawamoy added a commit that referenced this issue Aug 11, 2024
…patibility

Emit deprecation warnings when hooks in subclasses don't accept `**kwargs`.

Issue-312: #312
pawamoy added a commit that referenced this issue Aug 11, 2024
Some extensions need to know the user-selected docstring parser and options. The docstring parser and options can only be accessed through the agent.

Other extensions who create objects might want to trigger the rest of the extensions, which can again only be done if we have a reference to the agent.

Issue-312: #312
@pawamoy pawamoy closed this as completed Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant