elif impl_rt:
return impl_rt
elif mod.startswith("sqlalchemy."):
+ if type(type_) is sqltypes.Variant:
+ return _render_Variant_type(type_, autogen_context)
if "_render_%s_type" % type_.__visit_name__ in globals():
fn = globals()["_render_%s_type" % type_.__visit_name__]
return fn(type_, autogen_context)
)
+def _render_Variant_type(type_, autogen_context):
+ base = _repr_type(type_.impl, autogen_context)
+ for dialect in sorted(type_.mapping):
+ typ = type_.mapping[dialect]
+ base += ".with_variant(%r, %s)" % (
+ dialect,
+ _repr_type(typ, autogen_context),
+ )
+ return base
+
+
def _render_type_w_subtype(
type_, autogen_context, attrname, regexp, prefix=None
):
--- /dev/null
+.. change::
+ :tags: usecase, autogenerate
+ :tickets: 131
+
+ Added rendering for SQLAlchemy ``Variant`` datatypes, which render as the
+ base type plus one or more ``.with_variant()`` method calls.
+
"sqlalchemy_util.types.MyType()",
)
+ def test_render_variant(self):
+ from sqlalchemy import VARCHAR, CHAR
+
+ self.autogen_context.opts["user_module_prefix"] = None
+
+ type_ = (
+ String(5)
+ .with_variant(VARCHAR(10), "mysql")
+ .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)."
+ "with_variant('mysql', sa.VARCHAR(length=10))."
+ "with_variant('oracle', sa.CHAR(length=15))",
+ )
+
def test_repr_user_type_user_prefix_None(self):
class MyType(UserDefinedType):
def get_col_spec(self):