nodes ¤
This module contains utilities for extracting information from nodes.
Classes:
-
ObjectKind
–Enumeration of the different runtime object kinds.
-
ObjectNode
–Helper class to represent an object tree.
Functions:
-
ast_children
–Return the children of an AST node.
-
ast_first_child
–Return the first child of this node.
-
ast_kind
–Return the kind of an AST node.
-
ast_last_child
–Return the lasts child of this node.
-
ast_next
–Return the next sibling of this node.
-
ast_next_siblings
–Return the next siblings of this node, starting from the closest.
-
ast_previous
–Return the previous sibling of this node.
-
ast_previous_siblings
–Return the previous siblings of this node, starting from the closest.
-
ast_siblings
–Return the siblings of this node.
-
get__all__
–Get the values declared in
__all__
. -
get_docstring
–Extract a docstring.
-
get_instance_names
–Extract names from an assignment node, only for instance attributes.
-
get_name
–Extract name from an assignment node.
-
get_names
–Extract names from an assignment node.
-
get_value
–Get the string representation of a node.
-
relative_to_absolute
–Convert a relative import path to an absolute one.
-
safe_get__all__
–Safely (no exception) extract values in
__all__
. -
safe_get_value
–Safely (no exception) get the string representation of a node.
ObjectKind ¤
flowchart TD
griffe.agents.nodes.ObjectKind[ObjectKind]
click griffe.agents.nodes.ObjectKind href "" "griffe.agents.nodes.ObjectKind"
Enumeration of the different runtime object kinds.
Attributes:
-
ATTRIBUTE
(str
) –Attributes.
-
BUILTIN_FUNCTION
(str
) –Built-in functions.
-
BUILTIN_METHOD
(str
) –Built-in ethods.
-
CACHED_PROPERTY
(str
) –Cached properties.
-
CLASS
(str
) –Classes.
-
CLASSMETHOD
(str
) –Class methods.
-
COROUTINE
(str
) –Coroutines
-
FUNCTION
(str
) –Functions.
-
METHOD
(str
) –Methods.
-
METHOD_DESCRIPTOR
(str
) –Method descriptors.
-
MODULE
(str
) –Modules.
-
PROPERTY
(str
) –Properties.
-
STATICMETHOD
(str
) –Static methods.
BUILTIN_FUNCTION class-attribute
instance-attribute
¤
BUILTIN_FUNCTION: str = 'builtin_function'
Built-in functions.
BUILTIN_METHOD class-attribute
instance-attribute
¤
BUILTIN_METHOD: str = 'builtin_method'
Built-in ethods.
CACHED_PROPERTY class-attribute
instance-attribute
¤
CACHED_PROPERTY: str = 'cached_property'
Cached properties.
ObjectNode ¤
ObjectNode(
obj: Any, name: str, parent: ObjectNode | None = None
)
Helper class to represent an object tree.
It's not really a tree but more a backward-linked list: each node has a reference to its parent, but not to its child (for simplicity purposes and to avoid bugs).
Each node stores an object, its name, and a reference to its parent node.
Parameters:
-
obj
(Any
) –A Python object.
-
name
(str
) –The object's name.
-
parent
(ObjectNode | None
, default:None
) –The object's parent node.
Attributes:
-
alias_target_path
(str | None
) –Alias target path of this node, if the node should be an alias.
-
children
(Sequence[ObjectNode]
) –The children of this node.
-
is_builtin_function
(bool
) –Whether this node's object is a builtin function.
-
is_builtin_method
(bool
) –Whether this node's object is a builtin method.
-
is_cached_property
(bool
) –Whether this node's object is a cached property.
-
is_class
(bool
) –Whether this node's object is a class.
-
is_classmethod
(bool
) –Whether this node's object is a classmethod.
-
is_coroutine
(bool
) –Whether this node's object is a coroutine.
-
is_function
(bool
) –Whether this node's object is a function.
-
is_method
(bool
) –Whether this node's object is a method.
-
is_method_descriptor
(bool
) –Whether this node's object is a method descriptor.
-
is_module
(bool
) –Whether this node's object is a module.
-
is_property
(bool
) –Whether this node's object is a property.
-
is_staticmethod
(bool
) –Whether this node's object is a staticmethod.
-
kind
(ObjectKind
) –The kind of this node.
-
module
(ObjectNode
) –The object's module.
-
name
(str
) –The Python object's name.
-
obj
(Any
) –The actual Python object.
-
parent
(ObjectNode | None
) –The parent node.
-
parent_is_class
(bool
) –Whether the object of this node's parent is a class.
-
path
(str
) –The object's (Python) path.
alias_target_path cached
property
¤
alias_target_path: str | None
Alias target path of this node, if the node should be an alias.
is_builtin_function cached
property
¤
is_builtin_function: bool
Whether this node's object is a builtin function.
is_builtin_method cached
property
¤
is_builtin_method: bool
Whether this node's object is a builtin method.
is_cached_property cached
property
¤
is_cached_property: bool
Whether this node's object is a cached property.
is_method_descriptor cached
property
¤
is_method_descriptor: bool
Whether this node's object is a method descriptor.
Built-in methods (e.g. those implemented in C/Rust) are often method descriptors, rather than normal methods.
is_staticmethod cached
property
¤
is_staticmethod: bool
Whether this node's object is a staticmethod.
ast_children ¤
ast_first_child ¤
Return the first child of this node.
Parameters:
-
node
(AST
) –The AST node.
Raises:
-
LastNodeError
–When the node does not have children.
Returns:
-
AST
–The child.
ast_kind ¤
ast_last_child ¤
Return the lasts child of this node.
Parameters:
-
node
(AST
) –The AST node.
Raises:
-
LastNodeError
–When the node does not have children.
Returns:
-
AST
–The child.
ast_next ¤
Return the next sibling of this node.
Parameters:
-
node
(AST
) –The AST node.
Raises:
-
LastNodeError
–When the node does not have next siblings.
Returns:
-
AST
–The sibling.
ast_next_siblings ¤
ast_previous ¤
Return the previous sibling of this node.
Parameters:
-
node
(AST
) –The AST node.
Raises:
-
LastNodeError
–When the node does not have previous siblings.
Returns:
-
AST
–The sibling.
ast_previous_siblings ¤
ast_siblings ¤
get__all__ ¤
get_docstring ¤
get_instance_names ¤
get_name ¤
get_names ¤
get_value ¤
relative_to_absolute ¤
relative_to_absolute(
node: ImportFrom, name: alias, current_module: Module
) -> str
Convert a relative import path to an absolute one.
Parameters:
-
node
(ImportFrom
) –The "from ... import ..." AST node.
-
name
(alias
) –The imported name.
-
current_module
(Module
) –The module in which the import happens.
Returns:
-
str
–The absolute import path.
safe_get__all__ ¤
safe_get__all__(
node: Assign | AnnAssign | AugAssign,
parent: Module,
log_level: LogLevel = debug,
) -> list[str | Name]