From 0721a6bede7222386b2a2508aac5590909fbb148 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 1 Aug 2020 13:57:04 -0400 Subject: Genericize str() for types Remove lookup logic that attempts to locate a dialect for a type, just use StrSQLTypeCompiler. 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. Fixes: #4262 Change-Id: I02149e8a1ba1e7336149e962939b07ae0df83c6b --- test/sql/test_compiler.py | 2 +- test/sql/test_metadata.py | 16 ++++++---------- test/sql/test_types.py | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 12 deletions(-) (limited to 'test/sql') diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index d79d00555..1d31f1ea5 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -3940,7 +3940,7 @@ class StringifySpecialTest(fixtures.TestBase): 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): diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 3303eac1d..dc4e342fd 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -3970,13 +3970,9 @@ class ColumnOptionsTest(fixtures.TestBase): 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( @@ -3997,13 +3993,13 @@ class ColumnOptionsTest(fixtures.TestBase): 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"))) diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 30e5d1fca..fac9fd139 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -294,6 +294,33 @@ class AdaptTest(fixtures.TestBase): 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. @@ -2878,10 +2905,16 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): 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") -- cgit v1.2.1