]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
a handy @classproperty decorator
authorChris Withers <chris@simplistix.co.uk>
Tue, 23 Feb 2010 17:17:08 +0000 (17:17 +0000)
committerChris Withers <chris@simplistix.co.uk>
Tue, 23 Feb 2010 17:17:08 +0000 (17:17 +0000)
lib/sqlalchemy/util.py
test/base/test_utils.py

index 873243de166a49beadd6cb13797d8390644ad509..97270004b5295bf689ce7aef6de5949093988e98 100644 (file)
@@ -1639,3 +1639,13 @@ def _decorate_with_warning(func, wtype, message, docstring_header=None):
     decorated = warned(func)
     decorated.__doc__ = doc
     return decorated
+
+class classproperty(property):
+    """A decorator that behaves like @property except that operates
+    on classes rather than instances.
+
+    This is helpful when you need to compute __table_args__ and/or
+    __mapper_args__ when using declarative."""
+    def __get__(desc, self, cls):
+        return desc.fget(cls)
+
index e7e4116f7224fea447312e229fd5f25a9ecc6c8b..035e4f2682e148248f075b576ac01a8338478dc9 100644 (file)
@@ -980,3 +980,25 @@ class TestClassHierarchy(TestBase):
         eq_(set(util.class_hierarchy(A)), set((A, B, object)))
     # end Py2K
         
+
+class TestClassProperty(TestBase):
+
+    def test_simple(self):
+
+        from sqlalchemy.util import classproperty
+
+        class A(object):
+            something = {'foo':1}
+
+        class B(A):
+
+            @classproperty
+            def something(cls):
+                d = dict(super(B,cls).something)
+                d.update({'bazz':2})
+                return d
+
+        eq_(B.something,{
+                'foo':1,
+                'bazz':2,
+                })