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

Raise an exception if the given parameter is not recognized and is longer than 2 characters #1792

Merged
merged 3 commits into from
Mar 11, 2022

Conversation

seisman
Copy link
Member

@seisman seisman commented Mar 8, 2022

Description of proposed changes

Address #256.

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.

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 changed the title Raise an exception if the given parameter is not aliased and greater than 2 characters Raise an exception if the given parameter is not recognized and is longer than 2 characters Mar 8, 2022
@seisman seisman added the enhancement Improving an existing feature label Mar 8, 2022
@seisman seisman added this to the 0.6.0 milestone Mar 8, 2022
# raise an exception for unrecognized options
for key in kwargs:
if len(key) > 2:
raise GMTInvalidInput(f"Unrecognized parameter '{key}'.")
# Exclude arguments that are None and False
Copy link
Member Author

@seisman seisman Mar 8, 2022

Choose a reason for hiding this comment

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

Perhaps we can rewrite the whole function like below, so we don't need to loop over kwargs three times.

    for key in filtered_kwargs:
        if len(key) > 2:  # raise an exception for unrecognized options
            raise GMTInvalidInput(f"Unrecognized parameter '{key}'.")       
        if (kwargs[key] is None or kwargs[key] is False):
            pass  # Exclude arguments that are None and False
        elif is_nonstr_iter(kwargs[key]):
            for value in kwargs[key]:
                gmt_args.append(f"-{key}{value}")
        elif kwargs[key] is True:
            gmt_args.append(f"-{key}")
        else:
            gmt_args.append(f"-{key}{kwargs[key]}")
    return " ".join(sorted(gmt_args))

Copy link
Member

Choose a reason for hiding this comment

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

Yep, looks good. It might conflict with #1487 but we can fix it later.

@weiji14 weiji14 mentioned this pull request Mar 10, 2022
28 tasks
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

I had a last minute thought that the len(key) > 2 check should probably go in the @use_alias decorator, but I really like this refactor so let's just go with this 😝

@weiji14 weiji14 added the final review call This PR requires final review and approval from a second reviewer label Mar 10, 2022
@seisman seisman merged commit fc352ca into main Mar 11, 2022
@seisman seisman deleted the better-typo-handling branch March 11, 2022 13:53
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Mar 11, 2022
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants