我正在使用mkdocs&;mkdocstring来构建我的文档,并在文档字符串中包含代码示例.我还使用doctest(通过pytest --doctest-modules
)来测试所有这些示例.
备选方案1--文件格式
如果我将我的文档字符串设置为如下格式:
"""
Recursively flattens a nested iterable (including strings!) and returns all elements in order left to right.
Examples:
--------
```
>>> [x for x in flatten([1,2,[3,4,[5],6],7,[8,9]])]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
"""
然后,它在文档中呈现得很好,但doctest失败,并显示错误:
Expected:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
Got:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
这是有意义的,因为doctest将everything视为预期输出,直到空白行为止,目标是匹配exactly
选项2--文档测试的格式
如果在不使用代码块的情况下格式化doctest的文档字符串:
"""
Recursively flattens a nested iterable (including strings!) and returns all elements in order left to right.
Examples:
--------
>>> [x for x in flatten([1,2,[3,4,[5],6],7,[8,9]])]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
然后,doctest通过,但文档呈现
[X代表x in Flatten([1,2,[3,4,[5],6],7,[8,9]])][1,2,3,4,5,6,7,8,9]
解决方法?-为doctest添加一个空行
如果我在代码块末尾之前使用额外的空行格式化它:
"""
Recursively flattens a nested iterable (including strings!) and returns all elements in order left to right.
Examples:
--------
```
>>> [x for x in flatten([1,2,[3,4,[5],6],7,[8,9]])]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
"""
然后文档测试通过了,但是
- 在文档中的示例底部有一个空行(丑陋)
- 我需要记住在每个示例的末尾添加一个空行(容易出错且令人讨厌)
有人知道更好的解决办法吗?