Skip to content

htmy.typing

AsyncFunctionComponent = Callable[[T, Context], Coroutine[Any, Any, Component]] module-attribute

Protocol definition for async function components.

Component = ComponentType | ComponentSequence module-attribute

Component type: a single component or a sequence of components.

ComponentSequence = list[ComponentType] | tuple[ComponentType, ...] module-attribute

Component sequence type.

ComponentType = HTMYComponentType | str module-attribute

Type definition for a single component.

Context = Mapping[ContextKey, ContextValue] module-attribute

Context mapping.

ContextKey = Any module-attribute

Context key.

ContextProvider = SyncContextProvider | AsyncContextProvider module-attribute

Context provider type.

ContextValue = Any module-attribute

Context value.

FunctionComponent = SyncFunctionComponent[T] | AsyncFunctionComponent[T] module-attribute

Function component type.

HTMYComponentType = SyncComponent | AsyncComponent module-attribute

Sync or async htmy component type.

MutableContext = MutableMapping[ContextKey, ContextValue] module-attribute

Mutable context mapping.

It can be helpful when the created context should be marked as mutable for static type analysis (usually the created context is a plain dict).

Properties = Mapping[str, PropertyValue] module-attribute

Component/XML tag property mapping.

PropertyValue = Any | None module-attribute

Component/XML tag property value.

SyncFunctionComponent = Callable[[T, Context], Component] module-attribute

Protocol definition for sync function components.

TextProcessor = Callable[[str, Context], str | Coroutine[Any, Any, str]] module-attribute

Callable type that expects a string and a context, and returns a processed string.

AsyncComponent

Bases: Protocol

Protocol definition for async htmy components.

Source code in htmy/typing.py
@runtime_checkable
class AsyncComponent(Protocol):
    """Protocol definition for async `htmy` components."""

    async def htmy(self, context: Context, /) -> "Component":
        """Renders the component."""
        ...

htmy(context) async

Renders the component.

Source code in htmy/typing.py
async def htmy(self, context: Context, /) -> "Component":
    """Renders the component."""
    ...

AsyncContextProvider

Bases: Protocol

Protocol definition for async context providers.

Source code in htmy/typing.py
@runtime_checkable
class AsyncContextProvider(Protocol):
    """Protocol definition for async context providers."""

    async def htmy_context(self) -> Context:
        """Returns a context for child rendering."""
        ...

htmy_context() async

Returns a context for child rendering.

Source code in htmy/typing.py
async def htmy_context(self) -> Context:
    """Returns a context for child rendering."""
    ...

SyncComponent

Bases: Protocol

Protocol definition for sync htmy components.

Source code in htmy/typing.py
@runtime_checkable
class SyncComponent(Protocol):
    """Protocol definition for sync `htmy` components."""

    def htmy(self, context: Context, /) -> "Component":
        """Renders the component."""
        ...

htmy(context)

Renders the component.

Source code in htmy/typing.py
def htmy(self, context: Context, /) -> "Component":
    """Renders the component."""
    ...

SyncContextProvider

Bases: Protocol

Protocol definition for sync context providers.

Source code in htmy/typing.py
@runtime_checkable
class SyncContextProvider(Protocol):
    """Protocol definition for sync context providers."""

    def htmy_context(self) -> Context:
        """Returns a context for child rendering."""
        ...

htmy_context()

Returns a context for child rendering.

Source code in htmy/typing.py
def htmy_context(self) -> Context:
    """Returns a context for child rendering."""
    ...

TextResolver

Bases: Protocol

Protocol definition for resolvers that convert a string to a component.

Source code in htmy/typing.py
class TextResolver(Protocol):
    """
    Protocol definition for resolvers that convert a string to a component.
    """

    def resolve_text(self, text: str) -> Component:
        """
        Returns the resolved component for the given text.

        Arguments:
            text: The text to resolve.

        Raises:
            KeyError: If the text cannot be resolved to a component.
        """
        ...

resolve_text(text)

Returns the resolved component for the given text.

Parameters:

Name Type Description Default
text str

The text to resolve.

required

Raises:

Type Description
KeyError

If the text cannot be resolved to a component.

Source code in htmy/typing.py
def resolve_text(self, text: str) -> Component:
    """
    Returns the resolved component for the given text.

    Arguments:
        text: The text to resolve.

    Raises:
        KeyError: If the text cannot be resolved to a component.
    """
    ...