Skip to content

fasthx.typing

ComponentSelector = T | RequestComponentSelector[T] module-attribute

Type alias for all types of component selectors.

RenderFunction = SyncRenderFunction[Tcontra] | AsyncRenderFunction[Tcontra] module-attribute

Sync or async render function type.

AsyncRenderFunction

Bases: Protocol[Tcontra]

Async render function definition.

Source code in fasthx/typing.py
class AsyncRenderFunction(Protocol[Tcontra]):
    """Async render function definition."""

    async def __call__(self, result: Tcontra, *, context: dict[str, Any], request: Request) -> str:
        """
        Arguments:
            result: The result of the route the renderer is used on.
            context: Every keyword argument the route received.
            request: The request being served.

        Returns:
            The rendered string.
        """
        ...

__call__(result, *, context, request) async

Parameters:

Name Type Description Default
result Tcontra

The result of the route the renderer is used on.

required
context dict[str, Any]

Every keyword argument the route received.

required
request Request

The request being served.

required

Returns:

Type Description
str

The rendered string.

Source code in fasthx/typing.py
async def __call__(self, result: Tcontra, *, context: dict[str, Any], request: Request) -> str:
    """
    Arguments:
        result: The result of the route the renderer is used on.
        context: Every keyword argument the route received.
        request: The request being served.

    Returns:
        The rendered string.
    """
    ...

RequestComponentSelector

Bases: Protocol[Tco]

Component selector protocol that uses the request to select the component that will be rendered.

The protocol is runtime-checkable, so it can be used in isinstance(), issubclass() calls.

Source code in fasthx/typing.py
@runtime_checkable
class RequestComponentSelector(Protocol[Tco]):
    """
    Component selector protocol that uses the request to select the component that will be rendered.

    The protocol is runtime-checkable, so it can be used in `isinstance()`, `issubclass()` calls.
    """

    def get_component(self, request: Request, error: Exception | None) -> Tco:
        """
        Returns the component that was requested by the client.

        The caller should ensure that `error` will be the exception that was raised by the
        route or `None` if the route returned normally.

        If an implementation can not or does not want to handle route errors, then the method
        should re-raise the received exception. Example:

        ```python
        class MyComponentSelector:
            def get_component(self, request: Request, error: Exception | None) -> str:
                if error is not None:
                    raise error

                ...
        ```

        Raises:
            KeyError: If the component couldn't be identified.
            Exception: The received `error` argument if it was not `None` and the implementation
                can not handle route errors.
        """
        ...

get_component(request, error)

Returns the component that was requested by the client.

The caller should ensure that error will be the exception that was raised by the route or None if the route returned normally.

If an implementation can not or does not want to handle route errors, then the method should re-raise the received exception. Example:

class MyComponentSelector:
    def get_component(self, request: Request, error: Exception | None) -> str:
        if error is not None:
            raise error

        ...

Raises:

Type Description
KeyError

If the component couldn't be identified.

Exception

The received error argument if it was not None and the implementation can not handle route errors.

Source code in fasthx/typing.py
def get_component(self, request: Request, error: Exception | None) -> Tco:
    """
    Returns the component that was requested by the client.

    The caller should ensure that `error` will be the exception that was raised by the
    route or `None` if the route returned normally.

    If an implementation can not or does not want to handle route errors, then the method
    should re-raise the received exception. Example:

    ```python
    class MyComponentSelector:
        def get_component(self, request: Request, error: Exception | None) -> str:
            if error is not None:
                raise error

            ...
    ```

    Raises:
        KeyError: If the component couldn't be identified.
        Exception: The received `error` argument if it was not `None` and the implementation
            can not handle route errors.
    """
    ...

SyncRenderFunction

Bases: Protocol[Tcontra]

Sync render function definition.

Source code in fasthx/typing.py
class SyncRenderFunction(Protocol[Tcontra]):
    """Sync render function definition."""

    def __call__(self, result: Tcontra, *, context: dict[str, Any], request: Request) -> str:
        """
        Arguments:
            result: The result of the route the renderer is used on.
            context: Every keyword argument the route received.
            request: The request being served.

        Returns:
            The rendered string.
        """
        ...

__call__(result, *, context, request)

Parameters:

Name Type Description Default
result Tcontra

The result of the route the renderer is used on.

required
context dict[str, Any]

Every keyword argument the route received.

required
request Request

The request being served.

required

Returns:

Type Description
str

The rendered string.

Source code in fasthx/typing.py
def __call__(self, result: Tcontra, *, context: dict[str, Any], request: Request) -> str:
    """
    Arguments:
        result: The result of the route the renderer is used on.
        context: Every keyword argument the route received.
        request: The request being served.

    Returns:
        The rendered string.
    """
    ...