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 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ htmlcov
.*_cache/*

html/*
/.mypy_cache/
frank101010 marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 40 additions & 4 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,19 @@ def classes(self, sort=True) -> List['Class']:
"""
return self._filter_doc_objs(Class, sort)

def all_classes(self, sort=True) -> List['Class']:
frank101010 marked this conversation as resolved.
Show resolved Hide resolved
"""
Returns all documented classes in the module even the ones defined
in other 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 functions(self, sort=True) -> List['Function']:
"""
Returns all documented module-level functions in the module,
Expand Down Expand Up @@ -1005,9 +1018,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 +1032,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 +1083,14 @@ 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,
docstring=inspect.getdoc(obj)
frank101010 marked this conversation as resolved.
Show resolved Hide resolved
)
else:
self.doc[name] = Variable(
name, self.module,
Expand Down Expand Up @@ -1158,6 +1186,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 @@ -1204,7 +1240,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
4 changes: 2 additions & 2 deletions pdoc/templates/html.mako
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<%def name="show_module(module)">
<%
variables = module.variables(sort=sort_identifiers)
classes = module.classes(sort=sort_identifiers)
classes = module.all_classes(sort=sort_identifiers)
functions = module.functions(sort=sort_identifiers)
submodules = module.submodules()
%>
Expand Down Expand Up @@ -279,7 +279,7 @@
<%def name="module_index(module)">
<%
variables = module.variables(sort=sort_identifiers)
classes = module.classes(sort=sort_identifiers)
classes = module.all_classes(sort=sort_identifiers)
functions = module.functions(sort=sort_identifiers)
submodules = module.submodules()
supermodule = module.supermodule
Expand Down