feat: Add optional sqlalchemy_use_enum for dy.Enum#355
Open
Jack Oberman (jackoberman) wants to merge 2 commits into
Open
feat: Add optional sqlalchemy_use_enum for dy.Enum#355Jack Oberman (jackoberman) wants to merge 2 commits into
Jack Oberman (jackoberman) wants to merge 2 commits into
Conversation
Allow dy.Enum to emit sqlalchemy.Enum for to_sqlalchemy_columns when sqlalchemy_use_enum=True, with optional sqlalchemy_enum_name and column- name defaults for PostgreSQL native enum types. Closes Quantco#354 Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds optional support for generating native SQLAlchemy ENUM types from dy.Enum columns (primarily for PostgreSQL / Alembic drift detection), plus accompanying docs and tests.
Changes:
- Extend
dy.Enumwithsqlalchemy_use_enumandsqlalchemy_enum_nameflags to emitsqlalchemy.Enuminstead ofCHAR/VARCHAR. - Add tests for SQLAlchemy column/type generation and for (de)serialization/matching behavior of the new flags.
- Document how to enable native SQL enums and how enum type names are chosen.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
dataframely/columns/enum.py |
Implements SQLAlchemy native enum mapping and naming rules. |
tests/columns/test_sqlalchemy_columns.py |
Verifies dialect-specific compilation/type naming and error behavior. |
tests/column_types/test_enum.py |
Validates matches() semantics + as_dict/from_dict for new flags. |
docs/guides/features/sql-generation.md |
Documents how to use the new native SQL enum feature. |
Comment on lines
+109
to
+119
| def sqlalchemy_column(self, name: str, dialect: sa.Dialect) -> sa.Column: | ||
| if self.sqlalchemy_use_enum: | ||
| return sa.Column( | ||
| name, | ||
| self._sqlalchemy_enum_type(dialect, column_name=name), | ||
| nullable=self.nullable, | ||
| primary_key=self.primary_key, | ||
| unique=self.unique, | ||
| autoincrement=False, | ||
| ) | ||
| return super().sqlalchemy_column(name, dialect) |
Comment on lines
+135
to
+139
| name = self.sqlalchemy_enum_name | ||
| if self._enum_class is not None: | ||
| if name is not None: | ||
| kwargs["name"] = name | ||
| return sa.Enum(self._enum_class, **kwargs) |
| By default, {class}`~dataframely.Enum` maps to fixed-length `CHAR` or `VARCHAR` columns so stored values remain plain strings. For PostgreSQL setups that use database-level `ENUM` types (for example with Alembic autogenerate), set `sqlalchemy_use_enum=True`: | ||
|
|
||
| ```python | ||
| from enum import StrEnum |
Comment on lines
+94
to
+96
| class Status(StrEnum): | ||
| PENDING = "pending" | ||
| APPROVED = "approved" |
Comment on lines
+178
to
+185
| class _Status(str, Enum): | ||
| PENDING = "pending" | ||
| APPROVED = "approved" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("column", "dialect", "datatype"), | ||
| [ |
| PGDialect_psycopg2(), | ||
| "my_status", | ||
| ), | ||
| (dy.Enum(_Status, sqlalchemy_use_enum=True), PGDialect_psycopg2(), "_status"), |
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.
Allow dy.Enum to emit sqlalchemy.Enum for to_sqlalchemy_columns when sqlalchemy_use_enum=True, with optional sqlalchemy_enum_name and column- name defaults for PostgreSQL native enum types.
Closes #354