Skip to content

Figure.legend: Refactor to simplify the logic of checking legend specification - #3437

Merged
seisman merged 3 commits into
mainfrom
refactor/legend
Sep 12, 2024
Merged

Figure.legend: Refactor to simplify the logic of checking legend specification#3437
seisman merged 3 commits into
mainfrom
refactor/legend

Conversation

@seisman

Copy link
Copy Markdown
Member

Description of proposed changes

This PR contains a subset of changes in PR #3326. I open this PR so that PR #3326 can be smaller and easier for review.

This PR refactors the following codes:

withSession() aslib:
ifspecisNone:
specfile=""elifdata_kind(spec) =="file"andnotis_nonstr_iter(spec):
# Is a file but not a list of filesspecfile=specelse:
raiseGMTInvalidInput(f"Unrecognized data type: {type(spec)}")
lib.call_module(module="legend", args=build_arg_list(kwargs, infile=specfile))

to

kind=data_kind(spec)
ifkindnotin {"vectors", "file"}: # kind="vectors" means spec is NoneraiseGMTInvalidInput(f"Unrecognized data type: {type(spec)}")
ifkind=="file"andis_nonstr_iter(spec):
raiseGMTInvalidInput("Only one legend specification file is allowed.")
withSession() aslib:
lib.call_module(module="legend", args=build_arg_list(kwargs, infile=spec))

The pros are:

  • Raise exceptions if any before entering a session
  • Better error message if multiple legend files are given

@seismanseisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Sep 11, 2024
@seismanseisman added this to the 0.14.0 milestone Sep 11, 2024
Comment threadpygmt/src/legend.py
kwargs["F"] = box

kind = data_kind(spec)
if kind not in {"vectors", "file"}: # kind="vectors" means spec is None

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.

This line can also be written as:

if spec is not None or kind != "file"

It's written in the current way so that this line can be extended to check "stringio" kind with minor changes (xref: #3326).

It's not straightforward to see why kind="vectors" means spec is None. That's also why I want to refactor the data_kind function in #3351.

@seisman
seisman requested a review from a teamSeptember 12, 2024 02:11
Comment threadpygmt/src/legend.py Outdated
@michaelgrundmichaelgrund added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Sep 12, 2024
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
@seismanseisman removed the final review call This PR requires final review and approval from a second reviewer label Sep 12, 2024
@seisman
seisman merged commit 98e2f6a into mainSep 12, 2024
@seisman
seisman deleted the refactor/legend branch September 12, 2024 16:00
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenanceBoring but important stuff for the core devsskip-changelogSkip adding Pull Request to changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@seisman@michaelgrund