--- /dev/null
+.. change::
+ :tags: bug, types
+ :tickets: 4262
+
+ Cleaned up the internal ``str()`` for datatypes so that all types produce a
+ string representation without any dialect present, including that it works
+ for third-party dialect types without that dialect being present. The
+ string representation defaults to being the UPPERCASE name of that type
+ with nothing else.
+
class StrSQLTypeCompiler(GenericTypeCompiler):
+ def process(self, type_, **kw):
+ try:
+ _compiler_dispatch = type_._compiler_dispatch
+ except AttributeError:
+ return self._visit_unknown(type_, **kw)
+ else:
+ return _compiler_dispatch(self, **kw)
+
def __getattr__(self, key):
if key.startswith("visit_"):
return self._visit_unknown
raise AttributeError(key)
def _visit_unknown(self, type_, **kw):
- return "%s" % type_.__class__.__name__
+ if type_.__class__.__name__ == type_.__class__.__name__.upper():
+ return type_.__class__.__name__
+ else:
+ return repr(type_)
+
+ def visit_null(self, type_, **kw):
+ return "NULL"
+
+ def visit_user_defined(self, type_, **kw):
+ try:
+ get_col_spec = type_.get_col_spec
+ except AttributeError:
+ return repr(type_)
+ else:
+ return get_col_spec(**kw)
class IdentifierPreparer(object):
@util.preload_module("sqlalchemy.engine.default")
def _default_dialect(self):
default = util.preloaded.engine_default
- if self.__class__.__module__.startswith("sqlalchemy.dialects"):
- tokens = self.__class__.__module__.split(".")[0:3]
- mod = ".".join(tokens)
- return getattr(__import__(mod).dialects, tokens[-1]).dialect()
- else:
- return default.DefaultDialect()
+ return default.StrCompileDialect()
def __str__(self):
if util.py2k:
)
for col, spec in zip(reflected_binary.c, columns):
eq_(
- str(col.type),
+ col.type.compile(dialect=mssql.dialect()),
spec[3],
- "column %s %s != %s" % (col.key, str(col.type), spec[3]),
+ "column %s %s != %s"
+ % (
+ col.key,
+ col.type.compile(dialect=mssql.dialect()),
+ spec[3],
+ ),
)
c1 = testing.db.dialect.type_descriptor(col.type).__class__
c2 = testing.db.dialect.type_descriptor(
eq_ignore_whitespace(
str(stmt),
- "SELECT CAST(mytable.myid AS MyType) AS myid FROM mytable",
+ "SELECT CAST(mytable.myid AS MyType()) AS myid FROM mytable",
)
def test_within_group(self):
assert Column(String, default=g2).default is g2
assert Column(String, onupdate=g2).onupdate is g2
- def _null_type_error(self, col):
- t = Table("t", MetaData(), col)
- assert_raises_message(
- exc.CompileError,
- r"\(in table 't', column 'foo'\): Can't generate DDL for NullType",
- schema.CreateTable(t).compile,
- )
+ def _null_type_no_error(self, col):
+ c_str = str(schema.CreateColumn(col).compile())
+ assert "NULL" in c_str
def _no_name_error(self, col):
assert_raises_message(
def test_argument_signatures(self):
self._no_name_error(Column())
- self._null_type_error(Column("foo"))
+ self._null_type_no_error(Column("foo"))
self._no_name_error(Column(default="foo"))
self._no_name_error(Column(Sequence("a")))
- self._null_type_error(Column("foo", default="foo"))
+ self._null_type_no_error(Column("foo", default="foo"))
- self._null_type_error(Column("foo", Sequence("a")))
+ self._null_type_no_error(Column("foo", Sequence("a")))
self._no_name_error(Column(ForeignKey("bar.id")))
t1 = typ()
repr(t1)
+ @testing.uses_deprecated()
+ @testing.combinations(*[(t,) for t in _all_types(omit_special_types=True)])
+ def test_str(self, typ):
+ if issubclass(typ, ARRAY):
+ t1 = typ(String)
+ else:
+ t1 = typ()
+ str(t1)
+
+ def test_str_third_party(self):
+ class TINYINT(types.TypeEngine):
+ __visit_name__ = "TINYINT"
+
+ eq_(str(TINYINT()), "TINYINT")
+
+ def test_str_third_party_uppercase_no_visit_name(self):
+ class TINYINT(types.TypeEngine):
+ pass
+
+ eq_(str(TINYINT()), "TINYINT")
+
+ def test_str_third_party_camelcase_no_visit_name(self):
+ class TinyInt(types.TypeEngine):
+ pass
+
+ eq_(str(TinyInt()), "TinyInt()")
+
def test_adapt_constructor_copy_override_kw(self):
"""test that adapt() can accept kw args that override
the state of the original object.
def test_default_compile_mysql_integer(self):
self.assert_compile(
dialects.mysql.INTEGER(display_width=5),
- "INTEGER(5)",
+ "INTEGER",
allow_dialect_select=True,
)
+ self.assert_compile(
+ dialects.mysql.INTEGER(display_width=5),
+ "INTEGER(5)",
+ dialect="mysql",
+ )
+
def test_numeric_plain(self):
self.assert_compile(types.NUMERIC(), "NUMERIC")