Skip to content

Commit dee5b64

Browse files
Emit prominent MigrationWarning instead of breaking InferenceData imports
1 parent 59bfd79 commit dee5b64

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/arviz/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
import functools
1818
import logging
1919
import re
20+
import warnings
2021

21-
from xarray import open_datatree as from_netcdf
22+
from xarray import open_datatree as from_netcdf, DataTree
2223

2324
from_zarr = functools.partial(from_netcdf, engine="zarr")
2425

@@ -27,8 +28,8 @@
2728
info = ""
2829

2930

30-
class MigrationError(RuntimeError):
31-
"""Error raised when a legacy name is accessed on the ``arviz`` namespace."""
31+
class MigrationWarning(DeprecationWarning):
32+
"""Warning raised when a legacy name is accessed on the ``arviz`` namespace."""
3233

3334

3435
try:
@@ -104,11 +105,13 @@ class MigrationError(RuntimeError):
104105
def __getattr__(name):
105106
"""Guide users who expect legacy names on the ``arviz`` namespace."""
106107
if name == "InferenceData":
107-
raise MigrationError(
108+
warnings.warn(
108109
"arviz.InferenceData is no longer available on the "
109110
"arviz package; ArviZ now uses xarray's DataTree for the same "
110-
f"role. See the migration guide: {_MIGRATION_GUIDE_URL}"
111+
f"role. See the migration guide: {_MIGRATION_GUIDE_URL}",
112+
MigrationWarning,
111113
)
114+
return DataTree
112115
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
113116

114117

tests/test_namespace.py

Lines changed: 4 additions & 3 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

@@ -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.InferenceData, match="https://python.arviz.org/.*/migration_guide.*"):
100101
az.InferenceData
101102

102103

0 commit comments

Comments
 (0)