我试图用斯芬克斯自动文档,拿破仑和autodoc_typehints自动文档类型,但我有问题,因为它不适用于我的大多数类型。我正在使用deap软件包做一些遗传优化算法,这使得我有一些非常特殊的类型,我想斯芬克斯无法处理。
我的conf.py文件如下所示:
import os
import sys
sys.path.insert(0, os.path.abspath('../python'))
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinx.ext.napoleon',
'sphinx_autodoc_typehints'
]
set_type_checking_flag = False
always_document_param_types = False我有一个Algo.rst文件:
.. automodule:: python.algo.algo
:members: crossover_worker,
test我的python.algo.algo模块如下所示(我添加了一个虚拟测试函数来显示它在没有指定的特殊类型时工作):
# Type hinting imports
from config.config import Config
from typing import List, Set, Dict, NamedTuple, Union, Tuple
from types import ModuleType
from numpy import ndarray
from numpy import float64
from multiprocessing.pool import MapResult
from deap.tools.support import Logbook, ParetoFront
from deap.base import Toolbox
from pandas.core.frame import DataFrame
from deap import creator
...
def crossover_worker(sindices: List[creator.Individual, creator.Individual]) -> Tuple[creator.Individual, creator.Individual]:
"""
Uniform crossover using fixed threshold
Args:
sindices: list of two individuals on which we want to perform crossover
Returns:
tuple of the two individuals with crossover applied
"""
ind1, ind2 = sindices
size = len(ind1)
for i in range(size):
if random.random() < 0.4:
ind1[i], ind2[i] = ind2[i], ind1[i]
return ind1, ind2
def test(a: DataFrame, b: List[int]) -> float:
"""
test funcition
Args:
a: something
b: something
Returns:
something
"""
return b当conf.py中的设置与上面一样时,我没有错误,我的test函数的类型是正确的,但是我的crossover_worker函数的类型缺少:

但是,当我将set_type_checking_flag= True设置为强制使用所有类型时,会出现一个循环导入错误:
reading sources... [100%] index
WARNING: autodoc: failed to import module 'algo' from module 'python.algo'; the following exception was raised:
cannot import name 'ArrayLike' from partially initialized module 'pandas._typing' (most likely due to a circular import) (/usr/local/lib/python3.8/site-packages/pandas/_typing.py)
looking for now-outdated files... none found我从来不导入ArrayLike,所以我不能从它的来源或如何解决它得到它?或者如何强制导入代码中随处可见的creator.Individual类型?
我的狮身人面像:
sphinx==3.0.1
sphinx-autodoc-typehints==1.10.3发布于 2020-04-24 09:50:56
经过一番探索,我的方法有一些缺陷:
首先,
List只接受一种类型,该列表中的每个元素都必须具有该类型。“()。因此,我不能执行类似于List[Union[int,float]]List[creator.Individual],或者如果列表中有多个类型,则应该使用一个联合运算符,例如List[creator.Individual, creator.Individual] -- sphinx不能将creator.Individual类型识别为有效类型。相反,我应该使用TypeVar来定义它:from typing import TypeVar, List
CreatorIndividual = TypeVar("CreatorIndividual", bound=List[int])因此,通过将我的crossover_worker函数转换成这个函数,这一切都起了作用:
def crossover_worker(sindices: List[CreatorIndividual]) -> Tuple[CreatorIndividual, CreatorIndividual]:注意:“与此相反,元组是产品类型的示例,是由一组固定类型组成的类型,其值是值的集合,每个值来自产品类型中的每种类型。Tuple[int,int,int]__、Tuple[str,int]和Tuple[int,str]都是不同的类型,它们的区别在于产品中类型的数量和它们出现的顺序。”()
https://stackoverflow.com/questions/61245710
复制相似问题