Skip to content

Commit

Permalink
refactor: Avoid side-effect in inspector by checking early if an obje…
Browse files Browse the repository at this point in the history
…ct is a cached property
  • Loading branch information
pawamoy committed May 12, 2024
1 parent 3f74f67 commit a6bfcfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/griffe/agents/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ def inspect_cached_property(self, node: ObjectNode) -> None:
Parameters:
node: The node to inspect.
"""
node.obj = node.obj.func
self.handle_function(node, {"cached", "property"})

def inspect_property(self, node: ObjectNode) -> None:
Expand Down
19 changes: 12 additions & 7 deletions src/griffe/agents/nodes/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, obj: Any, name: str, parent: ObjectNode | None = None) -> Non
name: The object's name.
parent: The object's parent node.
"""
# Unwrap object.
try:
obj = inspect.unwrap(obj)
except Exception as error: # noqa: BLE001
Expand All @@ -49,12 +50,21 @@ def __init__(self, obj: Any, name: str, parent: ObjectNode | None = None) -> Non
# See https://github.com/pawamoy/pytkdocs/issues/45
logger.debug(f"Could not unwrap {name}: {error!r}")

# Unwrap cached properties (`inpsect.unwrap` doesn't do that).
if isinstance(obj, cached_property):
is_cached_property = True
obj = obj.func
else:
is_cached_property = False

self.obj: Any = obj
"""The actual Python object."""
self.name: str = name
"""The Python object's name."""
self.parent: ObjectNode | None = parent
"""The parent node."""
self.is_cached_property: bool = is_cached_property
"""Whether this node's object is a cached property."""

def __repr__(self) -> str:
return f"ObjectNode(name={self.name!r})"
Expand Down Expand Up @@ -86,6 +96,8 @@ def kind(self) -> ObjectKind:
return ObjectKind.STATICMETHOD
if self.is_classmethod:
return ObjectKind.CLASSMETHOD
if self.is_cached_property:
return ObjectKind.CACHED_PROPERTY
if self.is_method:
return ObjectKind.METHOD
if self.is_builtin_method:
Expand All @@ -96,8 +108,6 @@ def kind(self) -> ObjectKind:
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:
return ObjectKind.PROPERTY
if self.is_method_descriptor:
Expand Down Expand Up @@ -143,11 +153,6 @@ def is_property(self) -> bool:
"""Whether this node's object is a property."""
return isinstance(self.obj, property) or self.is_cached_property

@cached_property
def is_cached_property(self) -> bool:
"""Whether this node's object is a cached property."""
return isinstance(self.obj, cached_property)

@cached_property
def parent_is_class(self) -> bool:
"""Whether the object of this node's parent is a class."""
Expand Down

0 comments on commit a6bfcfd

Please sign in to comment.