From b517e974e0d3eb4f89c724f9a1e865dc35824c7f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 2 Feb 2013 17:00:32 -0500 Subject: [PATCH] Backported adjustment to ``__repr__`` for :class:`.TypeDecorator` to 0.7, allows :class:`.PickleType` to produce a clean ``repr()`` to help with Alembic. [ticket:2594] [ticket:2584] --- doc/build/changelog/changelog_07.rst | 8 ++++++++ lib/sqlalchemy/types.py | 4 ++++ lib/sqlalchemy/util/langhelpers.py | 6 ++++-- test/sql/test_types.py | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/build/changelog/changelog_07.rst b/doc/build/changelog/changelog_07.rst index 363e4cbee..c747edb16 100644 --- a/doc/build/changelog/changelog_07.rst +++ b/doc/build/changelog/changelog_07.rst @@ -8,6 +8,14 @@ :version: 0.7.10 :released: + .. change:: + :tags: sql, bug + :tickets: 2594, 2584 + + Backported adjustment to ``__repr__`` for + :class:`.TypeDecorator` to 0.7, allows :class:`.PickleType` + to produce a clean ``repr()`` to help with Alembic. + .. change:: :tags: sql, bug :tickets: 2643 diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index f95cecfea..5fe2ba209 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -777,6 +777,9 @@ class TypeDecorator(TypeEngine): else: return op, typ + def __repr__(self): + return util.generic_repr(self, to_inspect=self.impl) + class Variant(TypeDecorator): """A wrapping type that selects among a variety of implementations based on dialect in use. @@ -936,6 +939,7 @@ def adapt_type(typeobj, colspecs): + class NullType(TypeEngine): """An unknown type. diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 16d564430..b7c5132df 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -239,14 +239,16 @@ def unbound_method_to_callable(func_or_cls): else: return func_or_cls -def generic_repr(obj, additional_kw=()): +def generic_repr(obj, additional_kw=(), to_inspect=None): """Produce a __repr__() based on direct association of the __init__() specification vs. same-named attributes present. """ + if to_inspect is None: + to_inspect = obj def genargs(): try: - (args, vargs, vkw, defaults) = inspect.getargspec(obj.__init__) + (args, vargs, vkw, defaults) = inspect.getargspec(to_inspect.__init__) except TypeError: return diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 91bf17175..2995dca79 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -335,6 +335,12 @@ class UserDefinedTest(fixtures.TablesTest, AssertsCompiledSQL): Float().dialect_impl(pg).__class__ ) + def test_type_decorator_repr(self): + class MyType(TypeDecorator): + impl = VARCHAR + + eq_(repr(MyType(45)), "MyType(length=45)") + def test_user_defined_typedec_impl_bind(self): class TypeOne(types.TypeEngine): def bind_processor(self, dialect):