Skip to content

Commit

Permalink
feat: Support inspecting builtin functions/methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Dec 31, 2021
1 parent 2998195 commit aa1fce3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/griffe/agents/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions src/griffe/agents/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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:
"""
Expand Down

0 comments on commit aa1fce3

Please sign in to comment.