From: Mike Bayer Date: Mon, 13 Feb 2012 22:14:03 +0000 (-0500) Subject: - [feature] Informative error message when op.XYZ X-Git-Tag: rel_0_2_2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c314ebe69d396fe4fdaa1fa0a3ceab931af29ef;p=thirdparty%2Fsqlalchemy%2Falembic.git - [feature] Informative error message when op.XYZ directives are invoked at module import time. --- diff --git a/CHANGES b/CHANGES index 7c27ecf7..e1cd2bf8 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,8 @@ 0.2.2 ===== +- [feature] Informative error message when op.XYZ + directives are invoked at module import time. + - [bug] setup.py won't install argparse if on Python 2.7/3.2 diff --git a/alembic/util.py b/alembic/util.py index 3599531d..eb2d11e7 100644 --- a/alembic/util.py +++ b/alembic/util.py @@ -90,10 +90,25 @@ def create_module_class_proxy(cls, globals_, locals_): defaulted_vals, formatvalue=lambda x: '=' + x) + def _name_error(name): + raise NameError( + "Can't invoke function '%s', as the proxy object has "\ + "not yet been " + "established for the Alembic '%s' class. " + "Try placing this code inside a callable." % ( + name, cls.__name__ + )) + globals_['_name_error'] = _name_error + func_text = textwrap.dedent("""\ def %(name)s(%(args)s): %(doc)r + try: + p = _proxy + except NameError: + _name_error('%(name)s') return _proxy.%(name)s(%(apply_kw)s) + e """ % { 'name':name, 'args':args[1:-1], diff --git a/tests/test_op.py b/tests/test_op.py index 58f19cb8..2d6a29b1 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -1,6 +1,6 @@ """Test against the builders in the op.* module.""" -from tests import op_fixture +from tests import op_fixture, assert_raises_message from alembic import op from sqlalchemy import Integer, Column, ForeignKey, \ UniqueConstraint, Table, MetaData, String,\ @@ -294,3 +294,15 @@ def test_inline_literal(): "UPDATE account SET name='account 2' WHERE account.name = 'account 1'", "UPDATE account SET id=2 WHERE account.id = 1" ) + +def test_cant_op(): + if hasattr(op, '_proxy'): + del op._proxy + assert_raises_message( + NameError, + "Can't invoke function 'inline_literal', as the " + "proxy object has not yet been established " + "for the Alembic 'Operations' class. " + "Try placing this code inside a callable.", + op.inline_literal, "asdf" + ) \ No newline at end of file