Ability to inherit ReprWorkaround #118
Open
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.
Description
This pull request improves the get_replacement_type logic to allow developers to subclass ReprWorkaround. Currently, any subclass of postgresql.ENUM is forcibly replaced with the base ReprWorkaround class, which breaks compatibility with mixins like sqlalchemy_utils.ScalarCoercible that rely on methods such as _coerce.
Key changes:
Updated the get_replacement_type condition to exclude existing subclasses of ReprWorkaround
Modified ReprWorkaround.repr to handle inherited class names and avoid hardcoded module assumptions
Clarified that ReprWorkaround no longer needs to reside in sqlalchemy.dialects.postgresql when user_module_prefix is set to "" in Alembic config
This change enables more flexible enum customization without breaking autogeneration or runtime behavior.
Fixes: #119
Changes and Rationale
Change:
Why:
Previously, all postgresql.ENUM instances were replaced with the base ReprWorkaround, regardless of whether they were subclasses. This made it impossible to extend ReprWorkaround with additional functionality (e.g. _coerce from ScalarCoercible) without losing those custom methods. The added and not isinstance(..., ReprWorkaround) condition preserves subclass behavior and prevents unnecessary replacement.
Change:
Why:
The original repr implementation assumed ReprWorkaround was used directly and located within sqlalchemy.dialects.postgresql. This caused issues when subclassing, especially during autogeneration, where the replacement logic relied on strict string matching. The updated method dynamically replaces the current class name rather than hardcoding ReprWorkaround, ensuring that subclasses produce a compatible repr string. This preserves proper type comparison behavior in Alembic, even when using extended custom types.
Change:
Why:
To support user-defined column types in tests (e.g. subclasses of ReprWorkaround), it was necessary to configure Alembic with an empty user_module_prefix. This instructs Alembic to render those custom types directly, without requiring them to be fully importable via a Python module path. As a result, we no longer need to assign module = "sqlalchemy.dialects.postgresql" on the ReprWorkaround class.