From: Mike Bayer Date: Sun, 26 Aug 2012 15:30:42 +0000 (-0400) Subject: - tweak the GenericFunction constructor more so that it's action in parsing the X-Git-Tag: rel_0_8_0b1~203 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b0e0bbb816b578910d0e08f034a732fc5fc898fd;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - tweak the GenericFunction constructor more so that it's action in parsing the arguments is easier to understand - add a test to ensure generic function can have a custom name --- diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index fe64764b77..24f7e81b41 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -25,11 +25,6 @@ class _GenericMeta(VisitableType): reg[name] = cls super(_GenericMeta, cls).__init__(clsname, bases, clsdict) - def __call__(cls, *args, **kwargs): - if cls.coerce_arguments: - args = [_literal_as_binds(c) for c in args] - return type.__call__(cls, *args, **kwargs) - class GenericFunction(Function): """Define a 'generic' function. @@ -88,11 +83,14 @@ class GenericFunction(Function): coerce_arguments = True def __init__(self, *args, **kwargs): + parsed_args = kwargs.pop('_parsed_args', None) + if parsed_args is None: + parsed_args = [_literal_as_binds(c) for c in args] self.packagenames = [] self._bind = kwargs.get('bind', None) self.clause_expr = ClauseList( operator=operators.comma_op, - group_contents=True, *args).self_group() + group_contents=True, *parsed_args).self_group() self.type = sqltypes.to_instance( kwargs.pop("type_", None) or getattr(self, 'type', None)) @@ -108,7 +106,6 @@ class next_value(GenericFunction): """ type = sqltypes.Integer() name = "next_value" - coerce_arguments = False def __init__(self, seq, **kw): assert isinstance(seq, schema.Sequence), \ @@ -128,7 +125,9 @@ class ReturnTypeFromArgs(GenericFunction): """Define a function whose return type is the same as its arguments.""" def __init__(self, *args, **kwargs): + args = [_literal_as_binds(c) for c in args] kwargs.setdefault('type_', _type_from_args(args)) + kwargs['_parsed_args'] = args GenericFunction.__init__(self, *args, **kwargs) class coalesce(ReturnTypeFromArgs): diff --git a/test/sql/test_functions.py b/test/sql/test_functions.py index b69f8f6ba5..fc227f3753 100644 --- a/test/sql/test_functions.py +++ b/test/sql/test_functions.py @@ -114,6 +114,19 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): assert isinstance(func.mypackage.myfunc(), f1) assert isinstance(func.myotherpackage.myfunc(), f2) + def test_custom_name(self): + class MyFunction(GenericFunction): + name = 'my_func' + + def __init__(self, *args): + args = args + (3,) + super(MyFunction, self).__init__(*args) + + self.assert_compile( + func.my_func(1, 2), + "my_func(:param_1, :param_2, :param_3)" + ) + def test_custom_args(self): class myfunc(GenericFunction): pass