pymediate
API Reference

Request

API reference for the Request base class.

from pymediate import Request

class Request[ResponseT]: ...

Request is the base class for messages sent with Mediator.send(). Its type parameter declares the response type, allowing a type checker to infer the result of send().

Type parameterMeaning
ResponseTThe type returned by the request's handler

PyMediate records this relationship when Python defines the request class. It later uses the recorded type to validate the handler's return annotation. It does not inspect the value returned by the handler at dispatch time.

Example

from dataclasses import dataclass

from pymediate import Request


@dataclass(frozen=True)
class OrderReceipt:
    order_id: int
    summary: str


@dataclass(frozen=True)
class PlaceOrder(Request[OrderReceipt]):
    customer_id: int
    item: str
    quantity: int

Read PlaceOrder(Request[OrderReceipt]) as “PlaceOrder is a request for an OrderReceipt.” The response type is an ordinary Python type; it does not inherit from PyMediate.

Define request subclasses with a type argument. A bare subclass such as class PlaceOrder(Request): ... has no recorded response type and cannot be paired with a valid RequestHandler.

See also

On this page