[issue32509] doctest syntax ambiguity between continuation line and ellipsis

Steven D'Aprano report at bugs.python.org
Sat Jan 6 23:35:34 EST 2018


Steven D'Aprano <steve+python at pearwood.info> added the comment:

Here's a simple demonstration of the issue:


# --- cut %< ---
import doctest

def hash_files():
    """
    >>> hash_files()  # doctest: +ELLIPSIS
    ...
    d41d8cd98f00b204e9800998ecf8427e __init__.py
    ...

    """
    print("""\
e1f9390d13c90c7ed601afffd1b9a9f9 records.py
6a116973e8f29c923a08c2be69b11859 ledger.py
d41d8cd98f00b204e9800998ecf8427e __init__.py
b83c8a54d6b71e28ccb556a828e3fa5e qif.py
ac2d598f65b6debe9888aafe51e9570f ofx.py
9f2572f761342d38239a1394f4337165 msmoney.py
""")

doctest.run_docstring_examples(hash_files, globals())

# --- cut %< ---


The documentation does say that output must follow the final >>> or ... 

https://docs.python.org/3/library/doctest.html#how-are-docstring-examples-recognized

so I believe this is expected behaviour and not a bug.

Here is a workaround. Change the doctest to something like this:


    >>> print('#', end=''); hash_files()  # doctest: +ELLIPSIS
    #...
    d41d8cd98f00b204e9800998ecf8427e __init__.py
    ...



But a more elegant solution would be to add a new directive to tell doctest to interpret the ... or >>> as output, not input, or to add a new symbol similar to <BLANKLINE>.

I'm changing this to an enhancement request as I think this would be useful.

----------
components: +Library (Lib)
nosy: +steven.daprano, tim.peters -r.david.murray
type: behavior -> enhancement
versions: +Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32509>
_______________________________________


More information about the Python-bugs-list mailing list