-
Notifications
You must be signed in to change notification settings - Fork 229
pygmt.which: Fix the bug when passing multiple files #2726
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
""" | ||
Test pygmt.which. | ||
""" | ||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
from pygmt import which | ||
|
@@ -13,20 +13,20 @@ def test_which(): | |
Make sure `which` returns file paths for @files correctly without errors. | ||
""" | ||
for fname in ["tut_quakes.ngdc", "tut_bathy.nc"]: | ||
cached_file = which(f"@{fname}", download="c") | ||
assert os.path.exists(cached_file) | ||
assert os.path.basename(cached_file) == fname | ||
cached_file = which(fname=f"@{fname}", download="c") | ||
assert Path(cached_file).exists() | ||
assert Path(cached_file).name == fname | ||
|
||
|
||
def test_which_multiple(): | ||
""" | ||
Make sure `which` returns file paths for multiple @files correctly. | ||
""" | ||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we can pass a list of files without having to use |
||
for cached_file in cached_files: | ||
assert os.path.exists(cached_file) | ||
assert os.path.basename(cached_file) in filenames | ||
assert Path(cached_file).exists() | ||
assert Path(cached_file).name in filenames | ||
|
||
|
||
def test_which_fails(): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?Or just stick with the current one (which works also I think).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
Then the
gmtwhich [ERROR]
you mentioned at #2361 (comment) would turn into a slightly more useful error: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.There was a problem hiding this comment.
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 allowswhich("file.txt")
which is much simpler thanwhich(fname="file.txt")
.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gives a syntax error:
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.