Python 3: Sphinx doesn't show type hints correctly

为君一笑 提交于 2019-12-23 08:39:40

问题


I am generating a HTML documentation of a Python 3 module automatically from its reStructuredText docstrings of its functions by using Sphinx (make HTML). The generated HTML documentation looks fine so far, but the parameter types of the function signatures, which are given in the source code as PEP484 type hints aren't shown correctly.

E.g. this is some example output from the Sphinx-generated HTML doc of one of my functions:

static parse_from_file(filename: str) → list
    Parses stuff from a text file.

    Parameters:  filename – the filepath of a textfile to be parsed
    Returns:     list of parsed elements

This is what I would expect it to look like:

static parse_from_file(filename)
    Parses stuff from a text file.

    Parameters:  filename (str) – the filepath of a textfile to be parsed
    Returns:     list of parsed elements
    Return type: list

This is how the Python code actually looks like:

def parse_from_file(filename: str) -> list:
    """Parses stuff from a text file.

    :param filename: the filepath of a textfile to be parsed
    :returns: list of parsed elements
    """
    return []

How can I make Sphinx show the Python 3 type hints correctly?


回答1:


I tackled it on my own by using the sphinx-autodoc-typehints extension.



来源:https://stackoverflow.com/questions/40071573/python-3-sphinx-doesnt-show-type-hints-correctly

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