Skip to content

pygmt.which: Fix the bug when passing multiple files - #2726

Merged
seisman merged 3 commits into
mainfrom
which
Oct 15, 2023
Merged

pygmt.which: Fix the bug when passing multiple files#2726
seisman merged 3 commits into
mainfrom
which

Conversation

@seisman

@seismanseisman commented Oct 9, 2023

Copy link
Copy Markdown
Member

Description of proposed changes

Issue first reported in #2361 (comment). See that issue for context.

To reproduce the issue:

>>>frompygmtimportwhich>>># doesn't work>>>which(["@hotspots.txt", "@earth_relief_01d_g"])
gmtwhich [ERROR]: File ['@hotspots.txt', notfound!
gmtwhich [ERROR]: File'@earth_relief_01d_g'] notfound!
>>># works>>>which(fname=["@hotspots.txt", "@earth_relief_01d_g"])

Address #2361.

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

@seismanseisman added the bug Something isn't working label Oct 9, 2023
@seismanseisman added this to the 0.11.0 milestone Oct 9, 2023
"""
filenames = ["ridge.txt", "tut_ship.xyz"]
cached_files = which(fname=[f"@{fname}" for fname in filenames], download="c")
cached_files = which([f"@{fname}" for fname in filenames], download="c")

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Now we can pass a list of files without having to use fname=

@seismanseisman added the needs review This PR has higher priority and needs review. label Oct 9, 2023
@seisman
seisman requested a review from a teamOctober 12, 2023 01:54
Comment threadpygmt/tests/test_which.py Outdated
Comment threadpygmt/src/which.py
@fmt_docstring
@use_alias(G="download", V="verbose")
@kwargs_to_strings(fname="sequence_space")
def which(fname, **kwargs):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reading up on https://peps.python.org/pep-0570/#positional-or-keyword-arguments and https://stackoverflow.com/questions/9450656/positional-argument-vs-keyword-argument (see also Max's previous comment at #731 (comment)), should we explicitly mark fname as positional_or_keyword like this (unsure if syntax is ok)?

Suggested change
defwhich(fname, **kwargs):
defwhich(/, fname, *, **kwargs):

Or just stick with the current one (which works also I think).

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Better to open a separate issue so that we can have more discussions about the behavior and also have consistent function definitions in the whole project.

@weiji14weiji14Oct 15, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On second thought, maybe we should enforce using keyword-only arguments like so:

Suggested change
defwhich(fname, **kwargs):
defwhich(*, fname, **kwargs):

Then the gmtwhich [ERROR] you mentioned at #2361 (comment) would turn into a slightly more useful error:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 cached_files = which([f"@{fname}" for fname in filenames], download="c")
File ~/pygmt/pygmt/helpers/decorators.py:598, in use_alias.<locals>.alias_decorator.<locals>.new_module(*args, **kwargs)
591 msg = (
592 "Parameters 'Y' and 'yshift' are deprecated since v0.8.0. "
593 "and will be removed in v0.12.0. "
594 "Use Figure.shift_origin(yshift=...) instead."
595 )
596 warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
--> 598 return module_func(*args, **kwargs)
TypeError: which() takes 0 positional arguments but 1 was given

Well, actually not that useful (it doesn't say that the fname keyword should be used. But something to consider - if we want to allow 1) positional_or_keyword (current), or 2) only keyword.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Instead, I prefer to def which(fname, *, **kwargs). It allows which("file.txt") which is much simpler than which(fname="file.txt").

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, let's go with that. I'll approve this PR, but you can apply the change afterwards.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

def which(fname, *, **kwargs)

gives a syntax error:

'named arguments must follow bare *

See https://stackoverflow.com/questions/14301967/what-does-a-bare-asterisk-do-in-a-parameter-list-what-are-keyword-only-parame.

I'll leave the function definition as it is now.

seismanand others added 2 commits October 15, 2023 16:13
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Comment threadpygmt/src/which.py
@fmt_docstring
@use_alias(G="download", V="verbose")
@kwargs_to_strings(fname="sequence_space")
def which(fname, **kwargs):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, let's go with that. I'll approve this PR, but you can apply the change afterwards.

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.

2 participants

@seisman@weiji14