# ----------------------------------------------------------------------------# SymForce - Copyright 2022, Skydio, Inc.# This source code is under the Apache 2.0 license found in the LICENSE file.# ----------------------------------------------------------------------------"""Common type definitions."""# ruff: noqa: F403, F405, A005importabcimportdataclassesimportos# Expose all types.fromtypingimport*# This is kind of a heavy/unnecessary dependency,here so only import when type checking so we can# resolve the annotation belowifTYPE_CHECKING:importnumpyasnp# noqa: F401# TODO(hayk,aaron): Either make this a union of "Scalar types", or different typevars for numeric# and symbolic scalars or somethingifTYPE_CHECKING:# Currently this can be any type, and doesn't even express that multiple Scalars in a signature# are the same (which is usually or always the case). However, making this a TypeVar with# a loose enough bound is similarly annoyingScalar=Anyelse:Scalar=float# Alias for argument type of open, which typing does not seem to have. We don't include int because# who uses that anyway, and bytes because some things in os.path don't support thatOpenable=Union[str,os.PathLike]# Represents any Group element objectElement=Any# Represents any Group element type or objectElementOrType=Union[Element,Type]# Specialization for scalar elementsScalarElement=ScalarScalarElementOrType=Union[ScalarElement,Type[ScalarElement]]# Specialization for sequence elementsSequenceElement=Sequence[Element]SequenceElementOrType=Union[SequenceElement,Type[SequenceElement]]# Specialization for array elements# We need "Union" here to avoid import errors associated with numpy only being imported when type# checking. Without "Union" mypy thinks our type alias is just a string, not a type alias.# See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#type-aliases# This could be improved after we upgrade to Mypy 0.930 or later by using "TypeAlias"ArrayElement=Union["np.ndarray"]ArrayElementOrType=Union[ArrayElement,Type[ArrayElement]]# Dataclass MetaclassifTYPE_CHECKING:# Mypy doesn't understand __subclasshook__Dataclass=Anyelse:
[docs]classDataclass(abc.ABC):""" Metaclass for dataclasses (which do not have a common superclass) """@abc.abstractmethoddef__init__(self,*args:Any,**kwargs:Any):pass@classmethoddef__subclasshook__(cls,subclass:Type)->bool:returndataclasses.is_dataclass(subclass)andisinstance(subclass,type)
[docs]defany_args(f:Callable[...,_ReturnType])->Callable[...,_ReturnType]:""" Decorator to mark an abstract method as allowing subclasses to override with any argument types. THIS LIES TO THE TYPE CHECKER, AND ALLOWS VIOLATION OF THE LISKOV SUBSTITUTION PRINCIPLE. USE ONLY ON FUNCTIONS THAT WILL NEVER BE CALLED IN A CONTEXT THAT KNOWS ONLY THE BASE TYPE. """returnf