Skip to main content
DataAndMsgReturn extends BasicReturn with two additional fields — msg for a human-readable operation message and data for any result payload. It is the most commonly used return type in BasicReturns, covering the full range of function outcomes: success with results, success with a status message, or failure with a detailed error.

Import

Class Definition

Fields

Optional[bool]
default:"True"
Inherited from BasicReturn. True on success, False on failure. Set this to False in your except block so callers can check result.ok before accessing result.data.
Optional[Any]
default:"None"
Inherited from BasicReturn. The exception or error object captured on failure. Remains None when ok is True. Accepts any Python value — an Exception subclass, an error code string, or a custom error model.
Optional[str]
default:"None"
A human-readable message describing the outcome of the operation. Set on both success and failure paths. Useful for logging, toast notifications, or API response summaries — e.g. "User created successfully" or "Email address already in use".
Optional[Any]
default:"None"
The result payload. Can be any Python object — a dict, list, dataclass, Pydantic model, or primitive value. Typically set only on success; left as None on failure. When serialized via to_dict(), a None value is converted to an empty dict {} automatically.

Methods

to_dict()

Returns a plain Python dictionary that merges the parent BasicReturn.to_dict() output with the msg and data fields. If data is None it is serialized as {}, making the result immediately safe to pass to json.dumps() without a TypeError on None. Return type: dict

__str__()

Returns a human-readable, multi-line string that prints all four fields. Extends the parent BasicReturn.__str__() output with the msg and data lines. Return type: str
Format:

Complete Usage Example

The divide_numbers function below demonstrates the full BasicReturns pattern: a try/except block populates data and msg on success, and error and msg on failure. The caller checks result.ok before using result.data.

Inheritance

DataAndMsgReturn sits at the end of a three-level inheritance chain:
Every DataAndMsgReturn instance is a valid BasicReturn instance, so any code that accepts a BasicReturn will also accept a DataAndMsgReturn. This makes it straightforward to upgrade existing functions — swap the return type annotation from BasicReturn to DataAndMsgReturn and start populating msg and data without changing any caller that only checks result.ok. See the BasicReturn reference for full documentation of the base class and its fields.
data accepts any Python type — no serialization step is needed before assigning it. Pass a raw dict, a list, a Pydantic model, a dataclass, or even a primitive. Only call to_dict() when you are ready to hand the value off to a serialization boundary such as a JSON encoder or an HTTP response helper.
Call to_dict() at the API boundary to get a JSON-serializable dict that is ready for json.dumps() or a framework response helper such as Flask’s jsonify() or FastAPI’s JSONResponse: