我正在使用multipledispatch包来调度一些功能。我为每个函数创建了有意义的文档字符串。代码运行良好。
# md.py
from multipledispatch import dispatch
@dispatch(int, int)
def calc(a: int, b: int) -> int:
"""Adds two integers
Parameters
----------
a, b : int
Integer to be added
Returns
-------
int
Sum of `a` and `b`.
"""
return a + b
@dispatch(str, str)
def calc(a: str, b: str) -> str:
"""Adds two strings
Parameters
----------
a, b : str
Strings to be concatenated
Returns
-------
str
Concatenated string.
"""
return f"{a} {b}"
@dispatch(float, str)
def calc(a: float, b: str) -> float:
"""Adds two floats.
The second float wil be transformed from a string.
Parameters
----------
a : float
First summand.
b : str
Second summand, will be transformed into flaot.
Returns
-------
float
Sum of `a` and `b`
"""
return a + float(b)
def add(a: int, b: int) -> int:
"""Adds two integers
Parameters
----------
a, b : int
Integer to be added
Returns
-------
int
Sum of `a` and `b`.
"""
return a + b
但是,在使用Sphinx创建 HTML 文档时,我遇到了困难。它根本不会创建分派函数的文档。对于未调度的函数(例如add
在我的示例中),文档是正确的。发出命令Sphinx
时不会抛出任何错误。make html
这是我的md.rst
内容:
*************
API Reference
*************
.. automodule:: md
:members:
我不确定对 Sphinx 的真正期望(即结果应该如何),但help(calc)
在命令行窗口中发出会显示以下内容:
help(calc)
Help on Dispatcher in module multipledispatch.dispatcher:
calc = <dispatched calc>
Multiply dispatched method: calc
Inputs: <int, int>
-------------------
Adds two integers
Parameters
----------
a, b : int
Integer to be added
Returns
-------
int
Sum of `a` and `b`.
Inputs: <str, str>
-------------------
Adds two integers
Parameters
----------
a, b : str
Strings to be concatenated
Returns
-------
str
Concatenated string.
Inputs: <float, str>
---------------------
Adds two floats.
The second float wil be transformed from a string.
Parameters
----------
a : float
First summand.
b : str
Second summand, will be transformed into flaot.
Returns
-------
float
Sum of `a` and `b`
如果我能通过Sphinx
.