0
# Boolean Types
1
2
Comparison classes for validating truthiness and falsiness with configurable behavior for different data types. These types provide flexible boolean validation including special handling for strings and customizable falsy value detection.
3
4
## Capabilities
5
6
### IsTrueLike
7
8
Checks if a value is truthy using Python's built-in `bool()` function. This follows standard Python truthiness rules where values like `True`, non-zero numbers, non-empty strings, and non-empty collections are considered truthy.
9
10
```python { .api }
11
class IsTrueLike(DirtyEquals):
12
"""
13
Checks if value is truthy using bool(other).
14
15
Uses Python's standard truthiness evaluation:
16
- True, non-zero numbers, non-empty strings/collections are truthy
17
- False, 0, None, empty strings/collections are falsy
18
"""
19
20
def equals(self, other: Any) -> bool:
21
"""
22
Check if the value is truthy.
23
24
Args:
25
other: Value to check for truthiness
26
27
Returns:
28
bool: True if bool(other) is True
29
"""
30
```
31
32
#### Usage Examples
33
34
```python
35
from dirty_equals import IsTrueLike
36
37
# Basic truthy values
38
assert True == IsTrueLike
39
assert 1 == IsTrueLike
40
assert -1 == IsTrueLike
41
assert 42 == IsTrueLike
42
assert "hello" == IsTrueLike
43
assert [1, 2, 3] == IsTrueLike
44
assert {"key": "value"} == IsTrueLike
45
46
# These would fail (falsy values)
47
# assert False == IsTrueLike # False
48
# assert 0 == IsTrueLike # False
49
# assert "" == IsTrueLike # False
50
# assert [] == IsTrueLike # False
51
# assert None == IsTrueLike # False
52
53
# In data validation
54
api_response = {
55
'success': True,
56
'data': [1, 2, 3],
57
'message': 'Operation completed'
58
}
59
60
assert api_response == {
61
'success': IsTrueLike, # Must be truthy
62
'data': IsTrueLike, # Must have data
63
'message': IsTrueLike # Must have message
64
}
65
66
# With boolean logic
67
from dirty_equals import IsStr
68
69
# Must be both truthy and a string
70
assert "hello" == (IsTrueLike & IsStr)
71
```
72
73
### IsFalseLike
74
75
Checks if a value is falsy, with optional special handling for strings. By default uses Python's standard falsy evaluation, but can be configured to treat certain strings as falsy beyond just empty strings.
76
77
```python { .api }
78
class IsFalseLike(DirtyEquals):
79
"""
80
Checks if value is falsy with optional string support.
81
82
By default uses Python's falsy evaluation (False, 0, None, empty collections).
83
With allow_strings=True, also treats certain strings as falsy.
84
"""
85
86
def __init__(self, *, allow_strings: bool = False):
87
"""
88
Initialize falsy checker.
89
90
Args:
91
allow_strings: If True, enables special string falsy checking
92
"""
93
94
@staticmethod
95
def make_string_check(other: str) -> bool:
96
"""
97
Determine if a string should be considered falsy.
98
99
Args:
100
other: String to evaluate
101
102
Returns:
103
bool: True if string should be considered falsy
104
"""
105
106
def equals(self, other: Any) -> bool:
107
"""
108
Check if the value is falsy.
109
110
Args:
111
other: Value to check for falsiness
112
113
Returns:
114
bool: True if value is considered falsy
115
"""
116
```
117
118
#### Usage Examples
119
120
```python
121
from dirty_equals import IsFalseLike
122
123
# Basic falsy values
124
assert False == IsFalseLike
125
assert 0 == IsFalseLike
126
assert 0.0 == IsFalseLike
127
assert "" == IsFalseLike
128
assert [] == IsFalseLike
129
assert {} == IsFalseLike
130
assert None == IsFalseLike
131
132
# These would fail (truthy values)
133
# assert True == IsFalseLike # False
134
# assert 1 == IsFalseLike # False
135
# assert "hello" == IsFalseLike # False
136
# assert [1] == IsFalseLike # False
137
138
# With string support enabled
139
falsy_with_strings = IsFalseLike(allow_strings=True)
140
141
# Standard falsy values still work
142
assert False == falsy_with_strings
143
assert 0 == falsy_with_strings
144
assert "" == falsy_with_strings
145
146
# Additional string values treated as falsy (implementation specific)
147
assert "false" == falsy_with_strings # Example - depends on implementation
148
assert "0" == falsy_with_strings # Example - depends on implementation
149
150
# In data validation
151
form_data = {
152
'required_field': "",
153
'optional_field': None,
154
'enabled': False
155
}
156
157
# Check for missing/empty required fields
158
assert form_data == {
159
'required_field': IsFalseLike, # Empty string is falsy
160
'optional_field': IsFalseLike, # None is falsy
161
'enabled': IsFalseLike # False is falsy
162
}
163
164
# Validating disabled states
165
settings = {
166
'notifications': False,
167
'auto_save': 0,
168
'theme': ""
169
}
170
171
# All should be disabled/empty
172
assert all(value == IsFalseLike for value in settings.values())
173
174
# With boolean combinations
175
from dirty_equals import IsStr
176
177
# Must be falsy but if it's a string, allow string falsy values
178
string_falsy = IsFalseLike(allow_strings=True)
179
assert "" == (IsFalseLike & IsStr) # Empty string is both falsy and string
180
```
181
182
## Type Definitions
183
184
```python { .api }
185
from typing import Any
186
187
# No additional type definitions needed for boolean types
188
# Both classes inherit from DirtyEquals and use standard Python types
189
```