feat: add per-field default order by#196
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR adds ChangesDefault Order By Feature
🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #196 +/- ##
==========================================
+ Coverage 93.06% 93.26% +0.19%
==========================================
Files 69 69
Lines 6215 6338 +123
Branches 821 853 +32
==========================================
+ Hits 5784 5911 +127
+ Misses 288 285 -3
+ Partials 143 142 -1 ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/strawchemy/schema/field.py`:
- Around line 261-264: Replace the incorrect type-based check using
is_list(type_) with the instance-level list determination self.is_list when
validating self._default_order_by: in the method where the block raises
StrawchemyFieldError for `default_order_by`, change the condition to `if not
self.is_list:` (or equivalent) so fields flagged as list via `self.is_list`
(e.g., root_aggregations=True) are treated as list fields; keep the same error
message and raise StrawchemyFieldError when the instance is not a list.
- Around line 265-269: The code dereferences
decompose_order_by(expr).element.entity_namespace without checking existence;
update the loop that processes self._default_order_by to first retrieve element
= decompose_order_by(expr).element and guard access (e.g., use
hasattr/hasattr-like check or getattr with a sentinel) before comparing to
model, and if the attribute is missing raise StrawchemyFieldError with the same
message; ensure you reference
dto_model_from_type(strawberry_contained_user_type(type_))/decompose_order_by
and the StrawchemyFieldError to locate the fix.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 9b8c679a-c6bb-439d-a0ca-ed9e4ef731f8
⛔ Files ignored due to path filters (1)
tests/integration/__snapshots__/test_default_order_by.ambris excluded by!**/__snapshots__/**
📒 Files selected for processing (25)
README.mdsrc/strawchemy/dto/strawberry.pysrc/strawchemy/mapper.pysrc/strawchemy/repository/sqlalchemy/_base.pysrc/strawchemy/repository/strawberry/_async.pysrc/strawchemy/repository/strawberry/_sync.pysrc/strawchemy/schema/factories/types.pysrc/strawchemy/schema/field.pysrc/strawchemy/transpiler/_transpiler.pysrc/strawchemy/typing.pytests/integration/test_default_order_by.pytests/integration/types/mysql.pytests/integration/types/postgres.pytests/integration/types/sqlite.pytests/unit/mapping/test_schemas.pytests/unit/schemas/default_order_by_invalid.pytests/unit/schemas/default_order_by_non_list.pytests/unit/schemas/include/all_order_by.pytests/unit/schemas/order/field_order_by.pytests/unit/schemas/order/field_order_by_all.pytests/unit/schemas/order/field_order_by_specific_fields.pytests/unit/schemas/order/order_config_all_with_field_override.pytests/unit/schemas/order/order_config_with_field_override.pytests/unit/schemas/override/override_argument.pytests/unit/test_order_by_expr.py
| model = dto_model_from_type(strawberry_contained_user_type(type_)) | ||
| for expr in self._default_order_by: | ||
| if decompose_order_by(expr).element.entity_namespace is not model: | ||
| msg = f"`default_order_by` expression is not a column of {model.__name__}" | ||
| raise StrawchemyFieldError(msg) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Guard entity_namespace access to avoid uncaught runtime errors.
Line 267 dereferences .entity_namespace unconditionally. If the decomposed element doesn’t expose that attribute, this raises AttributeError instead of the expected StrawchemyFieldError.
Suggested fix
model = dto_model_from_type(strawberry_contained_user_type(type_))
for expr in self._default_order_by:
- if decompose_order_by(expr).element.entity_namespace is not model:
+ decomposed = decompose_order_by(expr)
+ entity_namespace = getattr(decomposed.element, "entity_namespace", None)
+ if entity_namespace is not model:
msg = f"`default_order_by` expression is not a column of {model.__name__}"
raise StrawchemyFieldError(msg)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| model = dto_model_from_type(strawberry_contained_user_type(type_)) | |
| for expr in self._default_order_by: | |
| if decompose_order_by(expr).element.entity_namespace is not model: | |
| msg = f"`default_order_by` expression is not a column of {model.__name__}" | |
| raise StrawchemyFieldError(msg) | |
| model = dto_model_from_type(strawberry_contained_user_type(type_)) | |
| for expr in self._default_order_by: | |
| decomposed = decompose_order_by(expr) | |
| entity_namespace = getattr(decomposed.element, "entity_namespace", None) | |
| if entity_namespace is not model: | |
| msg = f"`default_order_by` expression is not a column of {model.__name__}" | |
| raise StrawchemyFieldError(msg) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/strawchemy/schema/field.py` around lines 265 - 269, The code dereferences
decompose_order_by(expr).element.entity_namespace without checking existence;
update the loop that processes self._default_order_by to first retrieve element
= decompose_order_by(expr).element and guard access (e.g., use
hasattr/hasattr-like check or getattr with a sentinel) before comparing to
model, and if the attribute is missing raise StrawchemyFieldError with the same
message; ensure you reference
dto_model_from_type(strawberry_contained_user_type(type_))/decompose_order_by
and the StrawchemyFieldError to locate the fix.
Summary by CodeRabbit
New Features
Tests
Documentation