sphinx autodoc扩展的类型提示(pep 484)支持
sphinx-autodoc-napoleon-typehints的Python项目详细描述
Sphinx Autodoc Napoleon类型提示
此扩展允许您使用Python3注释来记录可接受的参数类型 函数的返回值类型。这允许您以非常自然的方式使用类型提示, 允许您从此处迁移:
defformat_unit(value,unit):""" Formats the given value as a human readable string using the given units. :param float|int value: a numeric value :param str unit: the unit for the value (kg, m, etc.) :rtype: str """return'{} {}'.format(value,unit)
对此:
fromtypingimportUniondefformat_unit(value:Union[float,int],unit:str)->str:""" Formats the given value as a human readable string using the given units. :param value: a numeric value :param unit: the unit for the value (kg, m, etc.) """return'{} {}'.format(value,unit)
在拿破仑的帮助下,也支持google docstrings或numpydocstrings。 napoleon sphinx extention。 这意味着即使是这样的docstring:
defformat_unit_google(self,value:Union[float,int],unit:str,test:Optional[Union[Iterable,str]])->str:""" Formats the given value as a human readable string using the given units. Args: value: a numeric value unit: the unit for the value (kg, m, etc.) test: bla bla blathe unit for the value (kg, m, etc.) Returns: This function returns something of value: and does not overwrite this part. """return'{} {}'.format(value,unit)defformat_unit_numpy(self,value:Union[float,int],unit:str,test:Optional[Union[Iterable,str]])->str:""" Formats the given value as a human readable string using the given units. Parameters ---------- value: a numeric value unit: the unit for the value (kg, m, etc.) test: bla bla blathe unit for the value (kg, m, etc.) Returns ------- This function returns something of value: and does not overwrite this part. """return'{} {}'.format(value,unit)
其结果与以上相同
安装和设置
首先,使用pip下载并安装扩展:
$ pip install sphinx-autodoc-napoleon-typehints
然后,将扩展名添加到conf.py:
extensions=['sphinx.ext.autodoc','sphinx_autodoc_napoleon_typehints']
工作原理
扩展监听autodoc-process-signature和autodoc-process-docstring 斯芬克斯事件。在前者中,它从函数签名中剥离注释。在后者中, 它将适当的:type argname:和:rtype:指令注入docstring。
只有DOCSTATE中存在一个现有的{TT6} $指令的参数得到它们各自的 :type:指令已添加。当且仅当不存在时,添加{TT5} $指令 ^找到{tt5}$。
此扩展当前没有任何配置选项。