From: Michael Trier Date: Sat, 4 Oct 2008 01:49:14 +0000 (+0000) Subject: Allowed column types to be callables. Fixes #1165. X-Git-Tag: rel_0_5rc2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56e88ed7c3b4bd6890087d6d2ced6514076d56c3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Allowed column types to be callables. Fixes #1165. --- diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index bb97943ebe..d859b90610 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -547,9 +547,13 @@ class Column(SchemaItem, expression._ColumnClause): "May not pass name positionally and as a keyword.") name = args.pop(0) if args: - if (isinstance(args[0], types.AbstractType) or - (isinstance(args[0], type) and - issubclass(args[0], types.AbstractType))): + coltype = args[0] + if callable(coltype): + coltype = args[0]() + + if (isinstance(coltype, types.AbstractType) or + (isinstance(coltype, type) and + issubclass(coltype, types.AbstractType))): if type_ is not None: raise exc.ArgumentError( "May not pass type_ positionally and as a keyword.") diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index a7243f2794..fe6cc7b534 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -246,9 +246,10 @@ class MutableType(object): def to_instance(typeobj): if typeobj is None: return NULLTYPE - elif isinstance(typeobj, type): + + try: return typeobj() - else: + except TypeError: return typeobj def adapt_type(typeobj, colspecs): diff --git a/test/sql/testtypes.py b/test/sql/testtypes.py index 2ed8f37d75..c8b9d7f394 100644 --- a/test/sql/testtypes.py +++ b/test/sql/testtypes.py @@ -797,5 +797,29 @@ class BooleanTest(TestBase, AssertsExecutionResults): print res2 assert(res2==[(2, False)]) +class CallableTest(TestBase, AssertsExecutionResults): + def setUpAll(self): + global meta + meta = MetaData(testing.db) + + def tearDownAll(self): + meta.drop_all() + + def test_callable_as_arg(self): + from functools import partial + ucode = partial(Unicode, assert_unicode=None) + + thing_table = Table('thing', meta, + Column('name', ucode, primary_key=True) + ) + + def test_callable_as_kwarg(self): + from functools import partial + ucode = partial(Unicode, assert_unicode=None) + + thang_table = Table('thang', meta, + Column('name', type_=ucode, primary_key=True) + ) + if __name__ == "__main__": testenv.main()