diff --git a/pygmt/src/text.py b/pygmt/src/text.py index e1783cebc78..2cde7480d19 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -7,7 +7,6 @@ from pygmt.helpers import ( build_arg_string, data_kind, - dummy_context, fmt_docstring, is_nonstr_iter, kwargs_to_strings, @@ -164,24 +163,27 @@ def text_( for texts, but this option is only valid if using x/y/text. {w} """ - - # pylint: disable=too-many-locals # pylint: disable=too-many-branches - kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access # Ensure inputs are either textfiles, x/y/text, or position/text if position is None: + if (x is not None or y is not None) and textfiles is not None: + raise GMTInvalidInput( + "Provide either position only, or x/y pairs, or textfiles." + ) kind = data_kind(textfiles, x, y, text) + if kind == "vectors" and text is None: + raise GMTInvalidInput("Must provide text with x/y pairs") else: - if x is not None or y is not None: + if x is not None or y is not None or textfiles is not None: raise GMTInvalidInput( - "Provide either position only, or x/y pairs, not both" + "Provide either position only, or x/y pairs, or textfiles." ) - kind = "vectors" - - if kind == "vectors" and text is None: - raise GMTInvalidInput("Must provide text with x/y pairs or position") + if text is None or is_nonstr_iter(text): + raise GMTInvalidInput("Text can't be None or array.") + kind = None + textfiles = "" # Build the -F option in gmt text. if kwargs.get("F") is None and ( @@ -219,18 +221,13 @@ def text_( extra_arrays.append(kwargs["t"]) kwargs["t"] = "" + # Append text at last column. Text must be passed in as str type. + if kind == "vectors": + extra_arrays.append(np.atleast_1d(text).astype(str)) + with Session() as lib: - file_context = dummy_context(textfiles) if kind == "file" else "" - if kind == "vectors": - if position is not None: - file_context = dummy_context("") - else: - file_context = lib.virtualfile_from_vectors( - np.atleast_1d(x), - np.atleast_1d(y), - *extra_arrays, - # text must be in str type, see issue #706 - np.atleast_1d(text).astype(str), - ) + file_context = lib.virtualfile_from_data( + check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays + ) with file_context as fname: lib.call_module(module="text", args=build_arg_string(kwargs, infile=fname)) diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index c160b8214d2..f725c768f2d 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name """ Tests text. """ @@ -15,16 +14,16 @@ CITIES_DATA = os.path.join(TEST_DATA_DIR, "cities.txt") -@pytest.fixture(scope="module") -def projection(): +@pytest.fixture(scope="module", name="projection") +def fixture_projection(): """ The projection system. """ return "x10c" -@pytest.fixture(scope="module") -def region(): +@pytest.fixture(scope="module", name="region") +def fixture_region(): """ The data region. """ @@ -124,15 +123,25 @@ def test_text_position(region): return fig -def test_text_xy_with_position_fails(region): +def test_text_invalid_inputs(region): """ - Run text by providing both x/y pairs and position arguments. + Run text by providing invalid combinations of inputs. """ fig = Figure() with pytest.raises(GMTInvalidInput): fig.text( region=region, projection="x1c", x=1.2, y=2.4, position="MC", text="text" ) + with pytest.raises(GMTInvalidInput): + fig.text(region=region, projection="x1c", textfiles="file.txt", text="text") + with pytest.raises(GMTInvalidInput): + fig.text(region=region, projection="x1c", position="MC", text=None) + with pytest.raises(GMTInvalidInput): + fig.text( + region=region, projection="x1c", position="MC", text=["text1", "text2"] + ) + with pytest.raises(GMTInvalidInput): + fig.text(region=region, projection="x1c", textfiles="file.txt", x=1.2, y=2.4) @pytest.mark.mpl_image_compare @@ -313,10 +322,8 @@ def test_text_transparency(): text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)] fig = Figure() - fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True) fig.text(x=x, y=y, text=text, transparency=50) - return fig @@ -333,7 +340,6 @@ def test_text_varying_transparency(): fig = Figure() fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True) fig.text(x=x, y=y, text=text, transparency=transparency) - return fig @@ -357,7 +363,6 @@ def test_text_nonstr_text(): Input text is in non-string type (e.g., int, float) """ fig = Figure() - fig.text( region=[0, 10, 0, 10], projection="X10c", @@ -366,5 +371,4 @@ def test_text_nonstr_text(): y=[1, 2, 3, 4], text=[1, 2, 3.0, 4.0], ) - return fig