feat(result): add schema_type parameter to SQLResult helper methods #106
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 optional
schema_typeparameter toSQLResultdata retrieval methods, enabling direct type-safe data transformation across the entire result API. Also refactorsto_schema()functionality into a standalone utility module to eliminate circular imports.Changes
Core Functionality
New
schema_typeparameter added to:SQLResult.get_data(schema_type=...)- returnslist[SchemaT]SQLResult.all(schema_type=...)- returnslist[SchemaT]SQLResult.one(schema_type=...)- returnsSchemaT(validated)SQLResult.one_or_none(schema_type=...)- returnsSchemaT | NoneSQLResult.get_first(schema_type=...)- returnsSchemaT | NoneRefactored schema transformation:
sqlspec/utils/schema.pywith standaloneto_schema()functionToSchemaMixinnow delegates to standalone function (backward compatible)API Examples
Documentation Updates (13 files)
result.datawithresult.all()throughout documentationresult.data[0]withresult.one()orresult.get_first()Architecture Benefits
No circular imports:
sqlspec/utils/schema.pycan be imported from anywhereNot mypyc-compiled:
utils/directory excluded from compilation for flexibilityType-safe: Full TypeVar support for dataclasses, TypedDict, Pydantic, msgspec, attrs
Backward compatible: All existing code continues to work
Test Coverage
get_data(schema_type=...)with dataclass and TypedDictall(schema_type=...)one(schema_type=...)one_or_none(schema_type=...)get_first(schema_type=...)Related
Builds on #105 which fixed TypedDict type hints for
schema_typeparameter in driver methods.