How to document callable class with Sphinx?

房东的猫 提交于 2020-01-16 09:39:29

问题


I have a callable class with @dataclass annotation.

I want to document it the way, so in the documentation, people can distinguish difference between __call__ and __init__ methods. Since I am using @dataclass __init__ is automatically generated.

Example of the code in my case:

@final
@dataclass(frozen=True, slots=True)
class Casting(object):
  """Cast one type of code to another.

  Constructor arguments:

      :param int_converter_function: function to convert to int

      :param float_converter_function: function to convert to float

  Callable arguments:

  :param casting: :term:`casting` object
  :type casting: dict

  :param value_to_cast: input value

  :return: Casted value

  Example
      >>> cast = Casting(int)
      >>> cast({'type': 'integer'}, '123')
      123
      >>> cast({'type': 'decimal'}, '123.12')
      Decimal('123.12')

  """    

  _int_converter_function = int
  _float_converter_function = float

  def __call__(self, casting, value_to_cast):
    if casting['type'] == 'integer':
        return self._int_converter_function(value_to_cast)
    return self._float_converter_function(value_to_cast)

I searched for the possible examples but couldn't find anything similar to the above case. The documentation above is hard to read and meshed into a single batch. How can it be done differently so the reader can distinguish the difference between __init__ and __call__ methods from docs?

来源:https://stackoverflow.com/questions/59752572/how-to-document-callable-class-with-sphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!