Skip to content

Commit 889a6e7

Browse files
authored
Rename deprecation (#142)
* module renaming deprecation * changelog * add version info
1 parent e6418dd commit 889a6e7

File tree

9 files changed

+160
-1
lines changed

9 files changed

+160
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
| seaborn | 0.12 | 0.13 |
1616
| xarray | 2022.12| 2023.9 |
1717

18-
- The modules ``cartopy_utils``, ``colormaps``, ``map_layout``, ``mpl``, and ``xrcompat`` were renamed (added a leading underscore) to indicate that they are private ([#141](https://github.com/mathause/mplotutils/pull/141)).
18+
- The modules ``cartopy_utils``, ``colormaps``, ``map_layout``, ``mpl``, and ``xrcompat``
19+
were renamed (added a leading underscore) to indicate that they are private
20+
([#141](https://github.com/mathause/mplotutils/pull/141) and [#142](https://github.com/mathause/mplotutils/pull/142)).
1921

2022
### Enhancements
2123

mplotutils/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from mplotutils._colorbar import colorbar
1616
from mplotutils._colormaps import from_levels_and_cmap
17+
from mplotutils._deprecate import _module_renamed_warning_init
1718
from mplotutils._hatch import hatch, hatch_map, hatch_map_global
1819
from mplotutils._map_layout import set_map_layout
1920
from mplotutils._mpl import _get_renderer
@@ -51,3 +52,29 @@
5152
# Local copy or not installed with setuptools.
5253
# Disable minimum version checks on downstream libraries.
5354
__version__ = "999"
55+
56+
57+
def __getattr__(attr):
58+
59+
m = (
60+
"cartopy_utils",
61+
"colormaps",
62+
"map_layout",
63+
"mpl",
64+
"xrcompat",
65+
)
66+
67+
import mplotutils
68+
69+
if attr in m:
70+
71+
_module_renamed_warning_init(attr)
72+
73+
# NOTE: could use importlib.import_module() but it registers the function in
74+
# sys.modules such that the warning is only called once
75+
# return importlib.import_module(f".{attr}", "mplotutils")
76+
77+
return getattr(mplotutils, f"_{attr}")
78+
79+
# required for ipython tab completion
80+
raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")

mplotutils/_deprecate.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,31 @@ def inner(*args, **kwargs):
107107
return inner
108108

109109
return _decorator
110+
111+
112+
def _module_renamed_warning_init(attr):
113+
114+
old_module = f"mplotutils.{attr}"
115+
new_module = f"mplotutils._{attr}"
116+
117+
msg = (
118+
f"``{old_module}`` is deprecated and has been renamed to ``{new_module}`` in v0.6.0.."
119+
f" Note that importing ``{new_module}`` is discuraged. Please use"
120+
f" functions directly from the main namespace."
121+
)
122+
123+
warnings.warn(msg, FutureWarning, stacklevel=2)
124+
125+
126+
def _module_renamed_warning(attr, submodule):
127+
128+
old_module = f"mplotutils.{submodule}"
129+
new_module = f"mplotutils._{submodule}"
130+
131+
warnings.warn(
132+
f"``{old_module}`` is deprecated and has been renamed to ``{new_module}`` in v0.6.0."
133+
f" Note that importing from ``{new_module}`` is discuraged. Please use"
134+
f" functions directly from the main namespace, i.e., ``mplotutils.{attr}``",
135+
FutureWarning,
136+
stacklevel=3,
137+
)

mplotutils/cartopy_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mplotutils import _cartopy_utils
2+
from mplotutils._deprecate import _module_renamed_warning
3+
4+
5+
def __getattr__(attr_name):
6+
attr = getattr(_cartopy_utils, attr_name)
7+
_module_renamed_warning(attr_name, "cartopy_utils")
8+
return attr

mplotutils/colormaps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mplotutils import _colormaps
2+
from mplotutils._deprecate import _module_renamed_warning
3+
4+
5+
def __getattr__(attr_name):
6+
attr = getattr(_colormaps, attr_name)
7+
_module_renamed_warning(attr_name, "colormaps")
8+
return attr

mplotutils/map_layout.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mplotutils import _map_layout
2+
from mplotutils._deprecate import _module_renamed_warning
3+
4+
5+
def __getattr__(attr_name):
6+
attr = getattr(_map_layout, attr_name)
7+
_module_renamed_warning(attr_name, "map_layout")
8+
return attr

mplotutils/mpl.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mplotutils import _mpl
2+
from mplotutils._deprecate import _module_renamed_warning
3+
4+
5+
def __getattr__(attr_name):
6+
attr = getattr(_mpl, attr_name)
7+
_module_renamed_warning(attr_name, "mpl")
8+
return attr
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import importlib
2+
3+
import pytest
4+
5+
import mplotutils as mpu
6+
7+
DEPRECATED_MODULES = {
8+
"cartopy_utils": (
9+
"cyclic_dataarray",
10+
"sample_data_map",
11+
"sample_dataarray",
12+
"xlabel_map",
13+
"xticklabels",
14+
"ylabel_map",
15+
"yticklabels",
16+
),
17+
"colormaps": ("from_levels_and_cmap",),
18+
"map_layout": ("set_map_layout",),
19+
"mpl": ("_get_renderer",),
20+
"xrcompat": ("infer_interval_breaks",),
21+
}
22+
23+
24+
def test_00_deprecated_not_in_dir():
25+
26+
dir = mpu.__dir__()
27+
28+
for module in DEPRECATED_MODULES:
29+
assert module not in dir
30+
31+
32+
def test_01_in_dir():
33+
34+
dir = mpu.__dir__()
35+
36+
assert "hatch" in dir
37+
38+
39+
@pytest.mark.parametrize("mod", DEPRECATED_MODULES)
40+
def test_01_deprecated_modules_from_import(mod):
41+
42+
with pytest.warns(FutureWarning, match=f"``mplotutils.{mod}`` is deprecated"):
43+
importlib.__import__("mplotutils", fromlist=[mod])
44+
45+
46+
@pytest.mark.parametrize("mod, functions", DEPRECATED_MODULES.items())
47+
def test_depm3(mod, functions):
48+
49+
module = importlib.import_module(f"mplotutils.{mod}")
50+
51+
for function in functions:
52+
with pytest.warns(FutureWarning, match=f"``mplotutils.{mod}`` is deprecated"):
53+
getattr(module, function)
54+
55+
56+
def test_fcn_warns():
57+
58+
# NOTE: this is the only import that does not warn
59+
import mplotutils.cartopy_utils
60+
61+
with pytest.warns(FutureWarning):
62+
mplotutils.cartopy_utils.sample_data_map(6, 6)

mplotutils/xrcompat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from mplotutils import _xrcompat
2+
from mplotutils._deprecate import _module_renamed_warning
3+
4+
5+
def __getattr__(attr_name):
6+
attr = getattr(_xrcompat, attr_name)
7+
_module_renamed_warning(attr_name, "xrcompat")
8+
return attr

0 commit comments

Comments
 (0)