3 回答

TA貢獻1829條經(jīng)驗 獲得超9個贊
我剛剛創(chuàng)建了一個工具來實現(xiàn)這一目標(biāo),稱為pydoctest
.
它將嘗試推斷文檔字符串中的類型(不僅僅是詞法比較)并報告參數(shù)數(shù)量、參數(shù)名稱、參數(shù)類型、返回類型之間的不匹配,(可選)如果缺少文檔字符串等則拋出錯誤。
它目前支持 google、sphinx 和 numpy 文檔字符串格式,但可以很容易地使用其他格式進行擴展。
例子:
def func_type_mismatch(self, a: int) -> int:
? ? """[summary]
? ? Args:
? ? ? ? a (float): [description]? ? ? ? <-- float is not int
? ? Returns:
? ? ? ? int: [description]
? ? """
? ? pass
在此函數(shù)上運行 pydoctest 會給出以下輸出:
Function: <function IncorrectTestClass.func_type_mismatch at 0x7f9a8b52c8c8> FAIL | Argument type differ. Argument 'a' was expected (from signature) to have type '<class 'int'>', but has (in docs) type '<class 'float'>'

TA貢獻1815條經(jīng)驗 獲得超10個贊
pip install docsig
然后運行docsig .
它就會為你檢查這一點
/path/to/project
-----------------------
def function(?*args) -> ?List[str]:
? ? """...
? ? :param None: ?
? ? :return: ?
? ? """
E103: parameters missing
目前還有8個錯誤

TA貢獻1859條經(jīng)驗 獲得超6個贊
除了其他答案中提到的pydoctest和docsig之外,至少還有這些工具:
pydoclint與?pydocstyle或ruff一起使用
Pylint 的docparams 擴展
flake8-文檔字符串-完整
darglit2(對于大型項目來說速度慢得驚人)
添加回答
舉報