Skip to content

htmy.typing

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

Protocol definition for async function components.

Component: TypeAlias = ComponentType | ComponentSequence module-attribute

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

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

Component sequence type.

ComponentType: TypeAlias = HTMYComponentType | str module-attribute

Type definition for a single component.

Context: TypeAlias = Mapping[ContextKey, ContextValue] module-attribute

Context mapping.

ContextKey: TypeAlias = Any module-attribute

Context key.

ContextProvider: TypeAlias = SyncContextProvider | AsyncContextProvider module-attribute

Context provider type.

ContextValue: TypeAlias = Any module-attribute

Context value.

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

Function component type.

HTMYComponentType: TypeAlias = SyncComponent | AsyncComponent module-attribute

Sync or async htmy component type.

MutableContext: TypeAlias = 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: TypeAlias = Mapping[str, PropertyValue] module-attribute

Component/XML tag property mapping.

PropertyValue: TypeAlias = Any | None module-attribute

Component/XML tag property value.

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

Protocol definition for sync function components.

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 an HTMY context for child rendering."""
        ...

htmy_context() async

Returns an HTMY context for child rendering.

Source code in htmy/typing.py
async def htmy_context(self) -> Context:
    """Returns an HTMY 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 an HTMY context for child rendering."""
        ...

htmy_context()

Returns an HTMY context for child rendering.

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

is_component_sequence(obj)

Returns whether the given object is a component sequence.

Source code in htmy/typing.py
def is_component_sequence(obj: Any) -> TypeGuard[ComponentSequence]:
    """Returns whether the given object is a component sequence."""
    return isinstance(obj, (list, tuple))