Running some code before the doctests

35 views
Skip to first unread message

Jean-Florent Raymond

unread,
Aug 17, 2021, 11:13:57 AM8/17/21
to sage-...@googlegroups.com
Hello,

Is there a way to run some code before the doctests of a file are performed?

Consider the following simple example of a module "mymodule" where is
defined a class "A".

mymodule.py:
---
class A():

def method1(self):
"""
Return 123.

TESTS::

sage: from mymodule import A
sage: a = A()
sage: a.method1()
123
"""
return 123


def method2(self):
"""
Return 456.

TESTS::

sage: from mymodule import A
sage: a = A()
sage: a.method2()
456
"""
return 456

It seems that I have to import "A" in the doctest of each method,
otherwise I get an error that it is not defined.
Is there a way to import "A" (and even to define "a") only once for all
the doctests of the file?

By doing some research online about doctests before writing this message
I learned about the existence of the "extraglobs" parameter but I do not
see how I could use it here ( 2nd answer of
https://stackoverflow.com/questions/2708178/python-using-doctests-for-classes
).

Any idea?

If this matters, the module I am working on is of the form of a
pip-installable package prepared following the example at
https://github.com/sagemath/sage_sample .

Best regards,
Jean-Florent.

Matthias Koeppe

unread,
Aug 17, 2021, 1:58:46 PM8/17/21
to sage-devel
On Tuesday, August 17, 2021 at 8:13:57 AM UTC-7 Jean-Florent Raymond wrote:
It seems that I have to import "A" in the doctest of each method,
otherwise I get an error that it is not defined.

Yes, that's normal; after all, users of your package will have to do the same when they want to run these examples...
 
If this matters, the module I am working on is of the form of a
pip-installable package prepared following the example at
https://github.com/sagemath/sage_sample .

Unrelated, but let me point out that sage_sample is unfortunately fairly outdated regarding its packaging practices:
1) Adding commands such as "setup.py test" is no longer recommended
2) Using "setup_requires" is outdated, and instead pyproject.toml should be used to declare the build system 

 

Jean-Florent Raymond

unread,
Aug 18, 2021, 11:33:49 AM8/18/21
to sage-...@googlegroups.com


Le 17/08/2021 à 19:58, Matthias Koeppe a écrit :
> On Tuesday, August 17, 2021 at 8:13:57 AM UTC-7 Jean-Florent Raymond wrote:
>
>> It seems that I have to import "A" in the doctest of each method,
>> otherwise I get an error that it is not defined.
>
>
> Yes, that's normal; after all, users of your package will have to do the
> same when they want to run these examples...
>

OK, thanks! I will do this then.

>
>> If this matters, the module I am working on is of the form of a
>> pip-installable package prepared following the example at
>> https://github.com/sagemath/sage_sample .
>
>
> Unrelated, but let me point out that sage_sample is unfortunately fairly
> outdated regarding its packaging practices:
> 1) Adding commands such as "setup.py test" is no longer recommended
> 2) Using "setup_requires" is outdated, and instead pyproject.toml should be
> used to declare the build system
>

Thanks for pointing out that!
Reply all
Reply to author
Forward
0 new messages