diff --git a/freeqdsk/_fileutils.py b/freeqdsk/_fileutils.py index d8b4065..61fe6c3 100644 --- a/freeqdsk/_fileutils.py +++ b/freeqdsk/_fileutils.py @@ -15,7 +15,8 @@ import fortranformat as ff import numpy as np -from numpy.typing import ArrayLike + +from ._typing import FloatArray, ArrayLike def f2s(f: float) -> str: @@ -145,7 +146,7 @@ def write_1d(values: Iterable[Any], out: ChunkOutput) -> None: out.newline() -def write_2d(values: ArrayLike, out: ChunkOutput) -> None: +def write_2d(values: FloatArray, out: ChunkOutput) -> None: r""" Writes a 2D array to a ChunkOutput file handle. diff --git a/freeqdsk/_typing.py b/freeqdsk/_typing.py new file mode 100644 index 0000000..437b2c2 --- /dev/null +++ b/freeqdsk/_typing.py @@ -0,0 +1,20 @@ +"""Some type aliases, mostly for hinting about arrays of +numbers. `numpy.typing.ArrayLike` covers anything that can be +converted to an array of any type, and we mostly want to restrict to +arrays of floats. + +""" + +from typing import Union, Any, List +import numpy as np + +try: + from numpy.typing import NDArray + + float_ = np.floating[Any] + FloatArray = NDArray[np.floating[Any]] +except ImportError: + float_ = float # type: ignore + FloatArray = np.ndarray # type: ignore + +ArrayLike = Union[int, float, float_, FloatArray, List[float_]] diff --git a/freeqdsk/aeqdsk.py b/freeqdsk/aeqdsk.py index 74c7a3a..0b77c14 100644 --- a/freeqdsk/aeqdsk.py +++ b/freeqdsk/aeqdsk.py @@ -14,8 +14,8 @@ from typing import Dict, Optional, TextIO, Union import numpy as np -from numpy.typing import ArrayLike +from ._typing import ArrayLike from ._fileutils import read_array, write_array, write_line diff --git a/freeqdsk/geqdsk.py b/freeqdsk/geqdsk.py index 927bf29..5d96679 100644 --- a/freeqdsk/geqdsk.py +++ b/freeqdsk/geqdsk.py @@ -142,8 +142,8 @@ from typing import Dict, Optional, TextIO, Union import numpy as np -from numpy.typing import ArrayLike +from ._typing import ArrayLike from ._fileutils import read_array, read_line, write_array, write_line