diff --git a/spatialmath/base/_types_311.py b/spatialmath/base/_types_311.py deleted file mode 100644 index bd1d64b9..00000000 --- a/spatialmath/base/_types_311.py +++ /dev/null @@ -1,157 +0,0 @@ -# for Python >= 3.9 - -from typing import ( - overload, - cast, - Union, - List, - Tuple, - Type, - TextIO, - Any, - Callable, - Optional, - Iterator, - Self, -) -from typing import Literal as L - -from numpy import ndarray, dtype, floating -from numpy.typing import NDArray, DTypeLike - -# array like - -# these are input to many functions in spatialmath.base, and can be a list, tuple or -# ndarray. The elements are generally float, but some functions accept symbolic -# arguments as well, which leads to a NumPy array with dtype=object. For now -# symbolics will throw a lint error. Possibly create variants ArrayLikeSym that -# admits symbols and can be used for those functions that accept symbols. -# -# The variants like ArrayLike2 indicate that a list, tuple or ndarray of -# length 2 is expected. Static checking of tuple length is possible, but not for lists. -# This might be possible in future versions of Python, but for now it is a hint to the -# coder about what is expected - -# cannot be a scalar -ArrayLikePure = Union[List[float], Tuple[float, ...], ndarray[Any, dtype[floating]]] - -ArrayLike = Union[float, List[float], Tuple[float, ...], ndarray[Any, dtype[floating]]] -ArrayLike2 = Union[ - List[float], - Tuple[float, float], - ndarray[ - Tuple[L[2]], - dtype[floating], - ], -] -ArrayLike3 = Union[ - List[float], - Tuple[float, float, float], - ndarray[ - Tuple[L[3]], - dtype[floating], - ], -] -ArrayLike4 = Union[ - List[float], - Tuple[float, float, float, float], - ndarray[ - Tuple[L[4]], - dtype[floating], - ], -] -ArrayLike6 = Union[ - List[float], - Tuple[float, float, float, float, float, float], - ndarray[ - Tuple[L[6]], - dtype[floating], - ], -] - -# real vectors -R1 = ndarray[ - Tuple[L[1]], - dtype[floating], -] # R^1 -R2 = ndarray[ - Tuple[L[2]], - dtype[floating], -] # R^2 -R3 = ndarray[ - Tuple[L[3]], - dtype[floating], -] # R^3 -R4 = ndarray[ - Tuple[L[4]], - dtype[floating], -] # R^4 -R6 = ndarray[ - Tuple[L[6]], - dtype[floating], -] # R^6 - -R8 = ndarray[ - Tuple[L[8,]], - dtype[floating], -] # R^8 - -# real matrices -R1x1 = ndarray[Tuple[L[1], L[1]], dtype[floating]] # R^{1x1} matrix -R2x2 = ndarray[Tuple[L[2], L[2]], dtype[floating]] # R^{2x2} matrix -R3x3 = ndarray[Tuple[L[3], L[3]], dtype[floating]] # R^{3x3} matrix -R4x4 = ndarray[Tuple[L[4], L[4]], dtype[floating]] # R^{4x4} matrix -R6x6 = ndarray[Tuple[L[6], L[6]], dtype[floating]] # R^{6x6} matrix -R8x8 = ndarray[Tuple[L[8], L[8]], dtype[floating]] # R^{8x8} matrix -R1x3 = ndarray[Tuple[L[1], L[3]], dtype[floating]] # R^{1x3} row vector -R3x1 = ndarray[Tuple[L[3], L[1]], dtype[floating]] # R^{3x1} column vector -R1x2 = ndarray[Tuple[L[1], L[2]], dtype[floating]] # R^{1x2} row vector -R2x1 = ndarray[Tuple[L[2], L[1]], dtype[floating]] # R^{2x1} column vector - -# Points2 = ndarray[Tuple[L[2, Any]], dtype[floating]] # R^{2xN} matrix -# Points3 = ndarray[Tuple[L[3, Any]], dtype[floating]] # R^{2xN} matrix -Points2 = NDArray # R^{2xN} matrix -Points3 = NDArray # R^{2xN} matrix - -# RNx3 = ndarray[(Any, 3), dtype[floating]] # R^{Nx3} matrix -RNx3 = NDArray - -# Lie group elements -SO2Array = ndarray[Tuple[L[2], L[2]], dtype[floating]] # SO(2) rotation matrix -SE2Array = ndarray[Tuple[L[3], L[3]], dtype[floating]] # SE(2) rigid-body transform -SO3Array = ndarray[Tuple[L[3], L[3]], dtype[floating]] # SO(3) rotation matrix -SE3Array = ndarray[Tuple[L[4], L[4]], dtype[floating]] # SE(3) rigid-body transform - -# Lie algebra elements -so2Array = ndarray[ - Tuple[L[2, 2]], dtype[floating] -] # so(2) Lie algebra of SO(2), skew-symmetrix matrix -se2Array = ndarray[ - Tuple[L[3, 3]], dtype[floating] -] # se(2) Lie algebra of SE(2), augmented skew-symmetrix matrix -so3Array = ndarray[ - Tuple[L[3, 3]], dtype[floating] -] # so(3) Lie algebra of SO(3), skew-symmetrix matrix -se3Array = ndarray[ - Tuple[L[4, 4]], dtype[floating] -] # se(3) Lie algebra of SE(3), augmented skew-symmetrix matrix - -# quaternion arrays -QuaternionArray = ndarray[ - Tuple[L[4,]], - dtype[floating], -] -UnitQuaternionArray = ndarray[ - Tuple[L[4,]], - dtype[floating], -] - -Rn = Union[R2, R3] - -SOnArray = Union[SO2Array, SO3Array] -SEnArray = Union[SE2Array, SE3Array] - -sonArray = Union[so2Array, so3Array] -senArray = Union[se2Array, se3Array] - -Color = Union[str, ArrayLike3] diff --git a/spatialmath/base/_types_35.py b/spatialmath/base/_types_35.py deleted file mode 100644 index d74f63ac..00000000 --- a/spatialmath/base/_types_35.py +++ /dev/null @@ -1,150 +0,0 @@ -# for Python <= 3.8 - -from typing import ( - overload, - Union, - List, - Tuple, - Type, - TextIO, - Any, - Callable, - Optional, - Iterator, -) -from typing_extensions import Literal as L -from typing_extensions import Self - -# array like - -# these are input to many functions in spatialmath.base, and can be a list, tuple or -# ndarray. The elements are generally float, but some functions accept symbolic -# arguments as well, which leads to a NumPy array with dtype=object -# -# The variants like ArrayLike2 indicate that a list, tuple or ndarray of length 2 is -# expected. Static checking of tuple length is possible but not a lists. This might be -# possible in future versions of Python, but for now it is a hint to the coder about -# what is expected - -from numpy.typing import DTypeLike, NDArray # , ArrayLike - -from typing import cast - -# from typing import TypeVar -# NDArray = TypeVar('NDArray') -import numpy as np - - -ArrayLike = Union[float, List[float], Tuple[float, ...], NDArray] -ArrayLikePure = Union[List[float], Tuple[float, ...], NDArray] -ArrayLike2 = Union[List, Tuple[float, float], NDArray] -ArrayLike3 = Union[List, Tuple[float, float, float], NDArray] -ArrayLike4 = Union[List, Tuple[float, float, float, float], NDArray] -ArrayLike6 = Union[List, Tuple[float, float, float, float, float, float], NDArray] - -# real vectors -R1 = NDArray[np.floating] # R^1 -R2 = NDArray[np.floating] # R^2 -R3 = NDArray[np.floating] # R^3 -R4 = NDArray[np.floating] # R^4 -R6 = NDArray[np.floating] # R^6 -R8 = NDArray[np.floating] # R^8 - -# real matrices -R1x1 = NDArray # R^{1x1} matrix -R2x2 = NDArray # R^{3x3} matrix -R3x3 = NDArray # R^{3x3} matrix -R4x4 = NDArray # R^{4x4} matrix -R6x6 = NDArray # R^{6x6} matrix -R8x8 = NDArray # R^{8x8} matrix - -R1x3 = NDArray # R^{1x3} row vector -R3x1 = NDArray # R^{3x1} column vector -R1x2 = NDArray # R^{1x2} row vector -R2x1 = NDArray # R^{2x1} column vector - -Points2 = NDArray # R^{2xN} matrix -Points3 = NDArray # R^{2xN} matrix - -RNx3 = NDArray # R^{Nx3} matrix - - -# Lie group elements -SO2Array = NDArray # SO(2) rotation matrix -SE2Array = NDArray # SE(2) rigid-body transform -SO3Array = NDArray # SO(3) rotation matrix -SE3Array = NDArray # SE(3) rigid-body transform - -# Lie algebra elements -so2Array = NDArray # so(2) Lie algebra of SO(2), skew-symmetrix matrix -se2Array = NDArray # se(2) Lie algebra of SE(2), augmented skew-symmetrix matrix -so3Array = NDArray # so(3) Lie algebra of SO(3), skew-symmetrix matrix -se3Array = NDArray # se(3) Lie algebra of SE(3), augmented skew-symmetrix matrix - -# quaternion arrays -QuaternionArray = NDArray -UnitQuaternionArray = NDArray - -Rn = Union[R2, R3] - -SOnArray = Union[SO2Array, SO3Array] -SEnArray = Union[SE2Array, SE3Array] - -sonArray = Union[so2Array, so3Array] -senArray = Union[se2Array, se3Array] - -# __all__ = [ -# overload, -# Union, -# List, -# Tuple, -# Type, -# TextIO, -# Any, -# Callable, -# Optional, -# Iterator, -# ArrayLike, -# ArrayLike2, -# ArrayLike3, -# ArrayLike4, -# ArrayLike6, -# # real vectors -# R2, -# R3, -# R4, -# R6, -# R8, -# # real matrices -# R2x2, -# R3x3, -# R4x4, -# R6x6, -# R8x8, -# R1x3, -# R3x1, -# R1x2, -# R2x1, -# Points2, -# Points3, -# RNx3, -# # Lie group elements -# SO2Array, -# SE2Array, -# SO3Array, -# SE3Array, -# # Lie algebra elements -# so2Array, -# se2Array, -# so3Array, -# se3Array, -# # quaternion arrays -# QuaternionArray, -# UnitQuaternionArray, -# Rn, -# SOnArray, -# SEnArray, -# sonArray, -# senArray, -# ] -Color = Union[str, ArrayLike3] diff --git a/spatialmath/base/_types_39.py b/spatialmath/base/_types_39.py deleted file mode 100644 index 350210f5..00000000 --- a/spatialmath/base/_types_39.py +++ /dev/null @@ -1,158 +0,0 @@ -# for Python >= 3.9 - -from typing import ( - overload, - cast, - Union, - List, - Tuple, - Type, - TextIO, - Any, - Callable, - Optional, - Iterator, -) -from typing import Literal as L -from typing_extensions import Self - -import numpy as np -from numpy import ndarray, dtype, floating -from numpy.typing import NDArray, DTypeLike - -# array like - -# these are input to many functions in spatialmath.base, and can be a list, tuple or -# ndarray. The elements are generally float, but some functions accept symbolic -# arguments as well, which leads to a NumPy array with dtype=object. For now -# symbolics will throw a lint error. Possibly create variants ArrayLikeSym that -# admits symbols and can be used for those functions that accept symbols. -# -# The variants like ArrayLike2 indicate that a list, tuple or ndarray of -# length 2 is expected. Static checking of tuple length is possible, but not for lists. -# This might be possible in future versions of Python, but for now it is a hint to the -# coder about what is expected - - -ArrayLike = Union[float, List[float], Tuple[float, ...], ndarray[Any, dtype[floating]]] -ArrayLikePure = Union[List[float], Tuple[float, ...], ndarray[Any, dtype[floating]]] -ArrayLike2 = Union[ - List[float], - Tuple[float, float], - ndarray[ - Tuple[L[2,]], - dtype[floating], - ], -] -ArrayLike3 = Union[ - List[float], - Tuple[float, float, float], - ndarray[ - Tuple[L[3,]], - dtype[floating], - ], -] -ArrayLike4 = Union[ - List[float], - Tuple[float, float, float, float], - ndarray[ - Tuple[L[4,]], - dtype[floating], - ], -] -ArrayLike6 = Union[ - List[float], - Tuple[float, float, float, float, float, float], - ndarray[ - Tuple[L[6,]], - dtype[floating], - ], -] - -# real vectors -R1 = ndarray[ - Tuple[L[1]], - dtype[floating], -] # R^1 -R2 = ndarray[ - Tuple[L[2]], - dtype[floating], -] # R^2 -R3 = ndarray[ - Tuple[L[3]], - dtype[floating], -] # R^3 -R4 = ndarray[ - Tuple[L[4]], - dtype[floating], -] # R^4 -R6 = ndarray[ - Tuple[L[6]], - dtype[floating], -] # R^6 -R8 = ndarray[ - Tuple[L[8]], - dtype[floating], -] # R^8 - -# real matrices -R1x1 = ndarray[Tuple[L[1], L[1]], dtype[floating]] # R^{1x1} matrix -R2x2 = ndarray[Tuple[L[2], L[2]], dtype[floating]] # R^{2x2} matrix -R3x3 = ndarray[Tuple[L[3], L[3]], dtype[floating]] # R^{3x3} matrix -R4x4 = ndarray[Tuple[L[4], L[4]], dtype[floating]] # R^{4x4} matrix -R6x6 = ndarray[Tuple[L[6], L[6]], dtype[floating]] # R^{6x6} matrix -R8x8 = ndarray[Tuple[L[8], L[8]], dtype[floating]] # R^{8x8} matrix -R1x3 = ndarray[Tuple[L[1], L[3]], dtype[floating]] # R^{1x3} row vector -R3x1 = ndarray[Tuple[L[3], L[1]], dtype[floating]] # R^{3x1} column vector -R1x2 = ndarray[Tuple[L[1], L[2]], dtype[floating]] # R^{1x2} row vector -R2x1 = ndarray[Tuple[L[2], L[1]], dtype[floating]] # R^{2x1} column vector - -# Points2 = ndarray[Tuple[L[2, Any]], dtype[floating]] # R^{2xN} matrix -# Points3 = ndarray[Tuple[L[3, Any]], dtype[floating]] # R^{2xN} matrix -Points2 = NDArray # R^{2xN} matrix -Points3 = NDArray # R^{2xN} matrix - -# RNx3 = ndarray[(Any, 3), dtype[floating]] # R^{Nx3} matrix -RNx3 = NDArray - -# Lie group elements -SO2Array = ndarray[Tuple[L[2, 2]], dtype[floating]] # SO(2) rotation matrix -SE2Array = ndarray[Tuple[L[3, 3]], dtype[floating]] # SE(2) rigid-body transform -# SO3Array = ndarray[Tuple[L[3, 3]], dtype[floating]] -SO3Array = np.ndarray[Tuple[L[3], L[3]], dtype[floating]] # SO(3) rotation matrix -SE3Array = ndarray[Tuple[L[4], L[4]], dtype[floating]] # SE(3) rigid-body transform - - -# Lie algebra elements -so2Array = ndarray[ - Tuple[L[2, 2]], dtype[floating] -] # so(2) Lie algebra of SO(2), skew-symmetrix matrix -se2Array = ndarray[ - Tuple[L[3, 3]], dtype[floating] -] # se(2) Lie algebra of SE(2), augmented skew-symmetrix matrix -so3Array = ndarray[ - Tuple[L[3, 3]], dtype[floating] -] # so(3) Lie algebra of SO(3), skew-symmetrix matrix -se3Array = ndarray[ - Tuple[L[4, 4]], dtype[floating] -] # se(3) Lie algebra of SE(3), augmented skew-symmetrix matrix - -# quaternion arrays -QuaternionArray = ndarray[ - Tuple[L[4,]], - dtype[floating], -] -UnitQuaternionArray = ndarray[ - Tuple[L[4,]], - dtype[floating], -] - -Rn = Union[R2, R3] - -SOnArray = Union[SO2Array, SO3Array] -SEnArray = Union[SE2Array, SE3Array] - -sonArray = Union[so2Array, so3Array] -senArray = Union[se2Array, se3Array] - -Color = Union[str, ArrayLike3] diff --git a/spatialmath/base/types.py b/spatialmath/base/types.py index eb35e9d2..18e110c3 100644 --- a/spatialmath/base/types.py +++ b/spatialmath/base/types.py @@ -1,11 +1,170 @@ -import sys +# Type aliases used throughout spatialmath and spatialmath.base. +# +# This used to dispatch across three per-Python-version files +# (_types_35.py, _types_39.py, _types_311.py) via sys.version_info, from +# when Python 3.5-3.10 support coexisted. pyproject.toml has required +# Python >=3.10 since #186, so the only genuinely version-dependent piece +# left is where `Self` lives (typing_extensions before 3.11, typing from +# 3.11 on). -_version = sys.version_info.minor +import sys +from typing import ( + overload, + cast, + Union, + List, + Tuple, + Type, + TextIO, + Any, + Callable, + Optional, + Iterator, +) +from typing import Literal as L -if _version >= 11: - from spatialmath.base._types_311 import * -elif _version >= 9: - from spatialmath.base._types_39 import * +if sys.version_info >= (3, 11): + from typing import Self else: - from spatialmath.base._types_35 import * + from typing_extensions import Self + +from numpy import ndarray, dtype, floating +from numpy.typing import NDArray, DTypeLike + +# array like + +# these are input to many functions in spatialmath.base, and can be a list, tuple or +# ndarray. The elements are generally float, but some functions accept symbolic +# arguments as well, which leads to a NumPy array with dtype=object. For now +# symbolics will throw a lint error. Possibly create variants ArrayLikeSym that +# admits symbols and can be used for those functions that accept symbols. +# +# The variants like ArrayLike2 indicate that a list, tuple or ndarray of +# length 2 is expected. Static checking of tuple length is possible, but not for lists. +# This might be possible in future versions of Python, but for now it is a hint to the +# coder about what is expected + +# cannot be a scalar +ArrayLikePure = Union[List[float], Tuple[float, ...], ndarray[Any, dtype[floating]]] + +ArrayLike = Union[float, List[float], Tuple[float, ...], ndarray[Any, dtype[floating]]] +ArrayLike2 = Union[ + List[float], + Tuple[float, float], + ndarray[ + Tuple[L[2]], + dtype[floating], + ], +] +ArrayLike3 = Union[ + List[float], + Tuple[float, float, float], + ndarray[ + Tuple[L[3]], + dtype[floating], + ], +] +ArrayLike4 = Union[ + List[float], + Tuple[float, float, float, float], + ndarray[ + Tuple[L[4]], + dtype[floating], + ], +] +ArrayLike6 = Union[ + List[float], + Tuple[float, float, float, float, float, float], + ndarray[ + Tuple[L[6]], + dtype[floating], + ], +] + +# real vectors +R1 = ndarray[ + Tuple[L[1]], + dtype[floating], +] # R^1 +R2 = ndarray[ + Tuple[L[2]], + dtype[floating], +] # R^2 +R3 = ndarray[ + Tuple[L[3]], + dtype[floating], +] # R^3 +R4 = ndarray[ + Tuple[L[4]], + dtype[floating], +] # R^4 +R6 = ndarray[ + Tuple[L[6]], + dtype[floating], +] # R^6 + +R8 = ndarray[ + Tuple[L[8,]], + dtype[floating], +] # R^8 + +# real matrices +R1x1 = ndarray[Tuple[L[1], L[1]], dtype[floating]] # R^{1x1} matrix +R2x2 = ndarray[Tuple[L[2], L[2]], dtype[floating]] # R^{2x2} matrix +R3x3 = ndarray[Tuple[L[3], L[3]], dtype[floating]] # R^{3x3} matrix +R4x4 = ndarray[Tuple[L[4], L[4]], dtype[floating]] # R^{4x4} matrix +R6x6 = ndarray[Tuple[L[6], L[6]], dtype[floating]] # R^{6x6} matrix +R8x8 = ndarray[Tuple[L[8], L[8]], dtype[floating]] # R^{8x8} matrix +R1x3 = ndarray[Tuple[L[1], L[3]], dtype[floating]] # R^{1x3} row vector +R3x1 = ndarray[Tuple[L[3], L[1]], dtype[floating]] # R^{3x1} column vector +R1x2 = ndarray[Tuple[L[1], L[2]], dtype[floating]] # R^{1x2} row vector +R2x1 = ndarray[Tuple[L[2], L[1]], dtype[floating]] # R^{2x1} column vector + +# Points2 = ndarray[Tuple[L[2, Any]], dtype[floating]] # R^{2xN} matrix +# Points3 = ndarray[Tuple[L[3, Any]], dtype[floating]] # R^{2xN} matrix +Points2 = NDArray # R^{2xN} matrix +Points3 = NDArray # R^{2xN} matrix + +# RNx3 = ndarray[(Any, 3), dtype[floating]] # R^{Nx3} matrix +RNx3 = NDArray + +# Lie group elements +SO2Array = ndarray[Tuple[L[2], L[2]], dtype[floating]] # SO(2) rotation matrix +SE2Array = ndarray[Tuple[L[3], L[3]], dtype[floating]] # SE(2) rigid-body transform +SO3Array = ndarray[Tuple[L[3], L[3]], dtype[floating]] # SO(3) rotation matrix +SE3Array = ndarray[Tuple[L[4], L[4]], dtype[floating]] # SE(3) rigid-body transform + +# Lie algebra elements +so2Array = ndarray[ + Tuple[L[2, 2]], dtype[floating] +] # so(2) Lie algebra of SO(2), skew-symmetrix matrix +se2Array = ndarray[ + Tuple[L[3, 3]], dtype[floating] +] # se(2) Lie algebra of SE(2), augmented skew-symmetrix matrix +so3Array = ndarray[ + Tuple[L[3, 3]], dtype[floating] +] # so(3) Lie algebra of SO(3), skew-symmetrix matrix +se3Array = ndarray[ + Tuple[L[4, 4]], dtype[floating] +] # se(3) Lie algebra of SE(3), augmented skew-symmetrix matrix + +# quaternion arrays +QuaternionArray = ndarray[ + Tuple[L[4,]], + dtype[floating], +] +UnitQuaternionArray = ndarray[ + Tuple[L[4,]], + dtype[floating], +] + +Rn = Union[R2, R3] + +SOnArray = Union[SO2Array, SO3Array] +SEnArray = Union[SE2Array, SE3Array] + +sonArray = Union[so2Array, so3Array] +senArray = Union[se2Array, se3Array] + +Color = Union[str, ArrayLike3]