@@ -225,8 +225,12 @@ def _pep224_docstrings(doc_obj: Union['Module', 'Class'], *,
225225 if _init_tree :
226226 tree = _init_tree
227227 else :
228+ source = doc_obj .source
229+ if source == "" :
230+ warn ("Couldn't read PEP-224 variable docstrings from {!r}" .format (doc_obj ))
231+ return {}, {}
228232 try :
229- tree = ast .parse (inspect . getsource ( doc_obj . obj ) )
233+ tree = ast .parse (source )
230234 except (OSError , TypeError , SyntaxError ) as exc :
231235 warn ("Couldn't read PEP-224 variable docstrings from {!r}: {}" .format (doc_obj , exc ))
232236 return {}, {}
@@ -786,6 +790,17 @@ def classes(self, sort=True):
786790 """
787791 return self ._filter_doc_objs (Class , sort )
788792
793+ def all_classes (self , sort = True ):
794+ """Returns all documented classes in the module even the ones defined
795+ in other classes."""
796+ stack = self .classes (sort )[::- 1 ]
797+ results = []
798+ while stack :
799+ c = stack .pop ()
800+ results .append (c )
801+ stack .extend (c .classes (sort = sort )[::- 1 ])
802+ return results
803+
789804 def functions (self , sort = True ):
790805 """
791806 Returns all documented module-level functions in the module,
@@ -813,9 +828,9 @@ class Class(Doc):
813828 """
814829 Representation of a class' documentation.
815830 """
816- __slots__ = ('doc' , '_super_members' )
831+ __slots__ = ('doc' , 'cls' , ' _super_members' )
817832
818- def __init__ (self , name , module , obj , * , docstring = None ):
833+ def __init__ (self , name , module , obj , * , docstring = None , cls = None ):
819834 assert inspect .isclass (obj )
820835
821836 if docstring is None :
@@ -826,6 +841,12 @@ def __init__(self, name, module, obj, *, docstring=None):
826841
827842 super ().__init__ (name , module , obj , docstring = docstring )
828843
844+ self .cls = cls
845+ """
846+ The `pdoc.Class` object if this class is defined in a class. If not,
847+ this is None.
848+ """
849+
829850 self .doc = {}
830851 """A mapping from identifier name to a `pdoc.Doc` objects."""
831852
@@ -845,6 +866,14 @@ def __init__(self, name, module, obj, *, docstring=None):
845866 self .doc [name ] = Function (
846867 name , self .module , obj , cls = self ,
847868 method = not self ._method_type (self .obj , name ))
869+ elif inspect .isclass (obj ):
870+ self .doc [name ] = Class (
871+ self .name + "." + name ,
872+ self .module ,
873+ obj ,
874+ cls = self ,
875+ docstring = inspect .getdoc (obj )
876+ )
848877 else :
849878 self .doc [name ] = Variable (
850879 name , self .module ,
@@ -940,6 +969,14 @@ def _filter_doc_objs(self, type: Type[T], include_inherited=True,
940969 if (include_inherited or not obj .inherits ) and filter_func (obj )]
941970 return sorted (result ) if sort else result
942971
972+ def classes (self , include_inherited = False , sort = False ):
973+ """Returns the classes defined in this class."""
974+ return self ._filter_doc_objs (
975+ Class ,
976+ include_inherited = include_inherited ,
977+ sort = sort
978+ )
979+
943980 def class_variables (self , include_inherited = True , sort = True ):
944981 """
945982 Returns an optionally-sorted list of `pdoc.Variable` objects that
0 commit comments