Python和sphinx:多行google风格docstring中的项目符号列表

问题描述 投票:3回答:2

我目前正在使用Sphinx记录我的Python项目。在文档字符串的多行部分中包含项目符号列表时,我遇到了一个问题。

我想列出一个项目符号列表,但其中一个项目很长。我想要:

  • 通过Sphinx正确呈现子弹列表
  • 还有我的代码尊重PEP8线长(<79)

你有什么建议让我为这个文档字符串做些什么:

class geography():
""" Class defining a geography (cities and distance matrix)

This class implements a geography with a list of named cities with their
associated coordinates in a plane. Helper functions enable to :

- give a visual representation of that geography
- give a visual representation of the distance matrix
- give a visual representation of a configuration, a configuration being the repartition of some or all cities in pools

...

最后一行超过79个字符。

然后通过Sphinx呈现评论。添加回车只会破坏Sphinx中的项目符号列表。

python python-sphinx docstring google-style-guide
2个回答
3
投票

你可以随意break the bulleted线。只需使用前面的行文本排列延续,如:

- give a visual representation of that geography
- give a visual representation of the distance matrix
- give a visual representation of a configuration, a configuration being the
  repartition of some or all cities in pools

2
投票

来自@Stephen Rauch的解决方案是完美的。我只是想补充一点,它也适用于非项目符号列表。我对函数或方法的参数的注释有类似的问题。例如:

def permute_array(arr, seq):
""" Function to "square permute" a 2D array

This function's purpose is to enable distance matrices permutations. That 
is, for example, permute both lines and columns of the array so as to 
reorder a distance matrix.

Args:
    arr (numpy array): the array to permute. It should be square of size n.
    seq (iterable of int): a permutation of range(n) (should be of length n and contain every integer from 0 to n-1)

最后一行太长了。

但是,“相同的缩进级别”换行符会破坏好的sphinx方法文档:

    Args:
        arr (numpy array): the array to permute. It should be square of size n.
        seq (iterable of int): a permutation of range(n) (should be of length n
        and contain every integer from 0 to n-1)

Badly built documentation

但是,用缩进来打破这条线就行了。

    Args:
        arr (numpy array): the array to permute. It should be square of size n.
        seq (iterable of int): a permutation of range(n) (should be of length n
            and contain every integer from 0 to n-1)

Nicely built documentation

© www.soinside.com 2019 - 2024. All rights reserved.