Skip to content

Commit 3e3b575

Browse files
authored
Add type hints for GMT anchor codes (#3459)
1 parent 8e04d00 commit 3e3b575

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

pygmt/_typing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Type aliases for type hints.
3+
"""
4+
5+
from typing import Literal
6+
7+
# Anchor codes
8+
AnchorCode = Literal["TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"]

pygmt/helpers/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import sys
1212
import time
1313
import webbrowser
14-
from collections.abc import Iterable, Sequence
14+
from collections.abc import Iterable, Mapping, Sequence
1515
from typing import Any, Literal
1616

1717
import xarray as xr
@@ -395,7 +395,7 @@ def non_ascii_to_octal(
395395

396396
def build_arg_list( # noqa: PLR0912
397397
kwdict: dict[str, Any],
398-
confdict: dict[str, str] | None = None,
398+
confdict: Mapping[str, Any] | None = None,
399399
infile: str | pathlib.PurePath | Sequence[str | pathlib.PurePath] | None = None,
400400
outfile: str | pathlib.PurePath | None = None,
401401
) -> list[str]:

pygmt/src/text.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
text - Plot text on a figure.
33
"""
44

5+
from collections.abc import Sequence
6+
57
import numpy as np
8+
from pygmt._typing import AnchorCode
69
from pygmt.clib import Session
710
from pygmt.exceptions import GMTInvalidInput
811
from pygmt.helpers import (
@@ -44,11 +47,11 @@ def text_( # noqa: PLR0912
4447
textfiles=None,
4548
x=None,
4649
y=None,
47-
position=None,
50+
position: AnchorCode | None = None,
4851
text=None,
4952
angle=None,
5053
font=None,
51-
justify=None,
54+
justify: bool | None | AnchorCode | Sequence[AnchorCode] = None,
5255
**kwargs,
5356
):
5457
r"""
@@ -90,14 +93,14 @@ def text_( # noqa: PLR0912
9093
x/y : float or 1-D arrays
9194
The x and y coordinates, or an array of x and y coordinates to plot
9295
the text.
93-
position : str
96+
position
9497
Set reference point on the map for the text by using x, y
9598
coordinates extracted from ``region`` instead of providing them
9699
through ``x``/``y``. Specify with a two-letter (order independent)
97100
code, chosen from:
98101
99-
* Horizontal: **L**\ (eft), **C**\ (entre), **R**\ (ight)
100102
* Vertical: **T**\ (op), **M**\ (iddle), **B**\ (ottom)
103+
* Horizontal: **L**\ (eft), **C**\ (entre), **R**\ (ight)
101104
102105
For example, ``position="TL"`` plots the text at the Top Left corner
103106
of the map.
@@ -116,7 +119,7 @@ def text_( # noqa: PLR0912
116119
font. If no font info is explicitly given (i.e. ``font=True``), then
117120
the input to ``textfiles`` must have this information in one of its
118121
columns.
119-
justify : str, bool or list of str
122+
justify
120123
Set the alignment which refers to the part of the text string that
121124
will be mapped onto the (x, y) point. Choose a two-letter
122125
combination of **L**, **C**, **R** (for left, center, or right) and

pygmt/src/timestamp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import Sequence
77

88
from packaging.version import Version
9+
from pygmt._typing import AnchorCode
910
from pygmt.clib import Session, __gmt_version__
1011
from pygmt.helpers import build_arg_list, kwargs_to_strings
1112

@@ -17,7 +18,7 @@ def timestamp(
1718
self,
1819
text: str | None = None,
1920
label: str | None = None,
20-
justify: str = "BL",
21+
justify: AnchorCode = "BL",
2122
offset: float | str | Sequence[float | str] = ("-54p", "-54p"),
2223
font: str = "Helvetica,black",
2324
timefmt: str = "%Y %b %d %H:%M:%S",

0 commit comments

Comments
 (0)