Skip to content

Let kwargs_to_strings work with default values and positional arguments - #2826

Merged
seisman merged 9 commits into
mainfrom
kwargs-to-strings-improved
Dec 15, 2023
Merged

Let kwargs_to_strings work with default values and positional arguments#2826
seisman merged 9 commits into
mainfrom
kwargs-to-strings-improved

Conversation

@seisman

@seismanseisman commented Nov 22, 2023

Copy link
Copy Markdown
Member

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:

importinspectdeffunc(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 argumentsbound.apply_defaults()
print(bound.arguments) # after apply_defaultsprint("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
seismanforce-pushed the kwargs-to-strings-improved branch 2 times, most recently from f6d64b7 to ac04502CompareNovember 22, 2023 13:33
@seisman
seismanforce-pushed the kwargs-to-strings-improved branch from ac04502 to ea046d2CompareNovember 22, 2023 13:47
@seisman
seisman marked this pull request as ready for review November 22, 2023 13:53
@seismanseisman added the enhancement Improving an existing feature label Nov 22, 2023
@seismanseisman added this to the 0.11.0 milestone Nov 22, 2023
@seismanseisman added the needs review This PR has higher priority and needs review. label Nov 22, 2023
Comment threadpygmt/helpers/decorators.py Outdated
Comment threadpygmt/helpers/decorators.py Outdated
Comment threadpygmt/helpers/decorators.py Outdated
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
@seismanseisman 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
@seismanseisman added the needs review This PR has higher priority and needs review. label Dec 4, 2023
@seismanseisman modified the milestones: 0.11.0, 0.12.0Dec 11, 2023
@seismanseisman removed the needs review This PR has higher priority and needs review. label Dec 11, 2023
@seismanseisman modified the milestones: 0.12.0, 0.11.0Dec 13, 2023
@seismanseisman added the needs review This PR has higher priority and needs review. label Dec 13, 2023
@seisman
seisman requested a review from a teamDecember 14, 2023 14:02
@seismanseisman removed enhancement Improving an existing feature needs review This PR has higher priority and needs review. labels Dec 15, 2023
@seismanseisman added the bug Something isn't working label Dec 15, 2023
@seisman
seisman merged commit b0a2c44 into mainDec 15, 2023
@seisman
seisman deleted the kwargs-to-strings-improved branch December 15, 2023 16:08
@seisman

Copy link
Copy Markdown
MemberAuthor

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

@yvonnefroehlichyvonnefroehlich changed the title Let kwargs_to_strings work with default values and postional argumentsLet kwargs_to_strings work with default values and positional argumentsJan 28, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugSomething 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

@seisman@michaelgrund