From 0b97dd866ce51f4015e325d29fa5642c3cbf808b Mon Sep 17 00:00:00 2001 From: tristanlatr Date: Tue, 2 Aug 2022 18:54:25 -0400 Subject: [PATCH 1/2] Add inheritance info to the class pages. Fix #633 --- pydoctor/model.py | 4 ++- pydoctor/templatewriter/pages/__init__.py | 44 +++++++++++++++++++++++ pydoctor/themes/base/apidocs.css | 13 +++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/pydoctor/model.py b/pydoctor/model.py index 5b4053814..92a02029b 100644 --- a/pydoctor/model.py +++ b/pydoctor/model.py @@ -585,9 +585,11 @@ def mro(self, include_external:bool=False, include_self:bool=True) -> List[Union if include_external is False: _mro = [o for o in self._mro if not isinstance(o, str)] else: - _mro = self._mro + _mro = self._mro[:] if include_self is False: _mro = _mro[1:] + + # It always returns a copy of the list return _mro @property diff --git a/pydoctor/templatewriter/pages/__init__.py b/pydoctor/templatewriter/pages/__init__.py index 828dda33b..bd784e801 100644 --- a/pydoctor/templatewriter/pages/__init__.py +++ b/pydoctor/templatewriter/pages/__init__.py @@ -382,9 +382,53 @@ def __init__(self, ): super().__init__(ob, template_lookup, docgetter) self.baselists = util.class_members(self.ob) + + def classInheritance(self) -> Optional['Flattenable']: + """ + Returns something only if the class inherits + more than the default object type and there + are more than two bases to show. + """ + def buildInheritance(ultag:Tag, bases:List['model.Class']) -> Tag: + """ + Recusrsive function to create nested lists based on the MRO. + """ + if not bases: + return ultag + last = bases.pop() + if isinstance(last, model.Documentable): + link = tags.code(epydoc2stan.taglink(last, self.page_url)) + else: + link = tags.code(self.ob.docstring_linker.link_to(last, last)) + ultag(tags.li(link)) + if bases: + new_ul = create_ul() + ultag(tags.li(buildInheritance(new_ul, bases))) + return ultag + + # fetch MRO + mro = self.ob.mro(True, False) + # Return None if the MRO is not informational + if not mro: + return None + if mro[-1] == 'object': + mro.pop(-1) + if len(mro) <= 1: + return None + + # Present inheritence in the form of a nested list, just like javadoc. + create_ul = lambda:tags.ul(class_='class-inheritance') + ul = create_ul() + buildInheritance(ul, mro) + return tags.div(tags.p("Inheritance: "), ul, class_="class-inheritance-div") def extras(self) -> List[Tag]: r: List[Tag] = [] + + # Render inheritance infos + _inheritance = self.classInheritance() + if _inheritance: + r.append(_inheritance) sourceHref = util.srclink(self.ob) source: "Flattenable" diff --git a/pydoctor/themes/base/apidocs.css b/pydoctor/themes/base/apidocs.css index 6d7a67d6a..5baa14374 100644 --- a/pydoctor/themes/base/apidocs.css +++ b/pydoctor/themes/base/apidocs.css @@ -1123,3 +1123,16 @@ pre.constant-value { padding: .5em; } .rst-deprecated > .rst-versionmodified{ color:#aa6708; } + +/* CSS for MRO */ +.class-inheritance { + margin-top:0; + border:0; + list-style-type: none; +} +.class-inheritance li{ + padding:0; +} +.class-inheritance-div{ + margin-bottom:15px; +} From 9e21b1dd95dfff3fbc31410ae35e5137a08d7cdd Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Tue, 2 Aug 2022 19:01:27 -0400 Subject: [PATCH 2/2] Update pydoctor/templatewriter/pages/__init__.py --- pydoctor/templatewriter/pages/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydoctor/templatewriter/pages/__init__.py b/pydoctor/templatewriter/pages/__init__.py index bd784e801..b24011c65 100644 --- a/pydoctor/templatewriter/pages/__init__.py +++ b/pydoctor/templatewriter/pages/__init__.py @@ -408,7 +408,7 @@ def buildInheritance(ultag:Tag, bases:List['model.Class']) -> Tag: # fetch MRO mro = self.ob.mro(True, False) - # Return None if the MRO is not informational + # Return None if the MRO is not informative if not mro: return None if mro[-1] == 'object':