-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: add ParserKlass key to DTConfig; check xdoctest.DocTestParser #174
base: main
Are you sure you want to change the base?
Conversation
The PR is green on CI because
I wonder if https://github.com/scipy/scipy_doctest/pull/174/files#diff-75fda68530c6a37e0fa0d24cc9a00b0c40093965801a2d9eda1917a2e2567c27R118 is the correct way to use |
That API is an intermediate step which returns a list of doctest parts (as well as non-doctest strings in the docstr). The return type of DoctestParser.parse is annotated incorrectly (something I just fixed as well as improving the example in this function). It should be Here is a MWE illustrating what xdoctest can do with the above test: def mwe():
docstr = """
Check that printed arrays are checked with atol/rtol
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4]) / 3
>>> print(a)
[0.33 0.66 1 1.33]
"""
import ubelt as ub
import xdoctest
from xdoctest.doctest_example import DocTest as XDoctest
# With the core API (removes surrounding string and just returns completed
# doctest objects)
doctests: list[XDoctest] = list(xdoctest.core.parse_docstr_examples(docstr))
for doctest in doctests:
result = doctest.run(on_error='return')
print(f'result = {ub.urepr(result, nl=1)}')
# With the Doctest Part API (similar to the stdlib API, but can group
# contiguous statements without a "got/want" structure)
from xdoctest.doctest_part import DoctestPart as XDoctestPart
xparser = xdoctest.parser.DoctestParser()
xparts: list[str | XDoctestPart] = xparser.parse(docstr)
print(f'xparts = {ub.urepr(xparts, nl=1)}')
# With stdlib API
import doctest
stdparser = doctest.DocTestParser()
stdparts = stdparser.parse(docstr)
print(f'stdparts = {ub.urepr(stdparts, nl=1)}')
if __name__ == '__main__':
mwe() This prints:
|
Thanks @Erotemic ! I've updated the drop-in test to account for the difference in
One thing to note is that |
Maybe, but maybe not for those reasons. Doctest parts cannot be run independently. They just tell you the lines of code to run (and what directives they will apply), but they have no context. Unlike stdlib doctest, xdoctest maintains a state as it executes a sequence of "examples / parts". Maybe this is ok, as the stdlib doctest runner needs to do something similar, as long as you don't do anything you wouldn't be able to do with regular doctest. Note: if you instantiate the parser via
That should be completely irrelevant. Everything should already be dedented based on the otput of the parser. I'm really not sure why stdlib doctests need this at all.
In xdoctest these are maintained only at the Something else to note is that xdoctest doesn't really have a concept of a "runner" as it is built into the run method of |
Add plug-in ParserKlass key, illustrate on
xdoctest
's parser. Cross-ref #59