luna.types
check_type
def check_type(t1: type, t2: type) -> bool
Returns whether t1 is t2. Handles cases such as dicts, union types and lists.
LunaType Objects
@dataclass
class LunaType(ABC, Generic[T])
A LunaType represents a type which can be inputted by a user. Thus, it must have a string representation.
serializer_class
@property
@abstractmethod
def serializer_class() -> Any
Returns the class necessary to serialize/deserialize this type. Must be compatible with Pydantic.
Have a look at the following for guidance on how to create a custom serializer:
- serialisation: https://docs.pydantic.dev/latest/concepts/serialization/#custom-serializers
- deserialisation: https://docs.pydantic.dev/latest/concepts/validators/
- https://docs.pydantic.dev/1.10/usage/types/#literal-type
type_name
@property
@abstractmethod
def type_name() -> str
Language agnostic name for the type. i.e. 'Integer', 'String'
type_detail
@property
def type_detail() -> dict
Returns a dict expressing the full type in detail in an AST-like structure. This is useful for the FE to process how to display types.
NumberFormatter Objects
@dataclass
class NumberFormatter()
NumberFormatter is used to format numbers in a human-friendly way. For example, percentages can be modelled using this class, where the value is stored as a Decimal between 0 and 1, and the precision is stored as an int, with the suffix being the % sign, and operate being " * 100". This allows us to display percentages in a human-friendly way, i.e. 0.5 => 50%.
When serialising, operate is applied to the value, and then the value is rounded to the precision specified, with any prefix/suffix added. When deserialising, the prefix/suffix is validated, then reverse_operate is applied to the value before it is stored.
get_places
@staticmethod
def get_places(precision: int) -> Decimal
Returns a Decimal with the correct number of decimal places for the given precision, i.e. 2 => 0.01 :param precision: :return:
PercentageType Objects
class PercentageType(LunaType[Percentage])
A percentage is stored internally, as a Decimal usually between 0 and 1 denoting 0% and 100% respectively. Additionally, a precision is stored, which is the number of decimal places to display when serialising.
When serialising, it is converted to a string with a % sign, in the expanded form rounded to the precision requested, for example 0.54324 with precision 2 will be displayed as 54.32%. This uses 5up4down rounding.
When deserialising, it is expected to be in the same format, i.e. 50.00% => 0.5 with a precision of 2
BasisPoint Objects
@dataclass
class BasisPoint(NumberFormatter)
Basis points is one hundredth of a percentage point, i.e. 0.01%. It is stored internally as a Decimal between 0 and
- For example 25 basis points are equal to 0.25% and 0.0025 stored. The suffix "bps" is used when serialising.
Note that the precision is always 0, as basis points are always displayed as an integer, therefore 0.249% will be displayed as 25bps.
BasisPointType Objects
class BasisPointType(LunaType[BasisPoint])
A percentage is stored internally, as a Decimal usually between 0 and 1 denoting 0% and 100% respectively. Additionally, a precision is stored, which is the number of decimal places to display when serialising.
When serialising, it is converted to a string with a % sign, in the expanded form rounded to the precision requested, for example 0.54324 with precision 2 will be displayed as 54.32%. This uses 5up4down rounding.
When deserialising, it is expected to be in the same format, i.e. 50.00% => 0.5 with a precision of 2
type_to_str
def type_to_str(t: Any) -> str
This function takes a type and returns a more human-friendly string representation.
Given an int it will return int
instead of <class 'int'>
Given a datetime it will return datetime.datetime
instead of <class 'datetime.datetime'>
hint_to_luna_type
def hint_to_luna_type(hint: Any) -> LunaType
Transforms a Python typehint into a Luna type. Example: list[int] => ListType(of=IntegerType())
get_luna_type
def get_luna_type(input: Any) -> LunaType
Get the associated LunaType
from any Python object.
full_type
def full_type(obj: Any) -> type
Get the full type of an object. Example:
- full_type([0]) == list[int]
- full_type({"a": [1, 2]}) == dict[str, list[int]]