Skip to content

Commit

Permalink
doc: Enable docstring with classproperty methods
Browse files Browse the repository at this point in the history
- modify docstring of classproperty methods to show a preleading
  classproperty and add rtype if present.
- return the modified decorator class when Sphinx is running.

Bug: T380628
Change-Id: Ie44c9f89d34e7cf4aec332daf8cbed9b30ac0dc8
  • Loading branch information
xqt committed Dec 1, 2024
1 parent 5ed63fd commit 50de04a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pywikibot/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,20 @@ def bar(cls): # a class property method
"""

def __init__(self, cls_method) -> None:
"""Hold the class method."""
"""Initializer: hold the class method and documentation."""
self.method = cls_method
self.__doc__ = self.method.__doc__
self.__annotations__ = self.method.__annotations__
self.__doc__ = (':class:`classproperty<tools.classproperty>` '
f'{self.method.__doc__}')
rtype = self.__annotations__.get('return')
if rtype:
self.__doc__ += f'\n\n:rtype: {rtype}'

def __get__(self, instance, owner):
"""Get the attribute of the owner class by its method."""
if SPHINX_RUNNING:
return self

return self.method(owner)


Expand Down

0 comments on commit 50de04a

Please sign in to comment.