diff --git a/src/griffe/agents/inspector.py b/src/griffe/agents/inspector.py index 306aca14..cbc15b11 100644 --- a/src/griffe/agents/inspector.py +++ b/src/griffe/agents/inspector.py @@ -26,6 +26,7 @@ from inspect import Signature, getdoc from inspect import signature as getsignature from pathlib import Path +from tokenize import TokenError from typing import Any from griffe.agents.base import BaseInspector @@ -219,6 +220,14 @@ def inspect_method_descriptor(self, node: ObjectNode) -> None: """ self.handle_function(node, {"method descriptor"}) + def inspect_builtin_method(self, node: ObjectNode) -> None: + """Inspect a builtin method. + + Parameters: + node: The node to inspect. + """ + self.handle_function(node, {"builtin"}) + def inspect_method(self, node: ObjectNode) -> None: """Inspect a method. @@ -235,6 +244,14 @@ def inspect_coroutine(self, node: ObjectNode) -> None: """ self.handle_function(node, {"async"}) + def inspect_builtin_function(self, node: ObjectNode) -> None: + """Inspect a builtin function. + + Parameters: + node: The node to inspect. + """ + self.handle_function(node, {"builtin"}) + def inspect_function(self, node: ObjectNode) -> None: """Inspect a function. @@ -268,7 +285,7 @@ def handle_function(self, node: ObjectNode, labels: set | None = None): # noqa: """ try: signature = getsignature(node.obj) - except ValueError: + except (ValueError, TokenError): parameters = None returns = None else: diff --git a/src/griffe/agents/nodes.py b/src/griffe/agents/nodes.py index 9fbc0d0b..8c9b708f 100644 --- a/src/griffe/agents/nodes.py +++ b/src/griffe/agents/nodes.py @@ -248,8 +248,10 @@ class ObjectKind(enum.Enum): CLASSMETHOD: str = "classmethod" METHOD_DESCRIPTOR: str = "method_descriptor" METHOD: str = "method" + BUILTIN_METHOD: str = "builtin_method" COROUTINE: str = "coroutine" FUNCTION: str = "function" + BUILTIN_FUNCTION: str = "builtin_function" CACHED_PROPERTY: str = "cached_property" PROPERTY: str = "property" ATTRIBUTE: str = "attribute" @@ -316,10 +318,14 @@ def kind(self) -> ObjectKind: return ObjectKind.METHOD_DESCRIPTOR if self.is_method: return ObjectKind.METHOD + if self.is_builtin_method: + return ObjectKind.BUILTIN_METHOD if self.is_coroutine: return ObjectKind.COROUTINE if self.is_function: return ObjectKind.FUNCTION + if self.is_builtin_function: + return ObjectKind.BUILTIN_FUNCTION if self.is_cached_property: return ObjectKind.CACHED_PROPERTY if self.is_property: @@ -369,6 +375,16 @@ def is_function(self) -> bool: """ return inspect.isfunction(self.obj) + @cached_property + def is_builtin_function(self) -> bool: + """ + Tell if this node's object is a builtin function. + + Returns: + If this node's object is a builtin function. + """ + return inspect.isbuiltin(self.obj) + @cached_property def is_coroutine(self) -> bool: """ @@ -433,6 +449,16 @@ def is_method_descriptor(self) -> bool: """ return inspect.ismethoddescriptor(self.obj) + @cached_property + def is_builtin_method(self) -> bool: + """ + Tell if this node's object is a builtin method. + + Returns: + If this node's object is a builtin method. + """ + return self.is_builtin_function and self.parent_is_class + @cached_property def is_staticmethod(self) -> bool: """