How to express multiple types for a single parameter or a return value in docstrings that are processed by Sphinx?

喜欢而已 提交于 2019-11-30 11:00:09

Python 3.5 Union type hints

https://docs.python.org/3/library/typing.html#typing.Union

For now, I recommend using the exact same syntax as that module, which will:

  • make porting easier, and possibly automatable, later on
  • specifies a unique well defined canonical way to do things

Example:

def f(int_or_float):
    """
    :type int_or_float: Union[int, float]
    :rtype: float
    """
    return int_or_float + 1.0

Then when you have 3.5, you will write just:

def f(list_of_int : Union[int, float]) -> float:
    return int_or_float + 1.0

I think it already has documentation generation support, but I haven't tested it yet: https://github.com/sphinx-doc/sphinx/issues/1968

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