14
14
from _pytest .doctest import (
15
15
DoctestItem ,
16
16
DoctestModule ,
17
- _get_checker ,
18
17
_get_continue_on_failure ,
19
18
_get_runner ,
20
19
_is_mocked ,
24
23
from _pytest .pathlib import import_path
25
24
import inspect
26
25
27
- import sage .all # type: ignore # to avoid cyclic import errors, see Trac #33580
28
- from sage .doctest .parsing import SageDocTestParser
29
-
26
+ # Import sage.all is necessary to:
27
+ # - avoid cyclic import errors, see Trac #33580
28
+ # - inject it into globals namespace for doctests
29
+ import sage .all
30
+ from sage .doctest .parsing import SageDocTestParser , SageOutputChecker
30
31
31
32
def pytest_collect_file (file_path , parent ):
32
33
if file_path .suffix == ".py" :
@@ -37,7 +38,8 @@ class SageDoctestModule(DoctestModule):
37
38
"""
38
39
This is essentially a copy of `DoctestModule` from
39
40
https://github.com/pytest-dev/pytest/blob/main/src/_pytest/doctest.py.
40
- The only change is that we use `SageDocTestParser` to extract the doctests.
41
+ The only change is that we use `SageDocTestParser` to extract the doctests
42
+ and `SageOutputChecker` to verify the output.
41
43
"""
42
44
43
45
def collect (self ) -> Iterable [DoctestItem ]:
@@ -103,7 +105,7 @@ def _find(
103
105
runner = _get_runner (
104
106
verbose = False ,
105
107
optionflags = optionflags ,
106
- checker = _get_checker (),
108
+ checker = SageOutputChecker (),
107
109
continue_on_failure = _get_continue_on_failure (self .config ),
108
110
)
109
111
@@ -114,9 +116,6 @@ def _find(
114
116
)
115
117
116
118
117
- from sage .all import * # type: ignore # to avoid cyclic import errors, see Trac #33580, and is implicitly used below by calling globals()
118
-
119
-
120
119
@pytest .fixture (autouse = True )
121
120
def add_imports (doctest_namespace : dict [str , Any ]):
122
121
"""
@@ -125,4 +124,13 @@ def add_imports(doctest_namespace: dict[str, Any]):
125
124
See `pytest documentation <https://docs.pytest.org/en/stable/doctest.html#doctest-namespace-fixture>`.
126
125
"""
127
126
# Inject sage.all into each doctest
128
- doctest_namespace .update (** globals ())
127
+ dict_all = sage .all .__dict__
128
+
129
+ # Remove '__package__' item from the globals since it is not
130
+ # always in the globals in an actual Sage session.
131
+ dict_all .pop ('__package__' , None )
132
+
133
+ sage_namespace = dict (dict_all )
134
+ sage_namespace ['__name__' ] = '__main__'
135
+
136
+ doctest_namespace .update (** sage_namespace )
0 commit comments