|
63 | 63 | if TYPE_CHECKING: |
64 | 64 | from sqlalchemy.orm import InstrumentedAttribute |
65 | 65 |
|
| 66 | + FilterFieldName: TypeAlias = Union[str, ColumnElement[Any], InstrumentedAttribute[Any]] |
| 67 | + InstrumentedField: TypeAlias = Union[ColumnElement[Any], InstrumentedAttribute[Any]] |
| 68 | +else: |
| 69 | + FilterFieldName: TypeAlias = Any |
| 70 | + InstrumentedField: TypeAlias = Any |
| 71 | + |
66 | 72 | __all__ = ( |
67 | 73 | "BeforeAfter", |
68 | 74 | "CollectionFilter", |
@@ -160,9 +166,7 @@ def append_to_statement( |
160 | 166 | return statement |
161 | 167 |
|
162 | 168 | @staticmethod |
163 | | - def _get_instrumented_attr( |
164 | | - model: Any, key: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
165 | | - ) -> "Union[ColumnElement[Any], InstrumentedAttribute[Any]]": |
| 169 | + def _get_instrumented_attr(model: Any, key: FilterFieldName) -> InstrumentedField: |
166 | 170 | """Get SQLAlchemy instrumented attribute from model. |
167 | 171 |
|
168 | 172 | Args: |
@@ -193,7 +197,7 @@ class BeforeAfter(StatementFilter): |
193 | 197 |
|
194 | 198 | """ |
195 | 199 |
|
196 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 200 | + field_name: FilterFieldName |
197 | 201 | """Field name, model attribute, or func expression.""" |
198 | 202 | before: Optional[datetime.datetime] |
199 | 203 | """Filter results where field is earlier than this value.""" |
@@ -239,7 +243,7 @@ class OnBeforeAfter(StatementFilter): |
239 | 243 |
|
240 | 244 | """ |
241 | 245 |
|
242 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 246 | + field_name: FilterFieldName |
243 | 247 | """Field name, model attribute, or func expression.""" |
244 | 248 | on_or_before: Optional[datetime.datetime] |
245 | 249 | """Filter results where field is on or earlier than this value.""" |
@@ -287,7 +291,7 @@ class CollectionFilter(InAnyFilter, Generic[T]): |
287 | 291 | Use ``prefer_any=True`` in ``append_to_statement`` to use the ``ANY`` operator. |
288 | 292 | """ |
289 | 293 |
|
290 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 294 | + field_name: FilterFieldName |
291 | 295 | """Field name, model attribute, or func expression.""" |
292 | 296 | values: Union[Collection[T], None] |
293 | 297 | """Values for the ``IN`` clause. If this is None, no filter is applied. |
@@ -346,7 +350,7 @@ class NotInCollectionFilter(InAnyFilter, Generic[T]): |
346 | 350 |
|
347 | 351 | """ |
348 | 352 |
|
349 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 353 | + field_name: FilterFieldName |
350 | 354 | """Field name, model attribute, or func expression.""" |
351 | 355 | values: Union[Collection[T], None] |
352 | 356 | """Values for the ``NOT IN`` clause. If None or empty, no filter is applied.""" |
@@ -419,7 +423,7 @@ class NullFilter(StatementFilter): |
419 | 423 | - :meth:`sqlalchemy.sql.expression.ColumnOperators.is_`: IS NULL operator |
420 | 424 | """ |
421 | 425 |
|
422 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 426 | + field_name: FilterFieldName |
423 | 427 | """Field name, model attribute, or func expression.""" |
424 | 428 |
|
425 | 429 | def append_to_statement(self, statement: StatementTypeT, model: type[ModelT]) -> StatementTypeT: |
@@ -472,7 +476,7 @@ class NotNullFilter(StatementFilter): |
472 | 476 | - :meth:`sqlalchemy.sql.expression.ColumnOperators.is_not`: IS NOT NULL operator |
473 | 477 | """ |
474 | 478 |
|
475 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 479 | + field_name: FilterFieldName |
476 | 480 | """Field name, model attribute, or func expression.""" |
477 | 481 |
|
478 | 482 | def append_to_statement(self, statement: StatementTypeT, model: type[ModelT]) -> StatementTypeT: |
@@ -556,7 +560,7 @@ class OrderBy(StatementFilter): |
556 | 560 | - :meth:`sqlalchemy.sql.expression.ColumnElement.desc`: Descending order |
557 | 561 | """ |
558 | 562 |
|
559 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 563 | + field_name: FilterFieldName |
560 | 564 | """Field name, model attribute, or func expression (e.g., ``func.random()``).""" |
561 | 565 | sort_order: Literal["asc", "desc"] = "asc" |
562 | 566 | """Sort direction ("asc" or "desc").""" |
@@ -728,7 +732,7 @@ class ComparisonFilter(StatementFilter): |
728 | 732 | ValueError: If an invalid operator is provided |
729 | 733 | """ |
730 | 734 |
|
731 | | - field_name: "Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]" |
| 735 | + field_name: FilterFieldName |
732 | 736 | """Field name, model attribute, or func expression.""" |
733 | 737 | operator: str |
734 | 738 | """Comparison operator to use (one of 'eq', 'ne', 'gt', 'ge', 'lt', 'le').""" |
|
0 commit comments