Skip to main content

marvin.utilities.types

Classes

Labels

A container for classification labels. This class provides a consistent interface for working with labels, whether they come from enums, literals, lists, or other sources. Args: values: The label values. Can be an enum class, a sequence of values, or a Literal type. many: Whether this is a multi-label classifier (i.e., can select multiple values). Examples:
Methods:
  • get_indexed_labels
    Get a mapping of indices to label string representations.
  • get_type
    Get the type that should be used for validation.
  • validate
    Validate a value against the labels.

ParameterModel

PythonFunction

A dataclass representing a Python function. Attributes: function (Callable): The original function object. signature (inspect.Signature): The signature object of the function. name (str): The name of the function. docstring (str | None): The docstring of the function. parameters (List[ParameterModel]): The parameters of the function. return_annotation (Any | None): The return annotation of the function. source_code (str): The source code of the function. bound_parameters (dict[str, Any]): The parameters of the function bound with values. return_value (Any | None): The return value of the function call. Methods:
  • from_function
    Create a PythonFunction instance from a function. Args: func (Callable): The function to create a PythonFunction instance from. **kwargs: Additional keyword arguments to set as attributes on the PythonFunction instance. Returns: PythonFunction: The created PythonFunction instance.
  • from_function_call
    Create a PythonFunction instance from a function call. Args: func (Callable): The function to call. *args: Positional arguments to pass to the function call. **kwargs: Keyword arguments to pass to the function call. Returns: PythonFunction: The created PythonFunction instance, with the return value of the function call set as an attribute.

Functions

as_classifier

Convert a type to a Labels instance. This should only be called on types that have been verified as classifiers via is_classifier(). Args: typ: A type that represents a classifier (Enum, Literal, sequence, or list thereof) Returns: Labels: A Labels instance representing the classifier Raises: ValueError: If the type is not a valid classifier

is_classifier

Check if a type represents a classification task. This includes:
  • Single-label: Enum, Literal, or any sequence of values
  • Multi-label: list[Enum], list[Literal], or list[list]
Examples:

issubclass_safe

Safely check if x is a subclass of cls without raising errors. This combines isinstance(x, type) and issubclass(x, cls) checks in a safe way that won’t raise TypeError if x is not a type. Args: x: The value to check cls: A type or tuple of types to check against Returns: bool: True if x is a type and is a subclass of cls, False otherwise Example:

Parent Module: utilities