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
注意:本项目仅依赖 pydantic-core;如需使用 BaseModel,请额外安装 pydantic>=2。
许可
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.0.tar.gz
(2.4 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.0-py3-none-any.whl
(2.3 kB
view details)
File details
Details for the file unset-0.1.0.tar.gz.
File metadata
- Download URL: unset-0.1.0.tar.gz
- Upload date:
- Size: 2.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c195b1531bea447c084017e726277e554733a75e18ab9e410ee8c182fd13c732
|
|
| MD5 |
af96a0efb3acc5d5703a35bd27249bf2
|
|
| BLAKE2b-256 |
5cb0a0dabb77173d3acc1645af56ce7209d010000d37207778af2410bec53016
|
File details
Details for the file unset-0.1.0-py3-none-any.whl.
File metadata
- Download URL: unset-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.3 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 |
6d0e352fc53c7fa99c3564dd26e3e5449763bb2b9b4bfa1d0099d2d13623f11d
|
|
| MD5 |
8685e4379f62b1f11d8cf8b7a00913b6
|
|
| BLAKE2b-256 |
bdabfdd5a40384b01d7b9887e021c80a4f62dd7d6f1cf7f42272b81a832a05d6
|