Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 58e11d7

Browse files
committed
Fix checker and namespace injection
1 parent d3fdd95 commit 58e11d7

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/conftest.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from _pytest.doctest import (
1515
DoctestItem,
1616
DoctestModule,
17-
_get_checker,
1817
_get_continue_on_failure,
1918
_get_runner,
2019
_is_mocked,
@@ -24,9 +23,11 @@
2423
from _pytest.pathlib import import_path
2524
import inspect
2625

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
3031

3132
def pytest_collect_file(file_path, parent):
3233
if file_path.suffix == ".py":
@@ -37,7 +38,8 @@ class SageDoctestModule(DoctestModule):
3738
"""
3839
This is essentially a copy of `DoctestModule` from
3940
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.
4143
"""
4244

4345
def collect(self) -> Iterable[DoctestItem]:
@@ -103,7 +105,7 @@ def _find(
103105
runner = _get_runner(
104106
verbose=False,
105107
optionflags=optionflags,
106-
checker=_get_checker(),
108+
checker=SageOutputChecker(),
107109
continue_on_failure=_get_continue_on_failure(self.config),
108110
)
109111

@@ -114,9 +116,6 @@ def _find(
114116
)
115117

116118

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-
120119
@pytest.fixture(autouse=True)
121120
def add_imports(doctest_namespace: dict[str, Any]):
122121
"""
@@ -125,4 +124,13 @@ def add_imports(doctest_namespace: dict[str, Any]):
125124
See `pytest documentation <https://docs.pytest.org/en/stable/doctest.html#doctest-namespace-fixture>`.
126125
"""
127126
# 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)

src/tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ commands = codespell \
158158
python_files = *_test.py
159159
norecursedirs = local prefix venv build pkgs .git src/sage/pkgs src/doc src/bin src/sage/src/sage_setup
160160
addopts = --import-mode importlib
161+
doctest_optionflags = NORMALIZE_WHITESPACE ELLIPSIS
161162
162163
[coverage:run]
163164
source = sage

0 commit comments

Comments
 (0)