Skip to content

Commit

Permalink
Added typehints for numpy array dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
timbernat committed Apr 19, 2024
1 parent 63ae833 commit 81b825b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions polymerist/genutils/typetools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''Additional type-hinting and typechecking not provided by builtins'''

from typing import Any, Callable, ParamSpec, TypeVar, Union
from typing import Any, Callable, ParamSpec, TypeAlias, TypeVar, Union
import numpy.typing # NOTE : this import is necessary to use np.typing calls
import numpy as np

Expand Down Expand Up @@ -33,4 +33,18 @@ def isinunion(var : Any) -> bool:

isnumeric = _union_member_factory(Numeric)
isarraylike = _union_member_factory(ArrayLike)
isjsonserializable = _union_member_factory(JSONSerializable)
isjsonserializable = _union_member_factory(JSONSerializable)

# NUMPY-SPECIFIC TYPEHINTS
N = TypeVar('N', bound=int) # for typing arbitrary array dimension
M = TypeVar('M', bound=int) # for typing arbitrary array dimension
DType = TypeVar('DType', bound=np.generic)
Shape : TypeAlias = tuple

Coords2D : TypeAlias = np.ndarray[Shape[M, 2], DType] # an Mx2 array of M 3D coordinates
Coords3D : TypeAlias = np.ndarray[Shape[M, 3], DType] # an Mx3 array of M 3D coordinates
CoordsND : TypeAlias = np.ndarray[Shape[M, N], DType] # an MxN array of M ND coordinates

Vector2D : TypeAlias = np.ndarray[Shape[2], DType] # a 1x2 array of M 3D coordinates
Vector3D : TypeAlias = np.ndarray[Shape[3], DType] # a 1x3 array of M 3D coordinates
VectorND : TypeAlias = np.ndarray[Shape[N], DType] # a 1xN array of M ND coordinates

0 comments on commit 81b825b

Please sign in to comment.