Skip to content

Commit 17c1e9c

Browse files
committed
Move StringArrayTypes to pygmt/_typing.py
Also improve docstring of strings_to_ctypes_array function to mention np.ndarray as supported input.
1 parent 757da24 commit 17c1e9c

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

pygmt/_typing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
Type aliases for type hints.
33
"""
44

5+
import contextlib
6+
import importlib
7+
from collections.abc import Sequence
58
from typing import Literal
69

10+
import numpy as np
11+
712
# Anchor codes
813
AnchorCode = Literal["TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"]
14+
15+
# String array types
16+
StringArrayTypes = Sequence[str] | np.ndarray
17+
with contextlib.suppress(ImportError):
18+
StringArrayTypes |= importlib.import_module(name="pyarrow").StringArray

pygmt/clib/conversion.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,9 @@
99
import numpy as np
1010
import pandas as pd
1111
from packaging.version import Version
12+
from pygmt._typing import StringArrayTypes
1213
from pygmt.exceptions import GMTInvalidInput
1314

14-
StringArrayTypes = Sequence[str] | np.ndarray
15-
16-
try:
17-
import pyarrow as pa
18-
19-
StringArrayTypes |= pa.StringArray
20-
except ImportError:
21-
pa = None
22-
2315

2416
def dataarray_to_matrix(grid):
2517
"""
@@ -292,13 +284,13 @@ def sequence_to_ctypes_array(
292284

293285
def strings_to_ctypes_array(strings: StringArrayTypes) -> ctp.Array:
294286
"""
295-
Convert a sequence (e.g., a list) of strings or a pyarrow.StringArray into a ctypes
296-
array.
287+
Convert a sequence (e.g., a list) or numpy.ndarray of strings or a
288+
pyarrow.StringArray into a ctypes array.
297289
298290
Parameters
299291
----------
300292
strings
301-
A sequence of strings or a pyarrow.StringArray.
293+
A sequence of strings, a numpy.ndarray of str dtype, or a pyarrow.StringArray.
302294
303295
Returns
304296
-------

pygmt/clib/session.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy as np
1818
import pandas as pd
1919
import xarray as xr
20+
from pygmt._typing import StringArrayTypes
2021
from pygmt.clib.conversion import (
2122
array_to_datetime,
2223
dataarray_to_matrix,
@@ -34,16 +35,6 @@
3435
tempfile_from_image,
3536
)
3637

37-
StringArrayTypes = Sequence[str] | np.ndarray
38-
39-
try:
40-
import pyarrow as pa
41-
42-
StringArrayTypes |= pa.StringArray
43-
except ImportError:
44-
pa = None
45-
46-
4738
FAMILIES = [
4839
"GMT_IS_DATASET", # Entity is a data table
4940
"GMT_IS_GRID", # Entity is a grid

0 commit comments

Comments
 (0)