Skip to content
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

Let kwargs_to_strings work with default values and positional arguments #2826

Merged
merged 9 commits into from
Dec 15, 2023

Conversation

seisman
Copy link
Member

@seisman seisman commented Nov 22, 2023

Description of proposed changes

See #2361 for the original issue report. The solution comes from https://stackoverflow.com/a/69170441.

In summary, it's possible to use the following codes to get the default values of parameters:

sig = inspect.signature(f)
bound = sig.bind(*args, **kwds)
bound.apply_defaults()

Here are some example codes to understand the behavior the the above codes, in which case 3 is usually how we call PyGMT functions/methods:

import inspect

def func(x, y, z=3, **kwargs):
    """
    An example function which 
    """
    print(f"x={x} y={y} z={z}")
    print(kwargs)

print("Case 1:")
sig = inspect.signature(func)
bound = sig.bind(1, 2)
print(bound.arguments)  # raw arguments
bound.apply_defaults()
print(bound.arguments)  # after apply_defaults

print("Case 2:")
bound = sig.bind(1, 2, 4, R=[1, 2, 3, 4], J="X10c")
bound.apply_defaults()
print(bound.arguments)

print("Case 3:")
bound = sig.bind(1, 2, z=4, R=[1, 2, 3, 4], J="X10c")
bound.apply_defaults()
print(bound.arguments)

bound.arguments["kwargs"]["R"] = "1/2/3/4"
print(bound.arguments)
print(bound.args, bound.kwargs)

func(*bound.args, **bound.kwargs)

The outputs are:

Case 1:
{'x': 1, 'y': 2}
{'x': 1, 'y': 2, 'z': 3, 'kwargs': {}}
Case 2:
{'x': 1, 'y': 2, 'z': 4, 'kwargs': {'R': [1, 2, 3, 4], 'J': 'X10c'}}
Case 3:
{'x': 1, 'y': 2, 'z': 4, 'kwargs': {'R': [1, 2, 3, 4], 'J': 'X10c'}}
{'x': 1, 'y': 2, 'z': 4, 'kwargs': {'R': '1/2/3/4', 'J': 'X10c'}}
(1, 2, 4) {'R': '1/2/3/4', 'J': 'X10c'}
x=1 y=2 z=4
{'R': '1/2/3/4', 'J': 'X10c'}

This PR will fix #2361. With changes in this PR, now we don't need to manually deal with offset in Figure.timestamp (see
#2208), panel in subplot (3edb1ee) and fname in pygmt.which (#2726).

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@seisman seisman force-pushed the kwargs-to-strings-improved branch 2 times, most recently from f6d64b7 to ac04502 Compare November 22, 2023 13:33
@seisman seisman marked this pull request as ready for review November 22, 2023 13:53
@seisman seisman added the enhancement Improving an existing feature label Nov 22, 2023
@seisman seisman added this to the 0.11.0 milestone Nov 22, 2023
@seisman seisman added the needs review This PR has higher priority and needs review. label Nov 22, 2023
pygmt/helpers/decorators.py Outdated Show resolved Hide resolved
pygmt/helpers/decorators.py Outdated Show resolved Hide resolved
pygmt/helpers/decorators.py Outdated Show resolved Hide resolved
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
@seisman seisman removed the needs review This PR has higher priority and needs review. label Nov 28, 2023
@seisman seisman marked this pull request as draft November 28, 2023 05:52
@seisman seisman marked this pull request as ready for review December 4, 2023 16:46
@seisman seisman added the needs review This PR has higher priority and needs review. label Dec 4, 2023
@seisman seisman modified the milestones: 0.11.0, 0.12.0 Dec 11, 2023
@seisman seisman removed the needs review This PR has higher priority and needs review. label Dec 11, 2023
@seisman seisman modified the milestones: 0.12.0, 0.11.0 Dec 13, 2023
@seisman seisman added the needs review This PR has higher priority and needs review. label Dec 13, 2023
@seisman seisman requested a review from a team December 14, 2023 14:02
@seisman seisman removed enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Dec 15, 2023
@seisman seisman added the bug Something isn't working label Dec 15, 2023
@seisman seisman merged commit b0a2c44 into main Dec 15, 2023
17 checks passed
@seisman seisman deleted the kwargs-to-strings-improved branch December 15, 2023 16:08
@seisman
Copy link
Member Author

seisman commented Dec 15, 2023

I've merged this PR without review and approval so that I can work on other issues that need the changes in this PR.

@yvonnefroehlich yvonnefroehlich changed the title Let kwargs_to_strings work with default values and postional arguments Let kwargs_to_strings work with default values and positional arguments Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limitations of the kwargs_to_strings decorator not handling positional arguments
2 participants