[PATCH] More readable repr() for column types

Issue #2209 resolved
Former user created an issue

Here is a patch so that repr() on a column type only includes arguments that differ from the defaults. This helps the code generated by sqlautocode and sqlalchemy-migrate to be more terse and readable.

Quick test:

from sqlalchemy.types import String
repr(String()) == 'String()'

Comments (5)

  1. Mike Bayer repo owner
    • changed milestone to 0.7.2

    I like it, it needs to be genericized further, as it doesn't work for all types:

    diff -r c0ffa33b2fc676734004895eefe6bc058f105fd6 test/sql/test_types.py
    --- a/test/sql/test_types.py    Fri Jul 01 22:38:11 2011 -0400
    +++ b/test/sql/test_types.py    Mon Jul 04 13:23:27 2011 -0400
    @@ -129,6 +129,17 @@
                             getattr(t2, k) == t1.__dict__[k](k) or \
                             t1.__dict__[k](k) is None
    
    +    @testing.uses_deprecated()
    +    def test_repr(self):
    +        for typ in self._all_types():
    +            if typ in (types.TypeDecorator, types.TypeEngine):
    +                continue
    +            elif typ is dialects.postgresql.ARRAY:
    +                t1 = typ(String)
    +            else:
    +                t1 = typ()
    +            repr(t1)
    +
         def test_plain_init_deprecation_warning(self):
             for typ in (Integer, Date, SmallInteger):
                 assert_raises_message(
    

    produces:

      File "/Users/classic/dev/sqlalchemy/./lib/sqlalchemy/types.py", line 273, in genargs
        assert len(args) == len(defaults) + 1
    AssertionError
    

    and probably part of langhelpers. We have a lot of ad-hoc reprs in the schema package I'd like to flatten though i think for now ill probably keep this just in types.

  2. Log in to comment