Skip to content

redsun.injection

Sharing and asking

provides

provides(method: F) -> F

Share a method's return value under the type it is annotated with.

class MotorPresenter:
    @provides
    def readings(self) -> MotorReadings:
        return MotorReadings(...)

Anything asking for MotorReadings in its setup receives the result of calling this method on the built component. The return annotation is the key, so it must be distinct across the application.

DevicesOf module-attribute

DevicesOf: TypeAlias = Annotated[Mapping[str, P], Devices()]

Every device of the session that satisfies P, by name.

class MotorProtocol(Protocol):
    async def set(self, value: float) -> None: ...


class MotorPresenter:
    def __init__(self, name: str, *, motors: DevicesOf[MotorProtocol]) -> None:
        self._motors = motors

Each device of the session, and each device below it, is checked on its own, so a root device and the devices it holds can all match. Devices are built before any presenter or view, so the mapping arrives complete and may be read while the component is built. Ask for redsun.DeviceMapping instead to receive every device, unfiltered.

Asking the session

satisfies

satisfies(candidate: type, protocol: TypeForm[P]) -> bool
satisfies(
    candidate: object, protocol: TypeForm[P]
) -> TypeIs[P]
satisfies(candidate: object, protocol: object) -> bool

Whether candidate satisfies protocol.

An instance that does is narrowed to protocol for a type checker; a class is not, since it is not an instance of the protocol it satisfies.

satisfying

satisfying(
    components: Mapping[str, object], protocol: TypeForm[P]
) -> dict[str, P]

Return the components of components that satisfy protocol, by name.

rejected

rejected(
    components: Mapping[str, Any], protocol: type
) -> dict[str, list[str]]

Return why each component that nearly matched protocol was left out.

Only components carrying some of the protocol's members appear, so a component missing all of them does not drown out a near miss.

The registry

Registry

Registry(ask: Ask | None = None)

What one session provides, one key to one value.

A registry belongs to one session. A key holds one value, and every lookup matches its key exactly: a subclass is not answered by a value registered under its base class, and a union is not answered by a value registered under one of its members, apart from X | None, which X answers. ask answers a key nobody registered, each time a provider for it is called.

register

register(
    key: object,
    factory: Callable[[], object],
    *,
    owner: str,
) -> None

Register factory under key, called on every request.

Raises:

Type Description
RegistrationError

If key is registered already, metadata diwire ignores aside.

register_resource

register_resource(
    key: object,
    provider: Callable[[], Generator[Any, None, None]],
    *,
    owner: str,
    internal: bool = False,
) -> None

Register the generator provider under key as a resource.

The resource is made on its first request, shared by every request after it, and torn down by close, the last made first. An internal key is refused to call.

Raises:

Type Description
RegistrationError

If key is registered already, metadata diwire ignores aside.

close

close() -> None

Tear down the resources made, the last made first.

A closed registry refuses a request for a resource. Closing twice does nothing.

providers

providers(key: object) -> Iterator[Callable[[], object]]

Yield a provider of key, or none when nothing answers it.

call

call(fn: Callable[..., R], /, **passed: object) -> R

Call fn, filling each parameter not in passed from the keys held.

A parameter with a default keeps it when nothing answers. One written X | None, or a union with None among its members, receives None then. A None from a held key does not answer a required parameter.

Raises:

Type Description
TypeError

If a required parameter has no value, naming each, or a parameter asks for an internal key.

RuntimeError

If the registry is closed and a parameter asks for a resource.