Skip to content

Commit 02ba876

Browse files
committed
fix: Ignore exceptions when trying to unwrap
Issue #45
1 parent 85831aa commit 02ba876

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/pytkdocs/loader.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ def __init__(self, obj: Any, name: str, parent: Optional["ObjectNode"] = None) -
4040
name: The object's name.
4141
parent: The object's parent node.
4242
"""
43-
self.obj: Any = inspect.unwrap(obj)
43+
try:
44+
obj = inspect.unwrap(obj)
45+
except Exception: # noqa: S110 (we purposely catch every possible exception)
46+
# inspect.unwrap at some point runs hasattr(obj, "__wrapped__"),
47+
# which triggers the __getattr__ method of the object, which in
48+
# turn can raise various exceptions. Probably not just __getattr__.
49+
# See https://github.com/pawamoy/pytkdocs/issues/45
50+
pass
51+
52+
self.obj: Any = obj
4453
"""The actual Python object."""
4554

4655
self.name: str = name
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class TryMe:
2+
def __getattr__(self, item):
3+
raise ValueError
4+
5+
6+
TRY_ME = TryMe()

tests/test_loader.py

+6
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,9 @@ def test_loading_module_wrapped_members():
368368
obj = loader.get_object_documentation("tests.fixtures.wrapped_objects")
369369
assert obj.functions and obj.functions[0].docstring == "My docstring."
370370
assert obj.classes and obj.classes[0].methods and obj.classes[0].methods[0].docstring == "Hello!"
371+
372+
373+
def test_unwrap_object_with_getattr_method_raising_exception():
374+
"""Try loading an object that defines a `__getattr__` method which raises an exception."""
375+
loader = Loader()
376+
loader.get_object_documentation("tests.fixtures.unwrap_getattr_raises")

0 commit comments

Comments
 (0)