Skip to content

Add a decorator to raise a FutureWarning when positional arguments are passed for functions that changed parameter orders #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pygmt/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Functions, classes, decorators, and context managers to help wrap GMT modules.
"""
from pygmt.helpers.decorators import (
check_data_input_order,
deprecate_parameter,
fmt_docstring,
kwargs_to_strings,
Expand Down
60 changes: 60 additions & 0 deletions pygmt/helpers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,63 @@ def new_module(*args, **kwargs):
return new_module

return deprecator


def check_data_input_order(deprecate_version, remove_version):
"""
Decorator to raise a FutureWarning if the order of data input parameters
changes and positional arguments are passed.

The decorator is temporary and should be removed in v0.7.0.

Parameters
----------
deprecate_version : str
The PyGMT version when the order of data input parameters is changed.
remove_version : str
The PyGMT version when the deprecation warning should be removed.

Examples
--------
>>> @check_data_input_order("v0.0.0", "v9.9.9")
... def module(data=None, x=None, y=None, z=None, **kwargs):
... "A module that prints the arguments it received"
... print(f"data={data}, x={x}, y={y}, z={z}")
>>> module(data="table.txt")
data=table.txt, x=None, y=None, z=None
>>> module(x=0, y=1, z=2)
data=None, x=0, y=1, z=2
>>> with warnings.catch_warnings(record=True) as w:
... module(0, 1, 2)
... assert len(w) == 1
... assert issubclass(w[0].category, FutureWarning)
...
data=0, x=1, y=2, z=None
"""

def data_input_order_checker(module_func):
"""
The decorator that creates the new function to check if positional
arguments are passed.
"""

@functools.wraps(module_func)
def new_module(*args, **kwargs):
"""
New module instance that raises a warning if positional arguments
are passed.
"""
if len(args) > 0: # positional arguments are used
msg = (
"The function parameters has been re-ordered as 'data, x, y, [z]' "
f"since {deprecate_version} but you're passing positional arguments. "
"You can silence the warning by passing keyword arguments "
"like 'x=x, y=y, z=z'. Otherwise, the warning will be removed "
f"in {remove_version}."
)
warnings.warn(msg, category=FutureWarning, stacklevel=2)
return module_func(*args, **kwargs)

return new_module

return data_input_order_checker