fry-return
A structured return object for Python functions. Instead of returning bare dicts or raising exceptions for every non-happy path, wrap your result in a FryReturnObj — it carries a typed status code, a human-readable message, optional data payload, and a list of detail strings.
Installation
pip install fry-return
Quick start
from fry_return import FryReturnObj, FryReturnCode
Direct usage — data goes into data_dict
Simple but less IDE-friendly: you get no autocomplete on the payload fields.
def get_user(user_id: int) -> FryReturnObj:
user = db.find(user_id)
if user is None:
return FryReturnObj.failed("用户不存在")
return FryReturnObj.success("查询成功").set_data("user", user)
result = get_user(42)
if result.is_success:
print(result.data_dict["user"])
else:
print(result.message_str)
Inheritance — core data as typed attributes (recommended)
Define a subclass and add your payload as proper typed fields. Editors will autocomplete everything.
from dataclasses import dataclass, field
from fry_return import FryReturnObj, FryReturnCode
@dataclass
class GetUserResult(FryReturnObj):
user_id: int = 0
username: str = ""
email: str = ""
def get_user(user_id: int) -> GetUserResult:
user = db.find(user_id)
if user is None:
return GetUserResult(code=FryReturnCode.FAILED, message_str="用户不存在")
result = GetUserResult(code=FryReturnCode.SUCCESS, message_str="查询成功")
result.user_id = user.id
result.username = user.name
result.email = user.email
return result
result = get_user(42)
if result.is_success:
print(result.username) # 编辑器可以自动补全
print(result.email)
Chaining
result = (
FryReturnObj.info("开始处理")
.add_detail("步骤1完成")
.add_detail("步骤2完成")
.set_success("全部完成")
.set_data("count", 10)
)
Serialization
d = result.to_dict() # -> dict,可直接 JSON 序列化
r = FryReturnObj.from_dict(d) # 从 dict 还原
Status codes (FryReturnCode)
| 名称 | 值 | 说明 |
|---|---|---|
DEBUG |
100 | 调试信息 |
INFO |
200 | 普通信息 |
IMPORTANT |
300 | 重要信息 |
WARNING |
400 | 警告 |
VALIDATE |
500 | 验证失败 |
FAILED |
600 | 操作失败 |
BUG |
700 | 程序错误 |
SUCCESS |
800 | 完全成功 |
PARTIAL_SUCCESS |
820 | 部分成功 |
Key properties
| 属性 | 说明 |
|---|---|
is_success / is_full_success |
是否为 SUCCESS |
is_partial_success |
是否为 PARTIAL_SUCCESS |
code_name |
状态码名称字符串 |
code_value |
状态码数值 |
License
MIT
Release files for fry-return 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fry_return-0.1.0.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fry_return-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.4 kB
Release files / fry_return-0.1.0.tar.gz
| Download URL | fry_return-0.1.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
58f84496f93c2b342c01e1c95deb423dd0ba7f3c0f548fcfafd071eed5cda06f
|
|
BLAKE2b-256 checksum How to use checksums |
8412d19ea7fb072730fe97771d839bd183cf87f6e412b6a3cd3c253a9cb590c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / fry_return-0.1.0-py3-none-any.whl
| Download URL | fry_return-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5cf0889e168e8b20adbdb8b32aabb40a2d702de79b00661f7faf4507aa012478
|
|
BLAKE2b-256 checksum How to use checksums |
65237b0c411ff10e47ed0e527196fe2979e30b5b4f4e1e574e347163a489ae5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|