Search code examples
pythondecoratordoxygenpython-decorators

Python: How can I add a doxygen comment to a function with a decorator?


Where do I have to insert the doxygen comment?:

 ##
 # @brief Text
 @classmethod
 def foo(self):
     pass

or like this:

 @classmethod
 ##
 # @brief Text
 def foo(self):
     pass

Solution

  • Docstrings are preferred when documenting functions and classes in Python. Fortunately, Doxygen supports this way of documenting as well. To do so, simply use an exclamation mark ! at the start of your docstring.

    from dataclasses import dataclass
    
    
    @dataclass(init=False)
    class SomeClass:
        """!
        @brief ...
        @details ...
        """
        _name: str   #<! ...
        _amount: int #<! ...
    
        def __init__(self, name: str, amount: int) -> None:
            """!
            @brief ...
            @details ...
            @param[in] name ...
            @param[in] amount ...
            """
            self._name = name
            self._amount = amount
    
        @property
        def name(self) -> str:
            """!
            @brief ...
            @return ...
            """
            return self._name
    
        @property
        def amount(self) -> int:
            """!
            @brief ...
            @return ...
            """
            return self._amount
    
        @amount.setter
        def amount(self, value: int) -> None:
            """!
            @brief ...
            @param[in] value ...
            """
            self._amount = value
    
        @staticmethod
        def addone(arg: int) -> int:
            """!
            @brief ...
            @details ...
            @param[in] arg ...
            @return ...
            """
            return arg + 1