cell magics in docstring tests raise a SyntaxError #2875

rlmv opened this Issue Feb 1, 2013 · 3 comments

3 participants


Came up while working on #2862 - I was writing a docstring unit test with %%timeit and could not get it to work. After investigating further it seems that cell magics do not work in docstring tests at all. Something like this should pass just fine:

def doctest_bash_echo():
    In [1]: %%bash
       ...: echo foo

However, I get this traceback:

Traceback (most recent call last):
  File "/Users/robertmarchman/Documents/CS/ipython/IPython/testing/plugin/ipdoctest.py", line 271, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for IPython.core.tests.test_magic.doctest_bash_echo
  File "/Users/robertmarchman/Documents/CS/ipython/IPython/core/tests/test_magic.py", line 89, in doctest_bash_echo

File "/Users/robertmarchman/Documents/CS/ipython/IPython/core/tests/test_magic.py", line 91, in IPython.core.tests.test_magic.doctest_bash_echo
Failed example:
    echo foo
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1289, in __run
        compileflags, 1) in test.globs
      File "<doctest IPython.core.tests.test_magic.doctest_bash_echo[0]>", line 1
    SyntaxError: invalid syntax

>>  raise self.failureException(self.format_failure(<StringIO.StringIO instance at 0x1074a4050>.getvalue()))

Is this something that #2447 will solve?

IPython member

It might be resolved in #2447, but my gut reaction is that we really shouldn't use doctests for cell magics. doctests are a bit hackish, and our extension to do doctests with our own syntax piles more hacks onto it. I'm trying to move away from them in favour of real tests.


Mmm interesting - I generally don't like them either, but there seem to be some cases that are difficult to reproduce in real tests, particularly when dealing with indirect output. There may not actually be to many of those though -- I don't really have a good enough handle on the code base to really know. Anyway, we can close this if it doesn't seem like an important issue.

IPython member

Let's leave it open - I've set it to low priority.

In the meantime, you can test cell magics with ip.run_cell_magic('name', 'firstline', 'body').

@minrk minrk removed the prio-low label Jan 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment