Ask Your Question

The function run_doctests creates a TypeError

asked 2021-03-10 16:46:12 +0200

DavidAyotte gravatar image

updated 2021-03-10 19:57:10 +0200

Hello everyone,

I've been writing a sage module in which I've written some documentation examples and tests. I wanted to run a doctest just to verify if everything is correct but function "run_doctests" seems to introduce a non-expected bug in the software. Here's an exampe:

sage: run_doctests("module.sage")
too few successful tests, not using stored timings
Running doctests with ID 2021-03-10-10-13-36-df8ccbc8.
Using --optional=sage
Doctesting 1 file.
sage -t /path-to-module.sage
    [40 tests, 0.07 s]
All tests passed!
Total time for all tests: 0.1 seconds
    cpu time: 0.1 seconds
    cumulative wall time: 0.1 seconds
sage: M = ModularForms()
sage: M
    Modular Forms space of dimension 0 for Modular Group SL(2,Z) of weight 2 over Rational Field
TypeError                                 Traceback (most recent call last)
/usr/lib/python3/dist-packages/sage/ in <module>
----> 1 M

/usr/lib/python3/dist-packages/IPython/core/ in __call__(self, result)
    260             self.start_displayhook()
    261             self.write_output_prompt()
--> 262             format_dict, md_dict = self.compute_format_data(result)
    263             self.update_user_ns(result)
    264             self.fill_exec_result(result)

/usr/lib/python3/dist-packages/IPython/core/ in compute_format_data(self, result)
    150         """
--> 151         return
    153     # This can be set to True by the write_output_prompt method in a subclass

/usr/lib/python3/dist-packages/sage/repl/display/ in format(self, obj, include, exclude)
    198         """
    199         # First, use Sage rich output if there is any
--> 200         sage_format, sage_metadata =
    201         assert PLAIN_TEXT in sage_format, 'plain text is always present'
    202         if not set(sage_format.keys()).issubset(self.default_mime()):

TypeError: cannot unpack non-iterable NoneType object

Here's more info about the error: it happens whenever I use the run_doctests function. The run_doctests seems to work as it should. However, the problem occurs after I used this function. For example, If I define any object from any class (in my example ModularForms), then when use the special method _repr_ of that object I obtain this error message. I don't think this have anything to do with the ModularForms class.

Do anybody else know what could introduce this error? I was wondering if this error comes from my module or from Sagemath. In my module, I've been following the general conventions of the developper guide.

I've tried both SageMath version 9.0 (from the debian SID package) and SageMath version 9.3.beta8 (compiled from source) and I've got the same error with both versions. If this error comes from the software, I shall open a sage-trac ticket about this issue.

Thank you very much in advance!

edit: I added more information about the issue.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2021-03-10 21:03:54 +0200

Sébastien gravatar image

updated 2021-03-16 11:00:53 +0200

Instead of

 sage: run_doctests("module.sage")

in the sage console (which runs the code and may have side effects), I would recommand to run instead:

$ sage -t module.sage

from the command line. You may also want to read the help since it has many options:

$ sage -t -h
edit flag offensive delete link more


In other words, run_doctests is buggy and should not be used? Has anyone ever investigated the nature of those bugs?

DavidAyotte gravatar imageDavidAyotte ( 2021-03-10 21:16:49 +0200 )edit

It looks like it's buggy in how it interacts with an interactive Sage. The underlying code of run_doctests is the engine that runs all of Sage's doctests, just via sage -t ... rather than interactively. The underlying code is very careful about setting the environment so that doctesting is in a sane controlled situation, but maybe it's bad at restoring to the original environment when it's done.

John Palmieri gravatar imageJohn Palmieri ( 2021-03-11 03:35:18 +0200 )edit

This has been noticed before, but no solutions yet:

John Palmieri gravatar imageJohn Palmieri ( 2021-03-11 03:36:53 +0200 )edit

Oh, thanks for pointing this ticket to me. I wasn't aware of it. I guess that I will just do the tests in the console as I am unsure about how to fix that. Thanks!

DavidAyotte gravatar imageDavidAyotte ( 2021-03-11 04:02:36 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-03-10 16:42:35 +0200

Seen: 235 times

Last updated: Mar 16 '21