Skip to content

Commit f3453db

Browse files
committed
BUG: Fix crash when some type.__module__ is None
Closes #172
1 parent fa96fb1 commit f3453db

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pdoc/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
_URL_INDEX_MODULE_SUFFIX = '.m.html' # For modules named literal 'index'
4040
_URL_PACKAGE_SUFFIX = '/index.html'
4141

42+
# type.__module__ can be None by the Python spec. In those cases, use this value
43+
_UNKNOWN_MODULE = '?'
44+
4245
T = TypeVar('T', bound='Doc')
4346

4447
__pdoc__ = {} # type: Dict[str, Union[bool, str]]
@@ -726,7 +729,7 @@ def find_class(self, cls: type):
726729
# XXX: Is this corrent? Does it always match
727730
# `Class.module.name + Class.qualname`?. Especially now?
728731
# If not, see what was here before.
729-
return self.find_ident(cls.__module__ + '.' + cls.__qualname__)
732+
return self.find_ident((cls.__module__ or _UNKNOWN_MODULE) + '.' + cls.__qualname__)
730733

731734
def find_ident(self, name: str) -> Doc:
732735
"""
@@ -1131,7 +1134,7 @@ def safe_default_value(p: inspect.Parameter):
11311134
if isinstance(value, enum.Enum):
11321135
replacement = str(value)
11331136
elif inspect.isclass(value):
1134-
replacement = value.__module__ + '.' + value.__qualname__
1137+
replacement = (value.__module__ or _UNKNOWN_MODULE) + '.' + value.__qualname__
11351138
elif ' at 0x' in repr(value):
11361139
replacement = re.sub(r' at 0x\w+', '', repr(value))
11371140

pdoc/test/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ def test_module(self):
402402
self.assertEqual(sorted(m.name for m in m.submodules()),
403403
[EXAMPLE_MODULE + '.' + m for m in submodules])
404404

405+
def test_Module_find_class(self):
406+
class A:
407+
__module__ = None
408+
409+
assert A.__module__ is None
410+
mod = pdoc.Module(pdoc)
411+
self.assertIsInstance(mod.find_class(pdoc.Doc), pdoc.Class)
412+
self.assertIsInstance(mod.find_class(A), pdoc.External)
413+
405414
def test_import_filename(self):
406415
with patch.object(sys, 'path', ['']), \
407416
chdir(os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE)):

0 commit comments

Comments
 (0)