From 976441b854379c1a3f7d536116ccad38a588ae7d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 5 Aug 2024 15:53:41 +0800 Subject: [PATCH 1/3] Add type hints for GMT anchor codes --- pygmt/_typing.py | 8 ++++++++ pygmt/src/text.py | 11 +++++++---- pygmt/src/timestamp.py | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 pygmt/_typing.py 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/src/text.py b/pygmt/src/text.py index 8a12031069b..e6f5faa1f07 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,7 +93,7 @@ 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) @@ -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 39a6c25bf39..5e38c5f8ef7 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", From 92c6e592d3d9d41f6e851adb2046f80f7ddc6fae Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 13:55:54 +0800 Subject: [PATCH 2/3] Update the order of horizontal/vertical codes --- pygmt/src/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/text.py b/pygmt/src/text.py index d286770f5de..0f734f6e284 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -99,8 +99,8 @@ def text_( # noqa: PLR0912 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. From 7251766cb48500664813f0df01167c9d099e0737 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 14:02:20 +0800 Subject: [PATCH 3/3] Fix mypy warning for confdict --- pygmt/helpers/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 055af53e53e..2d1d1f409a8 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 xarray as xr @@ -395,7 +395,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]: