A singleton sentinel value Unset for distinguishing unset from None in Python.
Project description
unset
一个用于区分“未设置/未提供”与“显式提供 None/False”的 Python 单例哨兵值:Unset。
特性
- 单例实现:全局仅一个实例
Unset - 语义清晰:
repr(Unset) -> "Unset",bool(Unset) -> False - 便捷检查:
Unset(value)等价于value is Unset - 复制与深拷贝安全:
copy(Unset)与deepcopy(Unset)都返回自身 - Pydantic v2 友好:内置
pydantic-coreschema 支持(可与pydantic>=2协同)
安装
python -m pip install --upgrade unset
依赖:pydantic-core>=2,<3。
快速示例
from unset import Unset
def update_user(email=Unset):
if email is Unset:
return "no change" # 调用方未传入 email
if email is None:
return "clear" # 调用方显式传入 None
return f"set to {email}"
assert bool(Unset) is False
assert Unset(Unset) is True # 等价于 `Unset is Unset`
与 None 的区别
Unset表示“未提供/未设置”。None表示“显式提供空值”。
这在“部分更新接口”、“差异合并逻辑”、“默认参数”场景中非常有用。
与 Pydantic v2 集成(可选)
若安装了 pydantic>=2,可以直接在模型中使用 Unset:
from pydantic import BaseModel
from unset import Unset, UnsetType
class PatchUser(BaseModel):
email: UnsetType | str | None = Unset
payload = PatchUser() # email == Unset
print(payload.model_dump_json()) # {"email": null} (默认:Unset -> null)
# 省略策略(根据你的语义选择其一)
print(payload.model_dump_json(exclude_none=True)) # {}
print(payload.model_dump_json(exclude_unset=True)) # {}
print(payload.model_dump_json(exclude_defaults=True)) # {}
注意:本项目仅依赖 pydantic-core;如需使用 BaseModel,请额外安装 pydantic>=2。
JSON 序列化行为与开关
- 默认行为:
Unset在 JSON 中序列化为null。- 这样可配合
exclude_none=True在序列化时省略该字段。 - 也可以使用
exclude_unset=True或exclude_defaults=True实现字段省略(当默认值为Unset或未显式提供时)。
- 这样可配合
- 可配置开关:通过环境变量
UNSET_JSON_SERIALIZE_AS_NONE控制行为。- 为真(默认:
1/true)→Unset -> null - 为假(
0/false)→Unset -> "<Unset>"(字符串)
- 为真(默认:
export UNSET_JSON_SERIALIZE_AS_NONE=1 # 或 true:Unset -> null(默认)
export UNSET_JSON_SERIALIZE_AS_NONE=0 # 或 false:Unset -> "<Unset>"
许可
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
unset-0.1.1.tar.gz
(2.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
unset-0.1.1-py3-none-any.whl
(2.9 kB
view details)
File details
Details for the file unset-0.1.1.tar.gz.
File metadata
- Download URL: unset-0.1.1.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
010814053def55c63825f4e3be7efbc33883ec083e02c2174b3b944abc6e13c0
|
|
| MD5 |
47eb3550248587059fe71504fde10e2f
|
|
| BLAKE2b-256 |
a2813080e1c5bd407aabcd4e93df628acfbdc4eb1e5419fee188eaaf41fdb223
|
File details
Details for the file unset-0.1.1-py3-none-any.whl.
File metadata
- Download URL: unset-0.1.1-py3-none-any.whl
- Upload date:
- Size: 2.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e36a1e68fa3d0a9dd97c8d88670f8817a5d733a7bf6751f46f98c1935b195d43
|
|
| MD5 |
71012ac97312ec0ca09bbb5b385ff1e1
|
|
| BLAKE2b-256 |
c2d6b2b4f1c2041b280a1cd38b1bdafa9b334c7dd7a16dea72ac510bbd9c0031
|