Repository navigation
Variables injected in doctest_namespace no longer loaded in Pytest 9.1.x #14683
Copy link
Copy link
Closed
Description
Description
I'm running into issues with pytest 9.1.x when running my doctests. I've been able to inject variables and functions into the namespace for doctests via a fixture so that I don't need to explicitly write these things into all my doctests, e.g.:
@pytest.fixture(scope="session", autouse=True)
def doctest_setup(doctest_namespace, nimbus, worker_id, open_dataset) -> None: # numpydoc ignore=PR01
"""Gather testing data on doctest run."""
testing_setup_warnings()
gather_testing_data(worker_cache_dir=nimbus.path, worker_id=worker_id)
doctest_namespace.update(custom_testing_data(nimbus=nimbus))
class AttrDict(dict): # numpydoc ignore=PR01
"""A dictionary that allows access to its keys as attributes."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
xr = AttrDict()
xr["DataArray"] = xarray.DataArray
xr["date_range"] = xarray.date_range
xr["open_dataset"] = custom_library.open_dataset
doctest_namespace["xr"] = xr
doctest_namespace.update(add_doctest_filepaths())
doctest_namespace.update(add_example_file_paths())This has been working well for many years, but it seems like there's been a major change in how the latest pytest collects/runs fixtures for doctests.
All my tests are now failing with NameError: name '<variable/function name injected from doctest_setup>' is not defined.
Environment Setup
Package Version
----------------------------- ------------
alabaster 1.0.0
annotated-doc 0.0.4
annotated-types 0.7.0
anyio 4.14.1
ast_serialize 0.6.0
astroid 4.0.4
asttokens 3.0.1
attrs 26.1.0
babel 2.18.0
beautifulsoup4 4.15.0
bleach 6.4.0
boltons 26.0.0
Bottleneck 1.6.0
bracex 3.0
bump-my-version 1.4.1
cachetools 7.1.4
certifi 2026.6.17
cf_xarray 0.11.3
cfgv 3.5.0
cftime 1.6.5
charset-normalizer 3.4.8
click 8.4.2
cloudpickle 3.1.2
codespell 2.4.2
colorama 0.4.6
comm 0.2.3
contourpy 1.3.3
coverage 7.15.0
cycler 0.12.1
dask 2026.6.0
debugpy 1.8.21
decorator 5.3.1
defusedxml 0.7.1
deptry 0.25.1
dill 0.4.1
distlib 0.4.3
docutils 0.22.4
execnet 2.1.2
executing 2.2.1
fastjsonschema 2.21.2
filelock 3.29.5
flake8 7.3.0
flake8-rst-docstrings 0.4.0
flexcache 0.3
flexparser 0.4
flox 0.11.2
fonttools 4.63.0
fsspec 2026.6.0
h11 0.16.0
h5netcdf 1.8.1
h5py 3.16.0
httpcore2 2.5.0
httpx2 2.5.0
identify 2.6.19
idna 3.18
imagesize 2.0.0
iniconfig 2.3.0
ipykernel 7.3.0
ipython 9.15.0
ipython_pygments_lexers 1.1.1
isort 8.0.1
jedi 0.20.0
Jinja2 3.1.6
joblib 1.5.3
jsonpickle 4.1.2
jsonschema 4.26.0
jsonschema-specifications 2025.9.1
jupyter_client 8.9.1
jupyter_core 5.9.1
jupyterlab_pygments 0.3.0
kiwisolver 1.5.0
librt 0.12.0
llvmlite 0.48.0
lmoments3 1.0.8
locket 1.0.0
markdown-it-py 4.2.0
MarkupSafe 3.0.3
matplotlib 3.11.0
matplotlib-inline 0.2.2
mccabe 0.7.0
mdurl 0.1.2
mistune 3.3.2
mypy 2.1.0
mypy_extensions 1.1.0
narwhals 2.23.0
nbclient 0.11.0
nbconvert 7.17.1
nbformat 5.10.4
nbval 0.11.0
nest-asyncio2 1.7.2
nodeenv 1.10.0
numba 0.66.0
numbagg 0.9.4
numpy 2.4.6
numpy-groupies 0.11.3
numpydoc 1.10.0
packaging 26.2
pandas 3.0.3
pandas-stubs 3.0.3.260530
pandocfilters 1.5.1
parso 0.8.7
partd 1.4.2
pathspec 1.1.1
patsy 1.0.2
pexpect 4.9.0
pillow 12.3.0
Pint 0.25.3
pip 26.1.2
platformdirs 4.10.0
pluggy 1.6.0
pooch 1.9.0
pre_commit 4.6.0
prompt_toolkit 3.0.52
psutil 7.2.2
ptyprocess 0.7.0
pure_eval 0.2.3
pyarrow 24.0.0
pycodestyle 2.14.0
pydantic 2.13.4
pydantic_core 2.46.4
pydantic-settings 2.14.2
pyflakes 3.4.0
Pygments 2.20.0
pylint 4.0.6
pymannkendall 1.4.3
pyparsing 3.3.2
pyproject-api 1.10.1
pytest 9.1.1
pytest-cov 7.1.0
pytest-socket 0.8.0
pytest-timeout 2.4.0
pytest-xdist 3.8.0
python-dateutil 2.9.0.post0
python-discovery 1.4.3
python-dotenv 1.2.2
PyYAML 6.0.3
pyzmq 27.1.0
questionary 2.1.1
referencing 0.37.0
requests 2.34.2
requirements-parser 0.13.1
restructuredtext_lint 2.0.2
rich 15.0.0
rich-click 1.9.8
roman-numerals 4.1.0
rpds-py 2026.6.3
ruff 0.15.20
scikit-learn 1.9.0
scipy 1.18.0
shellingham 1.5.4
six 1.17.0
snowballstemmer 3.1.1
soupsieve 2.8.4
Sphinx 9.1.0
sphinxcontrib-applehelp 2.0.0
sphinxcontrib-devhelp 2.0.0
sphinxcontrib-htmlhelp 2.1.0
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 2.0.0
sphinxcontrib-serializinghtml 2.0.0
stack-data 0.6.3
statsmodels 0.14.6
threadpoolctl 3.6.0
tinycss2 1.5.1
tokenize_rt 6.2.0
tomli 2.4.1
tomli_w 1.2.0
tomlkit 0.15.0
toolz 1.1.0
tornado 6.5.7
tox 4.56.1
tox-gh 1.7.1
traitlets 5.15.1
truststore 0.10.4
typer 0.26.8
typing_extensions 4.16.0
typing-inspection 0.4.2
urllib3 2.7.0
virtualenv 21.5.2
vulture 2.16
wcmatch 10.2.1
wcwidth 0.8.2
webencodings 0.5.1
xarray 2026.4.0
xclim 0.61.2.dev1
xsdba 0.7.0
yamale 6.1.0
yamllint 1.38.0
Questions
- Is this a bug or a new design direction?
- Is there a new method to set up
pytestfixtures for doctests that maintains the same behaviour as before?
Possibly related: #14635
Metadata
Metadata
Assignees
Labels
No labels
Activity
The-Compiler commentedon Jul 7, 2026
Can you show a minimal reproducer? Without more information, assuming this is indeed just a duplicate of #14635 and the fixture simply isn't run.
Maybe you could also add an
1/0to the fixture (or run with--setup-showfor a less crude approach) to see if the fixture is getting run at all.Zeitsperre commentedon Jul 7, 2026
Sure thing. Here's the simplified structure of my project:
In my project, the call to run the doctests is:
Under
pytest9.0, I can see the following being called:Under
pytest9.1, it looks like almost no fixtures are loaded (running with-xflag):When adding a
ZeroDivisionErrorto my fixture, it correctly fails inpytestv9.0 and is never reached in v9.1.The-Compiler commentedon Jul 7, 2026
Probably indeed a duplicate of #14635 then. @RonnyPfannschmidt wdyt?
Drop xdoctest (#2383)
github-actions commentedon Jul 22, 2026
This issue is stale because it has the
status: needs informationlabel and requested follow-up information was not provided for 14 days.Zeitsperre commentedon Jul 22, 2026
If any other information is required, please let me know!
RonnyPfannschmidt commentedon Jul 22, 2026
not a dupe #14694 fixes it
Fix rootdir conftest fixtures not visible outside rootdir (#14683)
fix: validate -c/--config-file and read [pytest] from custom TOML files
fix: validate -c/--config-file and read [pytest] from custom TOML files
fix: validate -c/--config-file and read [pytest] from custom TOML files
chore(deps): update dependency pytest to <9.2.1,>=9.2.0 (#368)
chore(deps): update dependency pytest to <9.2.1,>=9.2.0 (#631)
chore(deps): update dependency pytest to <9.2.1,>=9.2.0 (#256)
chore(deps): update dependency pytest to <9.2.1,>=9.2.0 (#493)
chore(deps): update dependency pytest to <9.2.1,>=9.2.0 (#494)
chore(deps): update dependency pytest to <9.2.1,>=9.2.0 (#297)
chore(deps): update dependency pytest to >=9.2,<9.3 (#85)
⬆️ Update dependency pytest to v9.2.0 (#206)