Replies: 2 comments 1 reply
-
Hi @MisLink, I've moved this to a discussion 🙂 I see that you tried to use the hybrid extension, nice work! I will push docs on how to use it soon, but you managed to get quite close. You could indeed use an inspector extension, but in fact it's just as easy and much more performant to use a visitor extension. Let me show you with an example similar to the one found in this comment: # changed_return_extension.py
from __future__ import annotations
import ast
import re
from griffe.extensions import VisitorExtension
from griffe.agents.nodes import safe_get_annotation
class ChangedReturnTypeExtension(VisitorExtension):
def visit_functiondef(self, node: ast.FunctionDef) -> None:
current = self.visitor.current.members[node.name] # type: ignore[assignment]
for decorator_node in node.decorator_list:
if isinstance(decorator_node, ast.Call) and decorator_node.func.id == "change_return_annotation":
returns = safe_get_annotation(decorator_node.args[0], parent=current)
if current.is_function: # function/method
current.returns = returns
elif current.is_attribute: # property
current.annotation = returns
Extension = ChangedReturnTypeExtension |
Beta Was this translation helpful? Give feedback.
-
You can also get inspiration from this full example using the new extension system: https://mkdocstrings.github.io/griffe/extensions/#full-example. Or read through the page to learn how this system works and how to do that statically instead of importing the object! Let me know if anything's unclear 🙂 |
Beta Was this translation helpful? Give feedback.
-
I have a function like this:
The decorator change the return annotation:
I'd like to show the changed return type in mkdocs. It seems that I need to use
InspectorExtension
to let griffe import the code. Then I try to create one:Here I'm stuck. How to get an AST node for
ret
, or do I totally do the wrong thing?Beta Was this translation helpful? Give feedback.
All reactions