Filters¶
Filter builder for V2 API filtering support.
Provides a type-safe, Pythonic way to build filter expressions for V2 list endpoints. The builder handles proper escaping and quoting of user inputs.
Example
from affinity.filters import Filter, F
Using the builder (recommended)¶
filter = ( F.field("name").contains("Acme") & F.field("status").equals("Active") ) companies = client.companies.list(filter=filter)
Or build complex filters¶
filter = ( (F.field("name").contains("Corp") | F.field("name").contains("Inc")) & ~F.field("archived").equals(True) )
Raw filter string escape hatch (power users)¶
companies = client.companies.list(filter='name =~ "Acme"')
AndExpression
dataclass
¶
Bases: FilterExpression
& combination of two expressions.
Source code in affinity/filters.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
matches(entity: dict[str, Any]) -> bool
¶
Both sides must match.
Source code in affinity/filters.py
233 234 235 | |
FieldBuilder
¶
Builder for field-based filter expressions.
Source code in affinity/filters.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
contains(value: str) -> FieldComparison
¶
Field contains substring (case-insensitive).
Source code in affinity/filters.py
283 284 285 | |
ends_with(value: str) -> FieldComparison
¶
Field ends with suffix.
Source code in affinity/filters.py
291 292 293 | |
equals(value: Any) -> FieldComparison
¶
Field equals value (exact match).
Source code in affinity/filters.py
275 276 277 | |
greater_than(value: int | float | datetime | date) -> FieldComparison
¶
Field is greater than value.
Source code in affinity/filters.py
295 296 297 | |
greater_than_or_equal(value: int | float | datetime | date) -> FieldComparison
¶
Field is greater than or equal to value.
Source code in affinity/filters.py
299 300 301 | |
in_list(values: list[Any]) -> FilterExpression
¶
Field value is in the given list (OR of equals).
Source code in affinity/filters.py
319 320 321 322 323 324 325 326 327 | |
is_not_null() -> FieldComparison
¶
Field is not null.
Source code in affinity/filters.py
315 316 317 | |
is_null() -> FieldComparison
¶
Field is null.
Source code in affinity/filters.py
311 312 313 | |
less_than(value: int | float | datetime | date) -> FieldComparison
¶
Field is less than value.
Source code in affinity/filters.py
303 304 305 | |
less_than_or_equal(value: int | float | datetime | date) -> FieldComparison
¶
Field is less than or equal to value.
Source code in affinity/filters.py
307 308 309 | |
not_equals(value: Any) -> FieldComparison
¶
Field does not equal value.
Source code in affinity/filters.py
279 280 281 | |
starts_with(value: str) -> FieldComparison
¶
Field starts with prefix.
Source code in affinity/filters.py
287 288 289 | |
FieldComparison
dataclass
¶
Bases: FilterExpression
A comparison operation on a field.
Source code in affinity/filters.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
matches(entity: dict[str, Any]) -> bool
¶
Evaluate field comparison against an entity dict.
For multi-select dropdown fields (arrays), the operators have special semantics:
- = with scalar: checks if value is IN the array (membership)
- = with list: checks set equality (order-insensitive)
- != with scalar: checks if value is NOT in the array
- != with list: checks set inequality
- =~ (contains): checks if any array element contains the substring
- =^ (starts_with): checks if any array element starts with the prefix
- =$ (ends_with): checks if any array element ends with the suffix
- >, >=, <, <=: numeric/date comparisons
Uses the shared compare module for consistent behavior across SDK and Query tool.
Source code in affinity/filters.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
Filter
¶
Factory for building filter expressions.
Example
Simple comparison¶
Filter.field("name").contains("Acme")
Complex boolean logic¶
(Filter.field("status").equals("Active") & Filter.field("type").in_list(["customer", "prospect"]))
Negation¶
~Filter.field("archived").equals(True)
Source code in affinity/filters.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
and_(*expressions: FilterExpression) -> FilterExpression
staticmethod
¶
Combine multiple expressions with &.
Source code in affinity/filters.py
364 365 366 367 368 369 370 371 372 | |
field(name: str) -> FieldBuilder
staticmethod
¶
Start building a filter on a field.
Source code in affinity/filters.py
346 347 348 349 | |
or_(*expressions: FilterExpression) -> FilterExpression
staticmethod
¶
Combine multiple expressions with |.
Source code in affinity/filters.py
374 375 376 377 378 379 380 381 382 | |
raw(expression: str) -> RawFilter
staticmethod
¶
Create a raw filter expression (escape hatch).
Use this when you need filter syntax not supported by the builder. The expression is passed directly to the API without modification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
Raw filter string (e.g., 'name =~ "Acme"') |
required |
Source code in affinity/filters.py
351 352 353 354 355 356 357 358 359 360 361 362 | |
FilterExpression
¶
Bases: ABC
Base class for filter expressions.
Source code in affinity/filters.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
__and__(other: FilterExpression) -> FilterExpression
¶
Combine two expressions with &.
Source code in affinity/filters.py
127 128 129 | |
__invert__() -> FilterExpression
¶
Negate the expression with !.
Source code in affinity/filters.py
135 136 137 | |
__or__(other: FilterExpression) -> FilterExpression
¶
Combine two expressions with |.
Source code in affinity/filters.py
131 132 133 | |
matches(entity: dict[str, Any]) -> bool
abstractmethod
¶
Evaluate filter against an entity dict (client-side).
Used for --expand-filter in list export where filtering happens after fetching data from the API.
Source code in affinity/filters.py
117 118 119 120 121 122 123 124 125 | |
to_string() -> str
abstractmethod
¶
Convert the expression to a filter string.
Source code in affinity/filters.py
112 113 114 115 | |
NotExpression
dataclass
¶
Bases: FilterExpression
! negation of an expression.
Source code in affinity/filters.py
255 256 257 258 259 260 261 262 263 264 265 266 | |
matches(entity: dict[str, Any]) -> bool
¶
Invert the inner expression.
Source code in affinity/filters.py
264 265 266 | |
OrExpression
dataclass
¶
Bases: FilterExpression
| combination of two expressions.
Source code in affinity/filters.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
matches(entity: dict[str, Any]) -> bool
¶
Either side must match.
Source code in affinity/filters.py
250 251 252 | |
RawFilter
dataclass
¶
Bases: FilterExpression
A raw filter string (escape hatch for power users).
Source code in affinity/filters.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
matches(entity: dict[str, Any]) -> bool
¶
RawFilter cannot be evaluated client-side.
Source code in affinity/filters.py
212 213 214 215 216 217 | |
RawToken
dataclass
¶
A raw token inserted into a filter expression without quoting.
Used for special Affinity Filtering Language literals like *.
Source code in affinity/filters.py
38 39 40 41 42 43 44 45 46 | |
parse(filter_string: str) -> FilterExpression
¶
Parse a filter string into a FilterExpression AST.
This function converts a human-readable filter string into a structured FilterExpression that can be used for client-side filtering with matches().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filter_string
|
str
|
The filter expression to parse |
required |
Returns:
| Type | Description |
|---|---|
FilterExpression
|
A FilterExpression AST representing the filter |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the filter string is invalid |
Examples:
>>> expr = parse('name = "Alice"')
>>> expr.matches({"name": "Alice"})
True
>>> expr = parse('status = Active | status = Pending')
>>> expr.matches({"status": "Active"})
True
>>> expr = parse('email = *') # IS NOT NULL
>>> expr.matches({"email": "test@example.com"})
True
>>> expr = parse('email != *') # IS NULL
>>> expr.matches({"email": None})
True
Source code in affinity/filters.py
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 | |