From 0e0c141f04359fd6cd8e4feea83c057645a7b938 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 24 Dec 2024 10:30:10 -0500 Subject: [PATCH] check for variants (recursion branch) first in all cases Fixed bug where autogen render of a "variant" type would fail to catch the variants if the leading type were a dialect-specific type, rather than a generic type. Fixes: #1585 Change-Id: I189e9ab3674b09700f2c774b600f6ff3abb52cd0 --- alembic/autogenerate/render.py | 7 ++++--- docs/build/unreleased/1585.rst | 8 ++++++++ tests/test_autogen_render.py | 14 +++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 docs/build/unreleased/1585.rst diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index 38bdbfca..6ebfbf9f 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -831,7 +831,10 @@ def _repr_type( mod = type(type_).__module__ imports = autogen_context.imports - if mod.startswith("sqlalchemy.dialects"): + + if not _skip_variants and sqla_compat._type_has_variants(type_): + return _render_Variant_type(type_, autogen_context) + elif mod.startswith("sqlalchemy.dialects"): match = re.match(r"sqlalchemy\.dialects\.(\w+)", mod) assert match is not None dname = match.group(1) @@ -843,8 +846,6 @@ def _repr_type( return "%s.%r" % (dname, type_) elif impl_rt: return impl_rt - elif not _skip_variants and sqla_compat._type_has_variants(type_): - return _render_Variant_type(type_, autogen_context) elif mod.startswith("sqlalchemy."): if "_render_%s_type" % type_.__visit_name__ in globals(): fn = globals()["_render_%s_type" % type_.__visit_name__] diff --git a/docs/build/unreleased/1585.rst b/docs/build/unreleased/1585.rst new file mode 100644 index 00000000..d6d6de0d --- /dev/null +++ b/docs/build/unreleased/1585.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, autogenerate + :tickets: 1585 + + Fixed bug where autogen render of a "variant" type would fail to catch the + variants if the leading type were a dialect-specific type, rather than a + generic type. + diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index 14a33194..6c78a8b4 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -26,6 +26,7 @@ from sqlalchemy import Unicode from sqlalchemy import UniqueConstraint from sqlalchemy import VARCHAR +from sqlalchemy.dialects.mysql import LONGTEXT from sqlalchemy.engine.default import DefaultDialect from sqlalchemy.sql import and_ from sqlalchemy.sql import column @@ -1842,7 +1843,6 @@ def test_render_variant(self): .with_variant(CHAR(15), "oracle") ) - # the new Black formatting will help a lot with this eq_ignore_whitespace( autogenerate.render._repr_type(type_, self.autogen_context), "sa.String(length=5)." @@ -1850,6 +1850,18 @@ def test_render_variant(self): "with_variant(sa.CHAR(length=15), 'oracle')", ) + def test_render_reverse_variant(self): + """test #1585""" + + self.autogen_context.opts["user_module_prefix"] = None + + type_ = LONGTEXT().with_variant(String(10), "oracle") + + eq_ignore_whitespace( + autogenerate.render._repr_type(type_, self.autogen_context), + "mysql.LONGTEXT()." "with_variant(sa.String(length=10), 'oracle')", + ) + def test_repr_user_type_user_prefix_None(self): class MyType(UserDefinedType): def get_col_spec(self):