Skip to content

Commit 0e0c141

Browse files
committed
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
1 parent de3958b commit 0e0c141

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

alembic/autogenerate/render.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,10 @@ def _repr_type(
831831

832832
mod = type(type_).__module__
833833
imports = autogen_context.imports
834-
if mod.startswith("sqlalchemy.dialects"):
834+
835+
if not _skip_variants and sqla_compat._type_has_variants(type_):
836+
return _render_Variant_type(type_, autogen_context)
837+
elif mod.startswith("sqlalchemy.dialects"):
835838
match = re.match(r"sqlalchemy\.dialects\.(\w+)", mod)
836839
assert match is not None
837840
dname = match.group(1)
@@ -843,8 +846,6 @@ def _repr_type(
843846
return "%s.%r" % (dname, type_)
844847
elif impl_rt:
845848
return impl_rt
846-
elif not _skip_variants and sqla_compat._type_has_variants(type_):
847-
return _render_Variant_type(type_, autogen_context)
848849
elif mod.startswith("sqlalchemy."):
849850
if "_render_%s_type" % type_.__visit_name__ in globals():
850851
fn = globals()["_render_%s_type" % type_.__visit_name__]

docs/build/unreleased/1585.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: bug, autogenerate
3+
:tickets: 1585
4+
5+
Fixed bug where autogen render of a "variant" type would fail to catch the
6+
variants if the leading type were a dialect-specific type, rather than a
7+
generic type.
8+

tests/test_autogen_render.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from sqlalchemy import Unicode
2727
from sqlalchemy import UniqueConstraint
2828
from sqlalchemy import VARCHAR
29+
from sqlalchemy.dialects.mysql import LONGTEXT
2930
from sqlalchemy.engine.default import DefaultDialect
3031
from sqlalchemy.sql import and_
3132
from sqlalchemy.sql import column
@@ -1842,14 +1843,25 @@ def test_render_variant(self):
18421843
.with_variant(CHAR(15), "oracle")
18431844
)
18441845

1845-
# the new Black formatting will help a lot with this
18461846
eq_ignore_whitespace(
18471847
autogenerate.render._repr_type(type_, self.autogen_context),
18481848
"sa.String(length=5)."
18491849
"with_variant(sa.VARCHAR(length=10), 'mysql')."
18501850
"with_variant(sa.CHAR(length=15), 'oracle')",
18511851
)
18521852

1853+
def test_render_reverse_variant(self):
1854+
"""test #1585"""
1855+
1856+
self.autogen_context.opts["user_module_prefix"] = None
1857+
1858+
type_ = LONGTEXT().with_variant(String(10), "oracle")
1859+
1860+
eq_ignore_whitespace(
1861+
autogenerate.render._repr_type(type_, self.autogen_context),
1862+
"mysql.LONGTEXT()." "with_variant(sa.String(length=10), 'oracle')",
1863+
)
1864+
18531865
def test_repr_user_type_user_prefix_None(self):
18541866
class MyType(UserDefinedType):
18551867
def get_col_spec(self):

0 commit comments

Comments
 (0)