-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated genutils.typetools into dedicated submodules
- Loading branch information
Showing
5 changed files
with
59 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'''Additional type-hinting and typechecking not provided by Python builtins''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'''Type-hinting for Union-bound categories of types''' | ||
|
||
from typing import Any, Callable, Literal, Type, Union | ||
from numpy import number as np_number | ||
from numpy.typing import ArrayLike | ||
|
||
from .parametric import U | ||
|
||
|
||
def _union_member_factory(union : U) -> Callable[[Any], bool]: | ||
'''Factory for making Uion-membership-checking functions''' | ||
def isinunion(var : Any) -> bool: | ||
'''Check if an object is a member of a Union class''' | ||
return isinstance(var, union.__args__) | ||
return isinunion | ||
|
||
Numeric = Union[int, float, complex, np_number] | ||
JSONSerializable = Union[str, bool, int, float, tuple, list, dict] | ||
|
||
isnumeric = _union_member_factory(Numeric) | ||
isarraylike = _union_member_factory(ArrayLike) | ||
isjsonserializable = _union_member_factory(JSONSerializable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'''Type aliases specific to numpy arrays''' | ||
|
||
from typing import TypeAlias, TypeVar | ||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'''Type aliases for Python callable input parameters''' | ||
|
||
from typing import TypeVar, ParamSpec, _UnionGenericAlias | ||
|
||
T = TypeVar('T') # universal generic type | ||
|
||
C = TypeVar('C') # generic type for a class | ||
O = TypeVar('O') # generic type for an object passed to a function | ||
F = TypeVar('F') # generic type for a function | ||
U = TypeVar('U', bound=_UnionGenericAlias) # generic class from representing Literal Unions (since arg-free union is not supported) | ||
|
||
P = ParamSpec('P') # for representing (preserved) input parameters | ||
R = TypeVar('R') # for representing generic return values | ||
|
||
# TOSELF: replace with typing.ParamSpecArgs/.ParamSpecKWArgs? | ||
Args = TypeVar('Args' ) # generic type for arguments to a function | ||
KWArgs = TypeVar('KWArgs') # generic type for keyword arguments to a function |