BasicReturn is the foundation of the BasicReturns library. It is a Pydantic v2 BaseModel subclass that represents the outcome of any operation — success or failure — in a consistent, type-safe structure. Every function that uses BasicReturns returns at minimum an instance of BasicReturn, giving callers a predictable interface regardless of what the function does internally.
Import
Class Definition
Fields
Optional[bool]
default:"True"
True when the operation succeeded; set to False on failure. Mirrors the
semantics of an HTTP success response — a truthy value signals the caller that
the operation completed without error.Optional[Any]
default:"None"
Holds the exception or error object when
ok is False. Remains None on
success. Accepts any Python value — an Exception subclass, a string
message, or any custom error object.Methods
to_dict()
Returns a plain Python dictionary representation of the instance. This is
useful at serialization boundaries — for example, before passing the return
value to json.dumps() or a web framework response helper.
Return type: dict
__str__()
Returns a human-readable, multi-line string representation of the instance.
Handy for logging and debugging.
Return type: strFormat:
OK: {ok}\nError: {error}
Usage Example
The canonical pattern is to wrap the body of a function in atry/except block
and return a BasicReturn in both branches. Callers can then check result.ok
before proceeding.
When to Use BasicReturn vs DataAndMsgReturn
Use
BasicReturn when the operation’s only meaningful outcome is whether it
succeeded or failed — there is no data to return to the caller. If you find
yourself stuffing results into the error field on success, that is a clear
signal to switch to DataAndMsgReturn instead.
BasicReturn extends Pydantic’s BaseModel, so you can construct it with
keyword arguments just like any other Pydantic model: