Как запустить doctest с примерами в блоках кода уценки для mkdocs?Python

Программы на Python
Ответить Пред. темаСлед. тема
Гость
 Как запустить doctest с примерами в блоках кода уценки для mkdocs?

Сообщение Гость »


I'm using mkdocs & mkdocstring to build my documentation and including code examples in the docstrings. I'm also using doctest (via pytest --doctest-modules) to test all those examples.
Option 1 - format for documentation
If I format my docstring like this:

""" 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] ``` """ Then it renders nicely in the documentation but doctest fails with the error:

Expected: [1, 2, 3, 4, 5, 6, 7, 8, 9] ``` Got: [1, 2, 3, 4, 5, 6, 7, 8, 9] That makes sense as doctest treats everything until a blank line as expected output and aims to match is exactly
Option 2 - format for doctest
If I format the docstring for doctest without code blocks:

""" 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] """ then doctest passes but the documentation renders

[x for x in flatten([1,2,[3,4,[5],6],7,[8,9]])][1, 2, 3, 4, 5, 6, 7, 8, 9]
Workaround? - add a blank line for doctest
If I format it with an extra blank line before the end of the codeblock:

""" 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] ``` """ Then doctest passes but
  • there is a blank line at the bottom of the example in the documentation (ugly)
  • I need to remember to add a blank line at the end of each example (error prone and annoying)

Does anyone know of a better solution?


Источник: https://stackoverflow.com/questions/780 ... for-mkdocs
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Как передать параметры doctest при запуске make doctest из Sphinx?
    Anonymous » » в форуме Python
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Как передать параметры doctest при запуске make doctest из Sphinx?
    Anonymous » » в форуме Python
    0 Ответы
    20 Просмотры
    Последнее сообщение Anonymous
  • Почему при загрузке уценки в Jupyter из файла отступ меньше, чем ячейка уценки?
    Anonymous » » в форуме Python
    0 Ответы
    19 Просмотры
    Последнее сообщение Anonymous
  • Как создать оглавление с метаданными для набора страниц в mkdocs?
    Anonymous » » в форуме Python
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous
  • Как создать оглавление с метаданными для набора страниц в mkdocs?
    Anonymous » » в форуме Python
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»