Skip to content

types.aliases

Module for all Alias types. Alias types are types that handle to conversion for other types. All builtin types (int, str, float, etc...) have a corresponding Alias type.

Alias

Parent class for all aliases. Stores references to all known alias types and handles resolving them. Additionally, it provides a convenience wrapper for alias types by implementing a custom cls.__convert__() that calls cls.convert() for non-parameterized types and cls.g_convert() for generic types.

Source code in /home/runner/work/arc/arc/arc/types/aliases.py
class Alias:
    """Parent class for all aliases. Stores references to all
    known alias types and handles resolving them. Additionally,
    it provides a convenience wrapper for alias types by implementing
    a custom `cls.__convert__()` that calls `cls.convert()` for non-parameterized
    types and `cls.g_convert()` for generic types.
    """

    aliases: dict[Annotation, type[TypeProtocol]] = {}
    alias_for: t.ClassVar[AliasFor | tuple[AliasFor]] = None  # type: ignore
    name: t.ClassVar[t.Optional[str]] = None
    convert: t.Callable[..., t.Any]
    g_convert: t.Callable[..., t.Any]

    @classmethod
    def __convert__(cls, value: str, typ: TypeInfo[T]) -> T:
        if cls.name:
            typ.name = cls.name

        if not typ.sub_types:
            obj = api.dispatch_args(cls.convert, value, typ)
        else:
            obj = api.dispatch_args(cls.g_convert, value, typ)

        return obj

    def __init_subclass__(cls, of: t.Optional[AliasFor | tuple[AliasFor]] = None):
        if of:
            cls.alias_for = of

            if isinstance(cls.alias_for, tuple):
                aliases = cls.alias_for
            else:
                aliases = (cls.alias_for,)

            for alias in aliases:
                Alias.aliases[alias] = cls  # type: ignore

    @classmethod
    def resolve(cls, annotation: Annotation) -> type[TypeProtocol]:
        """Handles resolving alias types"""

        if safe.issubclass(annotation, TypeProtocol):
            return t.cast(type[TypeProtocol], annotation)
        elif annotation in cls.aliases:
            # Type is a key
            # We perform this check once before hand
            # because some typing types don't have
            # the mro() method
            return cls.aliases[annotation]
        else:
            assert isinstance(annotation, type), f"{annotation} is not a type"

            # Type is a subclass of a key
            for parent in annotation.mro():
                if parent in cls.aliases:
                    return cls.aliases[parent]

        name = colorize(annotation.__name__, fg.YELLOW)
        raise TypeError(
            f"{name} is not a valid type. "
            f"Please ensure that {name} conforms to the custom type protocol "
            f"or that there is a alias type registered for it: "
            "https://arc.seancollings.dev/usage/parameters/types/custom-types"
        )

resolve(annotation) classmethod

Handles resolving alias types

Source code in /home/runner/work/arc/arc/arc/types/aliases.py
@classmethod
def resolve(cls, annotation: Annotation) -> type[TypeProtocol]:
    """Handles resolving alias types"""

    if safe.issubclass(annotation, TypeProtocol):
        return t.cast(type[TypeProtocol], annotation)
    elif annotation in cls.aliases:
        # Type is a key
        # We perform this check once before hand
        # because some typing types don't have
        # the mro() method
        return cls.aliases[annotation]
    else:
        assert isinstance(annotation, type), f"{annotation} is not a type"

        # Type is a subclass of a key
        for parent in annotation.mro():
            if parent in cls.aliases:
                return cls.aliases[parent]

    name = colorize(annotation.__name__, fg.YELLOW)
    raise TypeError(
        f"{name} is not a valid type. "
        f"Please ensure that {name} conforms to the custom type protocol "
        f"or that there is a alias type registered for it: "
        "https://arc.seancollings.dev/usage/parameters/types/custom-types"
    )