首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Sphinx autodoc不显示所有类型或循环导入错误。

Sphinx autodoc不显示所有类型或循环导入错误。
EN

Stack Overflow用户
提问于 2020-04-16 08:21:15
回答 1查看 1K关注 0票数 1

我试图用斯芬克斯自动文档,拿破仑和autodoc_typehints自动文档类型,但我有问题,因为它不适用于我的大多数类型。我正在使用deap软件包做一些遗传优化算法,这使得我有一些非常特殊的类型,我想斯芬克斯无法处理。

我的conf.py文件如下所示:

代码语言:javascript
运行
复制
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文件:

代码语言:javascript
运行
复制
.. automodule:: python.algo.algo
     :members: crossover_worker,
               test

我的python.algo.algo模块如下所示(我添加了一个虚拟测试函数来显示它在没有指定的特殊类型时工作):

代码语言:javascript
运行
复制
# 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设置为强制使用所有类型时,会出现一个循环导入错误:

代码语言:javascript
运行
复制
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类型?

我的狮身人面像:

代码语言:javascript
运行
复制
sphinx==3.0.1
sphinx-autodoc-typehints==1.10.3
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-24 09:50:56

经过一番探索,我的方法有一些缺陷:

首先,

  • a "list是包含一种类型的值的同构结构,因此,List只接受一种类型,该列表中的每个元素都必须具有该类型。“()。因此,我不能执行类似于List[Union[int,float]]
  • Secondly,的操作,但是应该将其转换为List[creator.Individual],或者如果列表中有多个类型,则应该使用一个联合运算符,例如List[creator.Individual, creator.Individual] -- sphinx不能将creator.Individual类型识别为有效类型。相反,我应该使用TypeVar来定义它:

代码语言:javascript
运行
复制
from typing import TypeVar, List
CreatorIndividual = TypeVar("CreatorIndividual", bound=List[int])

因此,通过将我的crossover_worker函数转换成这个函数,这一切都起了作用:

代码语言:javascript
运行
复制
def crossover_worker(sindices: List[CreatorIndividual]) -> Tuple[CreatorIndividual, CreatorIndividual]:

注意:“与此相反,元组是产品类型的示例,是由一组固定类型组成的类型,其值是值的集合,每个值来自产品类型中的每种类型。Tuple[int,int,int]__、Tuple[str,int]Tuple[int,str]都是不同的类型,它们的区别在于产品中类型的数量和它们出现的顺序。”()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61245710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档