Skip to content

Commit 94c659a

Browse files
committed
fix: xarray>=2025.03 compatibility
1 parent e14e680 commit 94c659a

File tree

6 files changed

+94
-96
lines changed

6 files changed

+94
-96
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
- `fill` and `fill_structures` argument in `td.Simulation.plot_structures()` and `td.Simulation.plot()` respectively to disable fill and plot outlines of structures only.
12+
### Fixed
13+
- Compatibility with `xarray>=2025.03`.
1214

1315
## [2.8.1] - 2025-03-20
1416

poetry.lock

Lines changed: 25 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ documentation = "https://docs.flexcompute.com/projects/tidy3d/en/latest/"
2424
[tool.poetry.dependencies]
2525
python = ">=3.9,<3.14"
2626
pyroots = ">=0.5.0"
27-
xarray = ">=2023.08,<2025.03"
27+
xarray = ">=2023.08"
2828
importlib-metadata = ">=6.0.0"
2929
h5netcdf = "1.0.2"
3030
h5py = "^3.0.0"

tests/test_package/test_compat.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# test_compat.py
2+
3+
import importlib
4+
import sys
5+
6+
import pytest
7+
8+
9+
@pytest.fixture
10+
def restore_modules_and_meta_path():
11+
"""Fixture to restore sys.modules and sys.meta_path after test."""
12+
original_modules = dict(sys.modules)
13+
original_meta_path = list(sys.meta_path)
14+
try:
15+
yield
16+
finally:
17+
sys.modules.clear()
18+
sys.modules.update(original_modules)
19+
sys.meta_path[:] = original_meta_path
20+
21+
22+
def test_alignment_import(restore_modules_and_meta_path):
23+
"""Test normal import of alignment from xarray."""
24+
# remove modules to ensure clean reload
25+
for modname in ("tidy3d.compat", "xarray.structure.alignment", "xarray.core.alignment"):
26+
if modname in sys.modules:
27+
del sys.modules[modname]
28+
29+
import tidy3d.compat
30+
31+
assert tidy3d.compat.alignment is not None, "Failed to import alignment normally."
32+
33+
34+
def test_compat_import_fallback(restore_modules_and_meta_path):
35+
"""Test fallback to xarray.core.alignment when structure.alignment fails."""
36+
37+
# mock to simulate ImportError for xarray.structure.alignment
38+
class MockImporter:
39+
def find_spec(self, fullname, path=None, target=None):
40+
if fullname == "xarray.structure.alignment":
41+
raise ImportError
42+
return None
43+
44+
sys.meta_path.insert(0, MockImporter())
45+
46+
# memove modules to force re-import
47+
for modname in ("tidy3d.compat", "xarray.structure.alignment", "xarray.core.alignment"):
48+
if modname in sys.modules:
49+
del sys.modules[modname]
50+
51+
# import should trigger fallback mechanism
52+
import tidy3d.compat
53+
54+
importlib.reload(tidy3d.compat)
55+
56+
assert tidy3d.compat.alignment is not None, "Expected fallback alignment to be imported."

tidy3d/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Compatibility layer for handling differences between package versions."""
2+
3+
try:
4+
from xarray.structure import alignment
5+
except ImportError:
6+
from xarray.core import alignment
7+
8+
__all__ = ["alignment"]

tidy3d/components/data/data_array.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import numpy as np
1111
import xarray as xr
1212
from autograd.tracer import isbox
13-
from xarray.core import alignment, missing
13+
from xarray.core import missing
1414
from xarray.core.indexes import PandasIndex
1515
from xarray.core.indexing import _outer_to_numpy_indexer
1616
from xarray.core.types import InterpOptions, Self
1717
from xarray.core.utils import OrderedSet, either_dict_or_kwargs
1818
from xarray.core.variable import as_variable
1919

20+
from ...compat import alignment
2021
from ...constants import (
2122
HERTZ,
2223
MICROMETER,

0 commit comments

Comments
 (0)