Erotemic / xdoctest

A rewrite of Python's builtin doctest module (with pytest plugin integration) with AST instead of REGEX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic docstring not recognized

ArneBachmannDLR opened this issue · comments

def func() -> None:
  r''' Dynamic doctest
  >>> %s
  %s
  '''
  return
func.__doc__ %= ('print(1)', '1')

Run via xdoctest --analysis dynamic x.py fails, because xdoctest seems to use the source code, not the doctring from the import.

Thanks for this report, I never would have caught this case.

It turns out there is a typo/bug where in package_calldefs even though it accepts an "analysis" argument, I fail to pass it to parse_calldefs.

Changing that line to calldefs = parse_calldefs(module_identifier, analysis=analysis) fixes it. I'll push that up as a patch.

In the meantime there is a workaround. There is an undocumented deprecated command line argument I was using for debugging, forgot about, and never removed. If you add --xdoc-force-dynamic to the command line it will force dynamic mode to always be used.

FWIW, the reason that dynamic mode exists at all is to be able to run doctests inside of C extension modules. IMHO code is simpler and better when it is easy for static analysis to work on it. For doctests, I think its especially valuable for them to be readable in the plaintext source code. I would suggest you rethink why you are doing this, and maybe save yourself a complexity headache later.

Regardless, xdoctest should still support this case. I've added the patch in #113

Great news! Yeah, I wish I could avoid it, too.
Right now I have a switch --offline that disables all network services I usually require, even during testing. Due to the Expect - Got logic of testing doctests I cannot simply make the test call conditional inside >>>, as the expected output would only be true in one case, but not when offline.
Therefore I made the actual expected part conditional by updating the docstring (weird, I know).

I guess using plain old unittest would have been much easier, but I'm really committed to using only doctests ;-)

I do have a similar flag in many of my repos, the exception being that I explicitly enable networks tests.

You can "guard" specific blocks of doctest code with the REQUIRES directive. For any test that touches the network I put a >>> # xdoctest: +REQUIRES(--network) which causes all following lines to be skipped until the doctest ends or the >>> # xdoctest: -REQUIRES(--network) directive is encountered.

Also, nothing is stopping you from just using asserts inside doctests. I think that's generally better practice anyway. Unless I'm not understanding details of your offline testing process.

@ArneBachmannDLR the patch (0.15.10) that fixes the primary --analysis=dynamic CLI arg has been merged, and should be on pypi shortly.

Works now! Thanks for the quick fix!