Skip to content

Commit 4d63d60

Browse files
Emit prominent MigrationWarning instead of breaking InferenceData import (#2575)
* Emit prominent `MigrationWarning` instead of breaking `InferenceData` imports * Subclass `FutureWarning` * Subclass `MigrationWarning(UserWarning, FutureWarning)`
1 parent 59bfd79 commit 4d63d60

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/arviz/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
info = ""
2828

2929

30-
class MigrationError(RuntimeError):
31-
"""Error raised when a legacy name is accessed on the ``arviz`` namespace."""
30+
class MigrationWarning(UserWarning, FutureWarning):
31+
"""Warning raised when a legacy name is accessed on the ``arviz`` namespace."""
3232

3333

3434
try:
@@ -104,11 +104,17 @@ class MigrationError(RuntimeError):
104104
def __getattr__(name):
105105
"""Guide users who expect legacy names on the ``arviz`` namespace."""
106106
if name == "InferenceData":
107-
raise MigrationError(
107+
import warnings
108+
from xarray import DataTree
109+
110+
warnings.warn(
108111
"arviz.InferenceData is no longer available on the "
109112
"arviz package; ArviZ now uses xarray's DataTree for the same "
110-
f"role. See the migration guide: {_MIGRATION_GUIDE_URL}"
113+
f"role. See the migration guide: {_MIGRATION_GUIDE_URL}",
114+
MigrationWarning,
111115
)
116+
117+
return DataTree
112118
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
113119

114120

tests/test_namespace.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import arviz_plots
1919
import arviz_stats
2020
import pytest
21+
from xarray import DataTree
2122

2223
import arviz as az
2324

@@ -33,10 +34,10 @@ def test_info_attr():
3334

3435
def test_aliases():
3536
# These are xarray aliases exposed for user convenience
36-
xarray_aliases = {"from_netcdf", "from_zarr"}
37+
xarray_aliases = {"from_netcdf", "from_zarr", "InferenceData"}
3738

3839
for obj_name in dir(az):
39-
if not obj_name.startswith("_") and obj_name not in ["info", "MigrationError"]:
40+
if not obj_name.startswith("_") and obj_name not in ["info", "MigrationWarning"]:
4041
obj = getattr(az, obj_name)
4142

4243
if obj_name in xarray_aliases:
@@ -88,15 +89,15 @@ def test_incompatible_package_versions(monkeypatch):
8889

8990
def test_inference_data_import_points_to_migration_guide():
9091
"""Legacy arviz.InferenceData should error with a link to the migration guide."""
91-
with pytest.raises(az.MigrationError, match="https://python.arviz.org/.*/migration_guide.*"):
92+
with pytest.warns(az.MigrationWarning, match="https://python.arviz.org/.*/migration_guide.*"):
9293
from arviz import InferenceData
9394

94-
InferenceData
95+
assert InferenceData is DataTree
9596

9697

9798
def test_inference_data_getattr_points_to_migration_guide():
9899
"""Legacy arviz.InferenceData should error with a link to the migration guide."""
99-
with pytest.raises(az.MigrationError, match="https://python.arviz.org/.*/migration_guide.*"):
100+
with pytest.warns(az.MigrationWarning, match="https://python.arviz.org/.*/migration_guide.*"):
100101
az.InferenceData
101102

102103

0 commit comments

Comments
 (0)