]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] TypeDecorator now includes a generic repr()
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Oct 2012 16:29:41 +0000 (12:29 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Oct 2012 16:29:41 +0000 (12:29 -0400)
    that works in terms of the "impl" type by default.
    This is a behavioral change for those TypeDecorator
    classes that specify a custom __init__ method; those
    types will need to re-define __repr__() if they need
    __repr__() to provide a faithful constructor representation.
    [ticket:2594]

CHANGES
lib/sqlalchemy/types.py
lib/sqlalchemy/util/langhelpers.py
test/sql/test_types.py

diff --git a/CHANGES b/CHANGES
index 97f5061c0f54e6f95008742e04da54859fd0eb20..084ab4eadd8786325e805c37fbb3706fa7240de1 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -716,6 +716,14 @@ underneath "0.7.xx".
     but not ForeignKeyConstraint or CheckConstraint.
     [ticket:2410]
 
+  - [bug] TypeDecorator now includes a generic repr()
+    that works in terms of the "impl" type by default.
+    This is a behavioral change for those TypeDecorator
+    classes that specify a custom __init__ method; those
+    types will need to re-define __repr__() if they need
+    __repr__() to provide a faithful constructor representation.
+    [ticket:2594]
+
   - [bug] column.label(None) now produces an
     anonymous label, instead of returning the
     column object itself, consistent with the behavior
index ebcffca6e264760c69da5637a99e43aad217b3db..e31cecfe22345a92b26460c70bfb8864ea88c94e 100644 (file)
@@ -817,6 +817,9 @@ class TypeDecorator(TypeEngine):
         """
         return self.impl.compare_values(x, y)
 
+    def __repr__(self):
+        return util.generic_repr(self, to_inspect=self.impl)
+
 
 class Variant(TypeDecorator):
     """A wrapping type that selects among a variety of
index 9761aeae9f3f66ede0d063902689b9770b289248..55b78090a5c175dcea3c0786db9726ff568215b6 100644 (file)
@@ -281,14 +281,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
 
index 1d096d7c416cc28b988b37bcc60e6e86be15005c..98bc516247ab075fa263a38073849ab98aac1e39 100644 (file)
@@ -336,6 +336,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):