Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/dev2forge/BasicReturns/llms.txt

Use this file to discover all available pages before exploring further.

DataAndMsgReturn extends BasicReturn with two additional fields — msg for a human-readable operation message and data for the result payload. It is the primary return type for functions that produce output.
from BasicReturns import DataAndMsgReturn

Class Signature

from typing import Any, Optional
from BasicReturns import BasicReturn

class DataAndMsgReturn(BasicReturn):
    msg: Optional[str] = None
    data: Optional[Any] = None

Inherited Fields

These fields are inherited from BasicReturn and behave identically.
ok
Optional[bool]
default:"True"
True when the operation completed successfully. Set to False on failure.
error
Optional[Any]
default:"None"
The exception or error object when ok is False. Defaults to None.

Additional Fields

msg
Optional[str]
default:"None"
A human-readable message describing the operation result. Recommended on both success and failure paths to make logs and error reports self-explanatory.
data
Optional[Any]
default:"None"
The result payload of the operation. Typically populated on success; left as None on failure.

Methods

to_dict()

Overrides BasicReturn.to_dict() to include all four fields.
def to_dict(self) -> dict:
    return {
        **super().to_dict(),
        "msg": self.msg,
        "data": self.data if self.data else {}
    }
Return value
ok
bool
Whether the operation succeeded.
error
Any or None
The error if ok is False, otherwise None.
msg
str or None
The human-readable operation message, or None if not set.
data
Any or {}
The result payload, or an empty dict {} if data is None.

__str__()

Returns a human-readable string representation of all four fields, formatted as:
OK: {ok}
Error: {error}
Message: {msg}
Data: {data}
Error: {error}

Constructor Shorthand

Because DataAndMsgReturn is a Pydantic model, all fields can be set directly at construction time instead of being assigned after instantiation.
result = DataAndMsgReturn(
    ok=False,
    error=ValueError("Bad input"),
    msg="Validation failed"
)

Full Usage Example

from BasicReturns import DataAndMsgReturn

def divide_numbers(a: float, b: float) -> DataAndMsgReturn:
    """Safely divide two numbers with unified return structure."""
    response = DataAndMsgReturn()

    try:
        if b == 0:
            raise ZeroDivisionError("Cannot divide by zero")
        response.data = a / b
        response.msg = "Division completed successfully"
    except Exception as e:
        response.ok = False
        response.error = e
        response.msg = "Division failed"

    return response

# Usage example
result = divide_numbers(10, 2)

if result.ok:
    print(f"{result.msg}: {result.data}")  # Division completed successfully: 5.0
else:
    print(f"{result.msg}: {result.error}")  # Division failed: Cannot divide by zero

to_dict() Output Examples

Success case
result = divide_numbers(10, 2)
print(result.to_dict())
# {'ok': True, 'error': None, 'msg': 'Division completed successfully', 'data': 5.0}
Failure case
result = divide_numbers(10, 0)
print(result.to_dict())
# {'ok': False, 'error': ZeroDivisionError('Cannot divide by zero'), 'msg': 'Division failed', 'data': {}}
Always set msg on both success and failure paths. A consistent message on every return makes logs scannable and error reports self-contained — you immediately know what the operation was and what happened, without needing to inspect data or error first.
See BasicReturn for the base class.

Build docs developers (and LLMs) love