A type that always evaluates to False in boolean context
Project description
falsetype
Installation
pip install falsetype
Functions
It provides a function FalseType to be a func's default value to pass type checking.
FalseType
FalseType(cls) 返回一个给定类型的实例,该实例在布尔转换时总是返回 False。
功能特点
- 类型保持:返回的对象是传入类型的子类,通过
isinstance()检查 - 布尔值恒为 False:在布尔上下文中总是返回
False - 通过类型检查:可以帮助函数默认参数通过 mypy 等类型检查工具的验证
使用示例
✅ 正面例子
1. 作为函数的默认参数(推荐用法)
当函数需要特定类型的默认参数,但该参数在布尔判断中应该为 "假" 时使用:
from falsetype import FalseType
# 正确:使用 FalseType 创建默认值
def process_list(items: list = FalseType(list)):
if not items: # 条件成立,因为 bool(FalseType(list)) == False
print("空列表")
return items
# 调用
process_list() # 输出:空列表
process_list([1, 2, 3]) # 正常处理列表
2. 通过 isinstance 检查
from falsetype import FalseType
obj = FalseType(str)
print(isinstance(obj, str)) # True - 保持了类型
print(bool(obj)) # False - 布尔值为假
print(type(obj).__name__) # 'str' - 类型名称保持一致
3. 适用于各种内置类型
from falsetype import FalseType
# 字符串
s = FalseType(str)
assert isinstance(s, str) and bool(s) is False
# 整数
i = FalseType(int)
assert isinstance(i, int) and bool(i) is False
# 列表
lst = FalseType(list)
assert isinstance(lst, list) and bool(lst) is False
# 字典
d = FalseType(dict)
assert isinstance(d, dict) and bool(d) is False
4. 在逻辑表达式中使用
from falsetype import FalseType
default_value = FalseType(str)
# 与运算
result = default_value and "other"
print(result) # 输出:FalseType 实例(因为第一个操作数为假)
# 或运算
result = default_value or "default"
print(result) # 输出:"default"(因为第一个操作数为假)
# if 语句
if default_value:
print("不会执行到这里")
else:
print("总是执行这里")
5. 替代 Optional 的更简洁写法
from typing import Optional
from falsetype import FalseType
# ❌ 不推荐:需要使用 Optional,且默认值为 None
def process_items_bad(items: Optional[list] = None):
if items is None:
items = []
return items
# ✅ 推荐:直接使用 FalseType,代码更简洁
def process_items_good(items: list = FalseType(list)):
if not items: # 直接判断,无需检查 None
return []
return items
❌ 反面例子
1. 不要直接用 None 作为强类型参数的默认值
# ❌ 错误:mypy 会报错
def accept_list(arg: list = None):
# mypy error: Incompatible types in assignment (expression has type "None", variable has type "list")
assert not arg
# ✅ 正确:使用 FalseType
def accept_list(arg: list = FalseType(list)):
# mypy 通过,运行时也为 False
assert not arg
2. 不要期望修改对象的属性或元素
由于 FalseType 返回的对象限制了属性和元素的设置,以下操作会抛出异常:
from falsetype import FalseType
obj = FalseType(list)
# ❌ 这些操作会抛出异常
try:
obj.append(1) # TypeError: Can't set items on FalseType
except TypeError as e:
print(e)
try:
obj.some_attr = "value" # AttributeError: Can't set attributes on FalseType
except AttributeError as e:
print(e)
设计原因:FalseType 的设计目的是作为一个"假值标记",而不是实际的数据容器。如果需要修改对象,应该传入真实的值。
3. 不要在需要真实数据的场景使用
# ❌ 错误用法:期望对 FalseType 返回的对象进行操作
def bad_usage():
items = FalseType(list)
items.append(1) # 会抛出异常!
return items
# ✅ 正确用法:作为默认参数,在实际使用时替换为真实值
def good_usage(items: list = FalseType(list)):
if not items:
return [] # 返回空列表
return items # 直接返回传入的列表
测试
运行测试确保功能正常:
pytest tests/test_bool.py -v
pytest tests/test_mypy.py -v
总结
FalseType 是一个用于创建"恒假"类型实例的工具函数,特别适合:
- ✅ 作为函数默认参数,通过类型检查
- ✅ 简化需要假值但又要保持类型的场景
- ✅ 避免
Optional类型的冗长检查
不适用于:
- ❌ 需要修改对象属性或元素的场景
- ❌ 需要真实数据的业务逻辑
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
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
File details
Details for the file falsetype-0.0.1.tar.gz.
File metadata
- Download URL: falsetype-0.0.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ca2b8ea09a387fc3bee0907e8b77faf559df4cd64fd2523eead6abccb970742
|
|
| MD5 |
c0adc37a1da08ceae0983f42515908f1
|
|
| BLAKE2b-256 |
054fd52c96ce198019b66a569c70444703b6e24377e26d90974f3cc399cf3559
|
File details
Details for the file falsetype-0.0.1-py3-none-any.whl.
File metadata
- Download URL: falsetype-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d37d48ced5424299d78ad074430839f9b1961269cd690fd1cfc709fa270ad9a6
|
|
| MD5 |
6f4c6743edd5737fd11c776f72cf4df0
|
|
| BLAKE2b-256 |
e47b974e68c60d7102a41c29e48e7f45a909789f22c2e7b1dd6a68431bd1ce16
|