Skip to content

Typing

fasthx.HTMLRenderer = SyncHTMLRenderer[Tcontra] | AsyncHTMLRenderer[Tcontra] module-attribute

Sync or async HTML renderer type.

fasthx.typing.SyncHTMLRenderer

Bases: Protocol[Tcontra]

Sync HTML renderer definition.

Source code in fasthx/typing.py
class SyncHTMLRenderer(Protocol[Tcontra]):
    """Sync HTML renderer definition."""

    def __call__(self, result: Tcontra, *, context: dict[str, Any], request: Request) -> str | Response:
        """
        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:
            HTML string (it will be automatically converted to `HTMLResponse`) or a `Response` object.
        """
        ...

__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 | Response

HTML string (it will be automatically converted to HTMLResponse) or a Response object.

Source code in fasthx/typing.py
def __call__(self, result: Tcontra, *, context: dict[str, Any], request: Request) -> str | Response:
    """
    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:
        HTML string (it will be automatically converted to `HTMLResponse`) or a `Response` object.
    """
    ...

fasthx.typing.AsyncHTMLRenderer

Bases: Protocol[Tcontra]

Async HTML renderer definition.

Source code in fasthx/typing.py
class AsyncHTMLRenderer(Protocol[Tcontra]):
    """Async HTML renderer definition."""

    async def __call__(
        self, result: Tcontra, *, context: dict[str, Any], request: Request
    ) -> str | Response:
        """
        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:
            HTML string (it will be automatically converted to `HTMLResponse`) or a `Response` object.
        """
        ...

__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 | Response

HTML string (it will be automatically converted to HTMLResponse) or a Response object.

Source code in fasthx/typing.py
async def __call__(
    self, result: Tcontra, *, context: dict[str, Any], request: Request
) -> str | Response:
    """
    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:
        HTML string (it will be automatically converted to `HTMLResponse`) or a `Response` object.
    """
    ...

fasthx.typing.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.
    """
    ...

fasthx.typing.ComponentSelector: TypeAlias = str | RequestComponentSelector[T] module-attribute

Type alias for known component selectors.