Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for nested classes; this is basically a newer version o… #390

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,15 @@ def is_from_this_module(obj):
if _is_function(obj):
self.doc[name] = Function(name, self, obj)
elif inspect.isclass(obj):
self.doc[name] = Class(name, self, obj)
cl = Class(name, self, obj)
self.doc[name] = cl
# Also add all nested classes of the class just found to the
# module context, otherwise some classes will be recognized
# as "External" even though they were correctly recognized
# as "Class" during an earlier scanning process
# (=> Module.find_ident()).
for ncl in cl._nested_classes():
self.doc[ncl.name] = ncl
elif name in var_docstrings:
self.doc[name] = Variable(name, self, var_docstrings[name], obj=obj)

Expand Down Expand Up @@ -945,7 +953,7 @@ def variables(self, sort=True) -> List['Variable']:

def classes(self, sort=True) -> List['Class']:
"""
Returns all documented module-level classes in the module,
Returns all documented classes in the module,
optionally sorted alphabetically, as a list of `pdoc.Class`.
"""
return self._filter_doc_objs(Class, sort)
Expand Down Expand Up @@ -1005,9 +1013,10 @@ class Class(Doc):
"""
Representation of a class' documentation.
"""
__slots__ = ('doc', '_super_members')
__slots__ = ('doc', 'cls', '_super_members')

def __init__(self, name: str, module: Module, obj, *, docstring: str = None):
def __init__(self, name: str, module: Module, obj, *, docstring: str = None,
cls: 'Class' = None):
assert inspect.isclass(obj)

if docstring is None:
Expand All @@ -1018,7 +1027,13 @@ def __init__(self, name: str, module: Module, obj, *, docstring: str = None):

super().__init__(name, module, obj, docstring=docstring)

self.doc: Dict[str, Union[Function, Variable]] = {}
self.cls = cls
frank101010 marked this conversation as resolved.
Show resolved Hide resolved
"""
The `pdoc.Class` object if this class is defined in a class. If not,
this is None.
"""

self.doc: Dict[str, Union[Function, Variable, Class]] = {}
"""A mapping from identifier name to a `pdoc.Doc` objects."""

# Annotations for filtering.
Expand Down Expand Up @@ -1063,6 +1078,13 @@ def definition_order_index(
if _is_function(obj):
self.doc[name] = Function(
name, self.module, obj, cls=self)
elif inspect.isclass(obj):
self.doc[name] = Class(
self.name + "." + name,
self.module,
obj,
cls=self
)
else:
self.doc[name] = Variable(
name, self.module,
Expand Down Expand Up @@ -1158,6 +1180,14 @@ def _filter_doc_objs(self, type: Type[T], include_inherited=True,
if (include_inherited or not obj.inherits) and filter_func(obj)]
return sorted(result) if sort else result

def classes(self, include_inherited=False, sort=False):
frank101010 marked this conversation as resolved.
Show resolved Hide resolved
"""Returns the classes nested in this class."""
return self._filter_doc_objs(
Class,
include_inherited=include_inherited,
sort=sort
)

def class_variables(self, include_inherited=True, sort=True) -> List['Variable']:
"""
Returns an optionally-sorted list of `pdoc.Variable` objects that
Expand Down Expand Up @@ -1195,6 +1225,19 @@ def functions(self, include_inherited=True, sort=True) -> List['Function']:
Function, include_inherited, lambda dobj: not dobj.is_method,
sort)

def _nested_classes(self, include_inherited=True, sort=True) -> List['Class']:
"""
Returns an optionally-sorted list of `pdoc.Class` objects that
represent this class' nested classes.
"""
stack = self.classes(sort)[::-1]
results = []
while stack:
c = stack.pop()
results.append(c)
stack.extend(c.classes(sort=sort)[::-1])
return results

def inherited_members(self) -> List[Tuple['Class', List[Doc]]]:
"""
Returns all inherited members as a list of tuples
Expand All @@ -1204,7 +1247,7 @@ def inherited_members(self) -> List[Tuple['Class', List[Doc]]]:
return sorted(((cast(Class, k), sorted(g))
for k, g in groupby((i.inherits
for i in self.doc.values() if i.inherits),
key=lambda i: i.cls)), # type: ignore
key=lambda i: i.cls)),
key=lambda x, _mro_index=self.mro().index: _mro_index(x[0])) # type: ignore

def _fill_inheritance(self):
Expand Down
54 changes: 53 additions & 1 deletion pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import doctest
import enum
import importlib
import inspect
import os
import shutil
Expand Down Expand Up @@ -721,7 +722,7 @@ def test__all__(self):
mod = pdoc.Module(module)
with self.assertWarns(UserWarning): # Only B is used but __pdoc__ contains others
pdoc.link_inheritance()
self.assertEqual(list(mod.doc.keys()), ['B'])
self.assertEqual(list(mod.doc.keys()), ['B', 'B.C'])

def test_find_ident(self):
mod = pdoc.Module(EXAMPLE_MODULE)
Expand Down Expand Up @@ -1026,6 +1027,57 @@ class G(F[int]):
self.assertEqual(pdoc.Class('F', mod, F).docstring, """baz\n\nbar""")
self.assertEqual(pdoc.Class('G', mod, G).docstring, """foo\n\nbar""")

def test_nested_classes(self):
m_name = 'M'
m_spec = importlib.util.spec_from_loader(m_name, loader=None)
m_module = importlib.util.module_from_spec(m_spec)
code = '''
class A:
"""Class A documentation"""
x: str = ''
class B:
""" Class A.B documentation"""
class C:
""" Class A.B.C documentation"""
pass

class D(A):
pass

class E(A.B.C):
pass
'''
exec(code, m_module.__dict__)

A = m_module.A
B = A.B
C = B.C
D = m_module.D
E = m_module.E

sys.modules[m_name] = m_module

try:
mod = pdoc.Module('M', context=pdoc.Context())
self.assertEqual([c.qualname for c in mod.classes()], ['A', 'A.B', 'A.B.C', 'D', 'E'])
self.assertEqual([c.qualname for c in pdoc.Class('A', mod, A).classes()], ['A.B'])
self.assertEqual([c.qualname for c in pdoc.Class('B', mod, B).classes()], ['A.B.C'])
self.assertEqual([c.qualname for c in pdoc.Class('C', mod, C).classes()], [])

self.assertEqual(pdoc.Class('A', mod, A).docstring, "Class A documentation")
self.assertEqual(pdoc.Class('B', mod, B).docstring, "Class A.B documentation")
self.assertEqual(pdoc.Class('C', mod, C).docstring, "Class A.B.C documentation")

# D inherits doc from A
self.assertEqual([c.qualname for c in pdoc.Class('D', mod, D).mro()], ['A'])
self.assertEqual(pdoc.Class('D', mod, D).docstring, "Class A documentation")

# E inherits doc from A.B.C
self.assertEqual([c.qualname for c in pdoc.Class('E', mod, E).mro()], ['A.B.C'])
self.assertEqual(pdoc.Class('E', mod, E).docstring, "Class A.B.C documentation")
finally:
del sys.modules[m_name]

@ignore_warnings
def test_Class_params(self):
class C:
Expand Down