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
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
matches(entity: dict[str, Any]) -> bool
¶
Both sides must match.
Source code in affinity/filters.py
211 212 213 | |
FieldBuilder
¶
Builder for field-based filter expressions.
Source code in affinity/filters.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 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 | |
contains(value: str) -> FieldComparison
¶
Field contains substring (case-insensitive).
Source code in affinity/filters.py
261 262 263 | |
ends_with(value: str) -> FieldComparison
¶
Field ends with suffix.
Source code in affinity/filters.py
269 270 271 | |
equals(value: Any) -> FieldComparison
¶
Field equals value (exact match).
Source code in affinity/filters.py
253 254 255 | |
greater_than(value: int | float | datetime | date) -> FieldComparison
¶
Field is greater than value.
Source code in affinity/filters.py
273 274 275 | |
greater_than_or_equal(value: int | float | datetime | date) -> FieldComparison
¶
Field is greater than or equal to value.
Source code in affinity/filters.py
277 278 279 | |
in_list(values: list[Any]) -> FilterExpression
¶
Field value is in the given list (OR of equals).
Source code in affinity/filters.py
297 298 299 300 301 302 303 304 305 | |
is_not_null() -> FieldComparison
¶
Field is not null.
Source code in affinity/filters.py
293 294 295 | |
is_null() -> FieldComparison
¶
Field is null.
Source code in affinity/filters.py
289 290 291 | |
less_than(value: int | float | datetime | date) -> FieldComparison
¶
Field is less than value.
Source code in affinity/filters.py
281 282 283 | |
less_than_or_equal(value: int | float | datetime | date) -> FieldComparison
¶
Field is less than or equal to value.
Source code in affinity/filters.py
285 286 287 | |
not_equals(value: Any) -> FieldComparison
¶
Field does not equal value.
Source code in affinity/filters.py
257 258 259 | |
starts_with(value: str) -> FieldComparison
¶
Field starts with prefix.
Source code in affinity/filters.py
265 266 267 | |
FieldComparison
dataclass
¶
Bases: FilterExpression
A comparison operation on a field.
Source code in affinity/filters.py
144 145 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 | |
matches(entity: dict[str, Any]) -> bool
¶
Evaluate field comparison against an entity dict.
Source code in affinity/filters.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
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
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 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 | |
and_(*expressions: FilterExpression) -> FilterExpression
staticmethod
¶
Combine multiple expressions with &.
Source code in affinity/filters.py
342 343 344 345 346 347 348 349 350 | |
field(name: str) -> FieldBuilder
staticmethod
¶
Start building a filter on a field.
Source code in affinity/filters.py
324 325 326 327 | |
or_(*expressions: FilterExpression) -> FilterExpression
staticmethod
¶
Combine multiple expressions with |.
Source code in affinity/filters.py
352 353 354 355 356 357 358 359 360 | |
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
329 330 331 332 333 334 335 336 337 338 339 340 | |
FilterExpression
¶
Bases: ABC
Base class for filter expressions.
Source code in affinity/filters.py
107 108 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 | |
__and__(other: FilterExpression) -> FilterExpression
¶
Combine two expressions with &.
Source code in affinity/filters.py
125 126 127 | |
__invert__() -> FilterExpression
¶
Negate the expression with !.
Source code in affinity/filters.py
133 134 135 | |
__or__(other: FilterExpression) -> FilterExpression
¶
Combine two expressions with |.
Source code in affinity/filters.py
129 130 131 | |
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
115 116 117 118 119 120 121 122 123 | |
to_string() -> str
abstractmethod
¶
Convert the expression to a filter string.
Source code in affinity/filters.py
110 111 112 113 | |
NotExpression
dataclass
¶
Bases: FilterExpression
! negation of an expression.
Source code in affinity/filters.py
233 234 235 236 237 238 239 240 241 242 243 244 | |
matches(entity: dict[str, Any]) -> bool
¶
Invert the inner expression.
Source code in affinity/filters.py
242 243 244 | |
OrExpression
dataclass
¶
Bases: FilterExpression
| combination of two expressions.
Source code in affinity/filters.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
matches(entity: dict[str, Any]) -> bool
¶
Either side must match.
Source code in affinity/filters.py
228 229 230 | |
RawFilter
dataclass
¶
Bases: FilterExpression
A raw filter string (escape hatch for power users).
Source code in affinity/filters.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
matches(entity: dict[str, Any]) -> bool
¶
RawFilter cannot be evaluated client-side.
Source code in affinity/filters.py
190 191 192 193 194 195 | |
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
36 37 38 39 40 41 42 43 44 | |
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
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |