SyntaxError for old-style docstrings with multiple statements
cjolowicz opened this issue · comments
This module
"""
>>> def f():
... return 42
...
... f()
42
"""
leads to the following backtrace:
Traceback (most recent call last):
File "/tmp/xdoctest/venv/bin/xdoctest", line 8, in <module>
sys.exit(main())
File "/tmp/xdoctest/venv/lib/python3.9/site-packages/xdoctest/__main__.py", line 160, in main
run_summary = xdoctest.doctest_module(modname, argv=[command], style=style,
File "/tmp/xdoctest/venv/lib/python3.9/site-packages/xdoctest/runner.py", line 302, in doctest_module
run_summary = _run_examples(enabled_examples, verbose, config,
File "/tmp/xdoctest/venv/lib/python3.9/site-packages/xdoctest/runner.py", line 465, in _run_examples
summary = example.run(verbose=verbose, on_error=on_error)
File "/tmp/xdoctest/venv/lib/python3.9/site-packages/xdoctest/doctest_example.py", line 560, in run
code = compile(
File "<doctest:example.py::__doc__:0>", line 4
f()
^
SyntaxError: invalid syntax
Stepping into the error, it looks like xdoctest invokes compile
in single mode even though multiple statements are present.
This does not happen when only PS1 is used.
Does this work with the original doctest module? I would expect using the PS2 ...
to prefix a secondary statement should result in an error. From what I understand it should only be used to denote command continuations. The statement f()
is not a continuation of the previous AST block, so it should be formatted as such:
"""
>>> def f():
... return 42
...
>>> f()
42
"""
Alternatively, you can simply use the new xdoctest prefix rule (Use >>>
for everything), and it will work.
"""
>>> def f():
>>> return 42
>>> f()
42
"""
However, if your example does work with the original doctest module, then we should consider supporting it.
You're entirely right, and the doctest module has the same behavior here. Thank you for clarifying this!