INNER CODE UNIT · Python
from_context
volfpeter/htmy · htmy/core.py:139
def from_context(cls, context: Context, default: Self) -> Self: ...
@classmethod
def from_context(cls, context: Context, default: Self | None = _MISSING) -> Self | None:
"""
Looks up an instance of this class from the given context.
Arguments:
context: The context the instance should be loaded from.
default: The default to use if no instance is found in the context. If omitted,
a `KeyError` is raised on absence.
Raises:
KeyError: If no instance was found and no `default` was given.
TypeError: If the value in the context is not an instance of this class.
"""
result = context[cls] if default is _MISSING else context.get(cls, default)
if isinstance(result, cls):