Skip to content

Commit 4681ba0

Browse files
authored
API: allow silencing of FutureWarnings from legacy API (pysal#617)
* API: allow silencing of FutureWarnings from legacy API * typo
1 parent 03dae0a commit 4681ba0

File tree

3 files changed

+84
-18
lines changed

3 files changed

+84
-18
lines changed

docs/legacy_api.rst

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Legacy API reference
88

99
.. warning::
1010
The functionality listed below is part of the legacy API and has been deprecated.
11+
Each class emits ``FutureWarning`` with a deprecation note. If you'd like to
12+
silence all of these warnigns, set an environment variable ``ALLOW_LEGACY_MOMEPY``
13+
to ``"True"``::
14+
15+
import os
16+
17+
os.environ["ALLOW_LEGACY_MOMEPY"] = "True"
1118

1219
elements
1320
--------

momepy/tests/test_utils.py

+47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import warnings
3+
14
import geopandas as gpd
25
import networkx
36
import numpy as np
@@ -215,3 +218,47 @@ def test_limit_range(self):
215218
mm.limit_range(np.array([np.nan, np.nan, np.nan]), rng=(25, 75)),
216219
np.array([np.nan, np.nan, np.nan]),
217220
)
221+
222+
def test_deprecated_decorators(self):
223+
with pytest.warns(
224+
FutureWarning,
225+
match=(
226+
"Class based API like `momepy.LongestAxisLength` is deprecated. "
227+
"Replace it with `momepy.longest_axis_length`"
228+
),
229+
):
230+
mm.LongestAxisLength(self.df_buildings)
231+
os.environ["ALLOW_LEGACY_MOMEPY"] = "True"
232+
233+
with warnings.catch_warnings():
234+
warnings.simplefilter("error")
235+
mm.LongestAxisLength(self.df_buildings)
236+
237+
os.environ["ALLOW_LEGACY_MOMEPY"] = "False"
238+
with pytest.warns(
239+
FutureWarning,
240+
match=(
241+
"Class based API like `momepy.LongestAxisLength` is deprecated. "
242+
"Replace it with `momepy.longest_axis_length`"
243+
),
244+
):
245+
mm.LongestAxisLength(self.df_buildings)
246+
247+
def test_removed_decorators(self):
248+
with pytest.warns(
249+
FutureWarning,
250+
match=("`momepy.Area` is deprecated"),
251+
):
252+
mm.Area(self.df_buildings)
253+
os.environ["ALLOW_LEGACY_MOMEPY"] = "True"
254+
255+
with warnings.catch_warnings():
256+
warnings.simplefilter("error")
257+
mm.Area(self.df_buildings)
258+
259+
os.environ["ALLOW_LEGACY_MOMEPY"] = "False"
260+
with pytest.warns(
261+
FutureWarning,
262+
match=("`momepy.Area` is deprecated"),
263+
):
264+
mm.Area(self.df_buildings)

momepy/utils.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
2-
32
import math
3+
import os
44
import warnings
55

66
import geopandas as gpd
@@ -28,15 +28,21 @@ def decorator(func1):
2828

2929
@functools.wraps(func1)
3030
def new_func1(*args, **kwargs):
31-
warnings.warn(
32-
f"Class based API like `momepy.{func1.__name__}` is deprecated. "
33-
f"Replace it with `momepy.{new_way}` to use functional API instead "
34-
"or pin momepy version <1.0. Class-based API will be removed in 1.0. "
35-
# "See details at https://docs.momepy.org/en/stable/migration.html",
36-
"",
37-
FutureWarning,
38-
stacklevel=2,
39-
)
31+
if os.getenv("ALLOW_LEGACY_MOMEPY", "False").lower() not in (
32+
"true",
33+
"1",
34+
"yes",
35+
):
36+
warnings.warn(
37+
f"Class based API like `momepy.{func1.__name__}` is deprecated. "
38+
f"Replace it with `momepy.{new_way}` to use functional API instead "
39+
"or pin momepy version <1.0. Class-based API will be removed in "
40+
"1.0. "
41+
# "See details at https://docs.momepy.org/en/stable/migration.html",
42+
"",
43+
FutureWarning,
44+
stacklevel=2,
45+
)
4046
return func1(*args, **kwargs)
4147

4248
return new_func1
@@ -54,14 +60,20 @@ def decorator(func1):
5460

5561
@functools.wraps(func1)
5662
def new_func1(*args, **kwargs):
57-
warnings.warn(
58-
f"`momepy.{func1.__name__}` is deprecated. Replace it with {new_way} "
59-
"or pin momepy version <1.0. This class will be removed in 1.0. "
60-
# "See details at https://docs.momepy.org/en/stable/migration.html"
61-
"",
62-
FutureWarning,
63-
stacklevel=2,
64-
)
63+
if os.getenv("ALLOW_LEGACY_MOMEPY", "False").lower() not in (
64+
"true",
65+
"1",
66+
"yes",
67+
):
68+
warnings.warn(
69+
f"`momepy.{func1.__name__}` is deprecated. Replace it with "
70+
f"{new_way} "
71+
"or pin momepy version <1.0. This class will be removed in 1.0. "
72+
# "See details at https://docs.momepy.org/en/stable/migration.html"
73+
"",
74+
FutureWarning,
75+
stacklevel=2,
76+
)
6577
return func1(*args, **kwargs)
6678

6779
return new_func1

0 commit comments

Comments
 (0)