Skip to content

Commit 8b029a5

Browse files
committed
Fix TypeError when cls.__module__ is None
Trying to use `pdoc -o` instead of `pdoc` in my project caused a long stacktrace with this error at the end: File "/Users/feuh/.../.venv/lib/python3.12/site-packages/pdoc/doc.py", line 640, in _var_annotations _safe_getattr(cls, "__module__", "") + "." + cls.__qualname__ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' This was caused by one class' `__module__` attribute being `None`. I could not find the reason why that happens and why it happens only when using `-o`, but this change ensures it won't happen again.
1 parent 485d9fa commit 8b029a5

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

pdoc/doc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,8 @@ def _children(doc: Namespace) -> str:
13071307
def _safe_getattr(obj, attr, default):
13081308
"""Like `getattr()`, but never raises."""
13091309
try:
1310-
return getattr(obj, attr, default)
1310+
result = getattr(obj, attr, default)
1311+
return result if result is not None else default
13111312
except Exception as e:
13121313
warnings.warn(
13131314
f"getattr({obj!r}, {attr!r}, {default!r}) raised an exception: {e!r}"

0 commit comments

Comments
 (0)