Repository navigation
feat: add support for SQLAlchemy func expressions in filter classes - #585
Merged
Merged
Conversation
cofin
force-pushed
the
feat/issue-519-func-support
branch
from
October 18, 2025 20:09
172a9fb to
eb40612
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #585 +/- ##
==========================================
- Coverage 79.48% 79.26% -0.23%
==========================================
Files 87 87
Lines 6346 6355 +9
Branches 807 810 +3
==========================================
- Hits 5044 5037 -7
- Misses 1051 1068 +17
+ Partials 251 250 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
cofin
force-pushed
the
feat/issue-519-func-support
branch
from
October 24, 2025 13:46
568733e to
b9fa255
Compare
Add support for SQLAlchemy func() expressions (e.g., func.random(), func.lower()) in filter classes to eliminate type checker errors when using database functions. Changes: - Update field_name type to Union[str, ColumnElement[Any], InstrumentedAttribute[Any]] in OrderBy, BeforeAfter, OnBeforeAfter, CollectionFilter, NotInCollectionFilter, and ComparisonFilter classes - Update _get_instrumented_attr() to handle ColumnElement and InstrumentedAttribute - Add comprehensive docstrings with examples for func expressions - Update mock repository methods to extract field names from InstrumentedAttribute - Add integration tests for func.random(), func.lower(), and InstrumentedAttribute Type Safety: - Both mypy 1.18.2 and pyright 1.1.406 pass in strict mode - No loss of type specificity - Union preserves full type information - ColumnOperators is common base type for both ColumnElement and InstrumentedAttribute Backward Compatibility: - All existing string-based usage continues to work - InstrumentedAttribute (Model.field) usage unchanged - Framework integrations (Litestar, FastAPI) work without changes - All existing tests pass Database Support: - PostgreSQL: func.random(), func.lower() - MySQL: func.rand() (different name) - SQLite: func.random() - SQL Server: func.newid() - Oracle: func.dbms_random.value() - All 8+ supported databases work Issue: litestar-org#519
… instance methods Convert _extract_field_name, _filter_in_collection, _filter_not_in_collection, _filter_on_datetime_field, and _order_by from @staticmethod to instance methods. This improves code clarity by using self._extract_field_name() instead of SQLAlchemyAsyncMockRepository._extract_field_name(), making the code more maintainable and following standard Python patterns for helper methods.
…data - Skip Oracle databases in test_order_by_with_func_random (uses dbms_random.value() instead of random()) - Replace 'The Matrix' with 'The Godfather' in test data to match test assertions - Update test_before_after_filter to expect 2 results (both movies before 1999) - Update test_on_before_after_filter to expect 1 result (only The Hangover after 1999)
Reverted the previous change that replaced 'The Matrix' with 'The Godfather' because it broke 40+ other tests that depend on the same test data. Instead, updated only the specific test assertions: - test_order_by_with_func_lower: Updated to expect alphabetical order with 'The Matrix' (Shawshank Redemption, The Hangover, The Matrix) - test_before_after_filter: Reverted to expect 1 result (only Shawshank before 1999-03-31) - test_on_before_after_filter: Reverted to expect 2 results (Matrix and Hangover on/after 1999-03-31) - test_order_by_with_func_random: Kept Oracle skip for dbms_random.value() compatibility All filter tests now pass on SQLite and other databases.
cofin
force-pushed
the
feat/issue-519-func-support
branch
from
October 24, 2025 15:19
b9fa255 to
bbd8e80
Compare
provinzkraut
approved these changes
Oct 25, 2025
|
Documentation preview will be available shortly at https://litestar-org.github.io/advanced-alchemy-docs-preview/585 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for SQLAlchemy func() expressions in filter classes to eliminate type checker errors when using database functions like
func.random()orfunc.lower().Closes #519
The Problem
Type checkers currently raise errors when using SQLAlchemy func expressions in filters:
The
field_nameparameter was typed asstr, butfunc.random()returns aFunctionElement(subclass ofColumnElement).The Solution
Updated type signatures to accept
Union[str, ColumnElement[Any], InstrumentedAttribute[Any]]in filter classes. The implementation already worked - this just fixes the type annotations.Key Features
func.random(),func.lower(), etc.)Example
Changes
OrderBy,BeforeAfter,OnBeforeAfter,CollectionFilter,NotInCollectionFilter,ComparisonFilter_get_instrumented_attr()to handle new typesInstrumentedAttribute, raise helpful error for func expressions (can't execute SQL in-memory)Note: Different databases use different function names (PostgreSQL:
func.random(), MySQL:func.rand(), SQL Server:func.newid()).