Commits

Mike Bayer  committed 23fbf45

- [feature] Informative error message when op.XYZ
directives are invoked at module import time.

  • Participants
  • Parent commits 0a6b144

Comments (0)

Files changed (3)

 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
 

File alembic/util.py

                                 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],

File tests/test_op.py

 """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,\
         "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"
+    )