GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
749 stars 217 forks source link

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

Closed seisman closed 2 weeks ago

seisman commented 2 weeks ago

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:

    with Session() as lib:
        if spec is None:
            specfile = ""
        elif data_kind(spec) == "file" and not is_nonstr_iter(spec):
            # Is a file but not a list of files
            specfile = spec
        else:
            raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}")
        lib.call_module(module="legend", args=build_arg_list(kwargs, infile=specfile))

to

    kind = data_kind(spec)
    if kind not in {"vectors", "file"}:  # kind="vectors" means spec is None
        raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}")
    if kind == "file" and is_nonstr_iter(spec):
        raise GMTInvalidInput("Only one legend specification file is allowed.")

    with Session() as lib:
        lib.call_module(module="legend", args=build_arg_list(kwargs, infile=spec))

The pros are: