diff --git a/pygmt/_typing.py b/pygmt/_typing.py new file mode 100644 index 00000000000..bbc7d596c65 --- /dev/null +++ b/pygmt/_typing.py @@ -0,0 +1,8 @@ +""" +Type aliases for type hints. +""" + +from typing import Literal + +# Anchor codes +AnchorCode = Literal["TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"] diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 74e2655dc67..ee3410bfe9a 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -11,7 +11,7 @@ import sys import time import webbrowser -from collections.abc import Iterable, Sequence +from collections.abc import Iterable, Mapping, Sequence from typing import Any, Literal import numpy as np @@ -406,7 +406,7 @@ def non_ascii_to_octal( def build_arg_list( # noqa: PLR0912 kwdict: dict[str, Any], - confdict: dict[str, str] | None = None, + confdict: Mapping[str, Any] | None = None, infile: str | pathlib.PurePath | Sequence[str | pathlib.PurePath] | None = None, outfile: str | pathlib.PurePath | None = None, ) -> list[str]: diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 9f0f80ccde1..24835fc1881 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -2,7 +2,10 @@ text - Plot text on a figure. """ +from collections.abc import Sequence + import numpy as np +from pygmt._typing import AnchorCode from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( @@ -44,11 +47,11 @@ def text_( # noqa: PLR0912 textfiles=None, x=None, y=None, - position=None, + position: AnchorCode | None = None, text=None, angle=None, font=None, - justify=None, + justify: bool | None | AnchorCode | Sequence[AnchorCode] = None, **kwargs, ): r""" @@ -90,14 +93,14 @@ def text_( # noqa: PLR0912 x/y : float or 1-D arrays The x and y coordinates, or an array of x and y coordinates to plot the text. - position : str + position Set reference point on the map for the text by using x, y coordinates extracted from ``region`` instead of providing them through ``x``/``y``. Specify with a two-letter (order independent) code, chosen from: - * Horizontal: **L**\ (eft), **C**\ (entre), **R**\ (ight) * Vertical: **T**\ (op), **M**\ (iddle), **B**\ (ottom) + * Horizontal: **L**\ (eft), **C**\ (entre), **R**\ (ight) For example, ``position="TL"`` plots the text at the Top Left corner of the map. @@ -116,7 +119,7 @@ def text_( # noqa: PLR0912 font. If no font info is explicitly given (i.e. ``font=True``), then the input to ``textfiles`` must have this information in one of its columns. - justify : str, bool or list of str + justify Set the alignment which refers to the part of the text string that will be mapped onto the (x, y) point. Choose a two-letter combination of **L**, **C**, **R** (for left, center, or right) and diff --git a/pygmt/src/timestamp.py b/pygmt/src/timestamp.py index 9818a289974..3db9ff694d9 100644 --- a/pygmt/src/timestamp.py +++ b/pygmt/src/timestamp.py @@ -6,6 +6,7 @@ from collections.abc import Sequence from packaging.version import Version +from pygmt._typing import AnchorCode from pygmt.clib import Session, __gmt_version__ from pygmt.helpers import build_arg_list, kwargs_to_strings @@ -17,7 +18,7 @@ def timestamp( self, text: str | None = None, label: str | None = None, - justify: str = "BL", + justify: AnchorCode = "BL", offset: float | str | Sequence[float | str] = ("-54p", "-54p"), font: str = "Helvetica,black", timefmt: str = "%Y %b %d %H:%M:%S",