Skip to content

None shall not pass args_in_kwargs #1815

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 7 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 17 additions & 1 deletion pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,21 @@ def args_in_kwargs(args, kwargs):
--------
bool
If one of the required arguments is in ``kwargs``.

Examples
--------

>>> args_in_kwargs(args=["A", "B"], kwargs={"C": "xyz"})
False
>>> args_in_kwargs(args=["A", "B"], kwargs={"B": "af"})
True
>>> args_in_kwargs(args=["A", "B"], kwargs={"B": None})
False
>>> args_in_kwargs(args=["A", "B"], kwargs={"B": True})
True
>>> args_in_kwargs(args=["A", "B"], kwargs={"B": False})
False
>>> args_in_kwargs(args=["A", "B"], kwargs={"B": 0})
True
"""
return any(arg in kwargs for arg in args)
return any(kwargs.get(arg) for arg in args)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to modify this one-liner return statement so that args_in_kwargs(args=["A", "B"], kwargs={"B": 0}) returns True.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we should change it to:

return any(kwargs.get(arg) is not None for arg in args)

right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Close. I had to do return any(kwargs.get(arg) is not None and kwargs.get(arg) is not False for arg in args) so that arguments that are False don't count. Done in 7c3c6b4

2 changes: 1 addition & 1 deletion pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def test_figure_shift_origin():
"""
Test if fig.shift_origin works.
"""
kwargs = dict(region=[0, 3, 0, 5], projection="X3c/5c", frame=0)
kwargs = dict(region=(0, 3, 0, 5), projection="X3c/5c", frame=0)
fig = Figure()
# First call shift_origin without projection and region.
# Test issue https://github.com/GenericMappingTools/pygmt/issues/514
Expand Down