Skip to content

Commit 35466a2

Browse files
committed
Add the deprecate_parameter decorator to deprecate parameters
1 parent 3414f69 commit 35466a2

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

pygmt/helpers/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
"""
22
Functions, classes, decorators, and context managers to help wrap GMT modules.
33
"""
4-
from pygmt.helpers.decorators import fmt_docstring, kwargs_to_strings, use_alias
4+
from pygmt.helpers.decorators import (
5+
deprecate_parameter,
6+
fmt_docstring,
7+
kwargs_to_strings,
8+
use_alias,
9+
)
510
from pygmt.helpers.tempfile import GMTTempFile, unique_name
611
from pygmt.helpers.utils import (
712
args_in_kwargs,

pygmt/helpers/decorators.py

+63
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import functools
99
import textwrap
10+
import warnings
1011

1112
import numpy as np
1213
from pygmt.exceptions import GMTInvalidInput
@@ -469,3 +470,65 @@ def remove_bools(kwargs):
469470
else:
470471
new_kwargs[arg] = value
471472
return new_kwargs
473+
474+
475+
def deprecate_parameter(oldname, newname, deprecate_version, remove_version):
476+
"""
477+
Decorator to deprecate a parameter.
478+
479+
Parameters
480+
----------
481+
oldname : str
482+
The old, deprecated parameter name.
483+
newname : str
484+
The new parameter name.
485+
deprecate_version : str
486+
The PyGMT version when the old parameter starts to be deprecated.
487+
remove_version : str
488+
The PyGMT version when the old parameter will be fully removed.
489+
490+
Examples
491+
--------
492+
493+
>>> @deprecate_parameter("sizes", "size", "v0.0.0", "v9.9.9")
494+
... @deprecate_parameter("colors", "color", "v0.0.0", "v9.9.9")
495+
... def module(size=0, **kwargs):
496+
... "A module that prints the arguments it received"
497+
... print(f"size={size}, color={kwargs['color']}")
498+
>>> # new names are supported
499+
>>> module(size=5.0, color="red")
500+
size=5.0, color=red
501+
>>> # old names are supported
502+
>>> module(sizes=5.0, colors="red")
503+
size=5.0, color=red
504+
"""
505+
506+
def deprecator(module_func):
507+
"""
508+
The decorator that creates our new function to work with both old and
509+
new parameters.
510+
"""
511+
512+
@functools.wraps(module_func)
513+
def new_module(*args, **kwargs):
514+
"""
515+
New module instance that converts old parameters to new parameters.
516+
"""
517+
if oldname in kwargs:
518+
if newname in kwargs:
519+
raise GMTInvalidInput(
520+
f"Can't provide both '{newname}' and '{oldname}'"
521+
)
522+
msg = (
523+
f"'{oldname}' is deprecated since {deprecate_version} and will"
524+
f" be removed in {remove_version}; use '{newname}' instead."
525+
)
526+
warnings.simplefilter("always", DeprecationWarning) # turn off filter
527+
warnings.warn(msg, DeprecationWarning, 2)
528+
warnings.simplefilter("default", DeprecationWarning) # reset filter
529+
kwargs[newname] = kwargs.pop(oldname)
530+
return module_func(*args, **kwargs)
531+
532+
return new_module
533+
534+
return deprecator

pygmt/src/plot.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pygmt.helpers import (
77
build_arg_string,
88
data_kind,
9+
deprecate_parameter,
910
fmt_docstring,
1011
is_nonstr_iter,
1112
kwargs_to_strings,
@@ -43,7 +44,8 @@
4344
t="transparency",
4445
)
4546
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
46-
def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
47+
@deprecate_parameter("sizes", "size", "v0.4.0", "v0.6.0")
48+
def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs):
4749
r"""
4850
Plot lines, polygons, and symbols in 2-D.
4951
@@ -78,7 +80,7 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
7880
Either a data file name or a 2d numpy array with the tabular data.
7981
Use parameter ``columns`` to choose which columns are x, y, color,
8082
and size, respectively.
81-
sizes : 1d array
83+
size : 1d array
8284
The sizes of the data points in units specified using ``style``.
8385
Only valid if using ``x``/``y``.
8486
direction : list of two 1d arrays
@@ -215,12 +217,12 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
215217
)
216218
extra_arrays.append(kwargs["G"])
217219
del kwargs["G"]
218-
if sizes is not None:
220+
if size is not None:
219221
if kind != "vectors":
220222
raise GMTInvalidInput(
221-
"Can't use arrays for sizes if data is matrix or file."
223+
"Can't use arrays for size if data is matrix or file."
222224
)
223-
extra_arrays.append(sizes)
225+
extra_arrays.append(size)
224226

225227
for flag in ["I", "t"]:
226228
if flag in kwargs and is_nonstr_iter(kwargs[flag]):

0 commit comments

Comments
 (0)