Registry Mirror
Skip to content

Conversation

@headtr1ck

@headtr1ck headtr1ck commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Two tests depended on which tests ran before them in the same process:

  • test_lazy_import (backends/test_plugins.py) checks that importing xarray doesn't import optional dependencies like scipy or zarr. It removed those modules from sys.modules and imported xarray again. But by then they had long been imported, during collection (e.g. by the has_* checks in xarray/tests/__init__.py) and by other tests, and modules or threads left over from those tests could import them again after the purge. So the test was flaky, and running it first isn't possible: collection happens before any test, and with xdist there is no global first test.

    It now runs the check in a fresh interpreter with subprocess, which is independent of test order and xdist and takes about 0.5 s. It also matches module names exactly instead of by prefix, so e.g. pint_xarray no longer counts as pint. It still catches regressions: with an eager import scipy added to xarray, it fails with scipy have been imported but should be lazy.

  • test_default_write_engine (backends/test_datatree.py) pretended that the other netCDF library isn't installed by emptying sys.meta_path. That blocks every import that isn't cached yet, e.g. h5netcdf's lazy from .legacyapi import Dataset. So the test failed whenever nothing had imported h5netcdf.legacyapi before, e.g. always when run on its own (ModuleNotFoundError: No module named 'h5netcdf.legacyapi'), and sometimes with xdist.

    It now hides only that library with sys.modules[name] = None. That makes importlib.util.find_spec return None and imports of it fail, while all other imports keep working. The default engine still correctly falls back to the remaining library.

No changes to xarray itself, so no whats-new entry.

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code (Claude Opus 5.5)

🤖 Generated with Claude Code

…st order

test_lazy_import removed the modules from sys.modules and imported xarray
again, but modules and threads left over from other tests could import them
again. Run the check in a fresh interpreter instead, and match module names
exactly instead of by prefix.

test_default_write_engine emptied sys.meta_path to hide the other netCDF
library, which also broke lazy imports of other modules, e.g. of
h5netcdf.legacyapi, unless another test had imported them already. Hide
only that library with a None entry in sys.modules.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck
headtr1ck enabled auto-merge (squash) September 30, 2026 19:02
@headtr1ck
headtr1ck merged commit d7a67ad into pydata:main Sep 30, 2026
45 checks passed
@headtr1ck
headtr1ck deleted the fix-flaky-import-tests branch October 1, 2026 18:24
felixonmars added a commit to felixonmars/archriscv-packages that referenced this pull request Oct 27, 2026
Drop the NaT backport already included in v2026.09.0 and the warning
filter now present in Arch packaging. Refresh the PKGBUILD context and
retain the datetime input bounds fix.

Backport the default-write-engine test fix from upstream PR #11648 to
the old test path. Clearing sys.meta_path blocks h5netcdf's lazy import,
causing "No module named 'h5netcdf.legacyapi'". Hide only the unwanted
backend in sys.modules so the test remains enabled.

pydata/xarray#11648
https://github.com/pydata/xarray/blob/v2026.09.0/xarray/coding/times.py#L389-L440
https://github.com/pydata/xarray/blob/v2026.09.0/xarray/tests/test_duck_array_ops.py#L350-L361
https://gitlab.archlinux.org/archlinux/packaging/packages/python-xarray/-/blob/2026.09.0-1/PKGBUILD
https://archriscv.felixc.at/.status/logs/python-xarray/python-xarray-2026.09.0-1.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants