Abstract parent classes when using simple inheritance

58 views
Skip to first unread message

Benjamin Sims

unread,
Dec 13, 2012, 10:29:17 AM12/13/12
to sqlal...@googlegroups.com
Hi, I'd appreciate any thoughts on this; it seems like there are ways to do much more complex versions, so I guess I'm just missing a simple way to achieve it:

I have some polymorphic classes which inherit from a single parent class. The parent has the table mapped on it, and also defines a fair bit of functionality for itself.

The child classes don't have any different fields, but do need to define a lot of their own functionality. Because there are a lot of methods involved, I'm interested in making the parent class abstract so that child classes are forced to define certain functions.

I had hoped to use Abstract Base Classes (http://www.doughellmann.com/PyMOTW/abc/) to do this, but I can't just declare the parent class as an ABC as it inherits from Base.


To use the traditional example:

  
  class Employee(Base):
    __tablename__ = 'employee'
    __metaclass__ = abc.ABCMeta

    __mapper_args__ = {        
             'polymorphic_on':type,
   }

   @abc.abstractmethod
   def calculate_bonus(self):
       """Each child class *must* implement this"""

 


    class Manager(Employee):
         __mapper_args__ = {
        'polymorphic_identity':'manager'
        }


The above will fail due to metaclass conflict, as you would expect. 

I've looked at a lot of ways of doing this, but none of them seemed right.

Thanks for any help,
Benjamin




                                                                                                                                                         
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
~                                                                                                                                                                        
sqlamail.txt                                                                                                                                           1,1            All
"sqlamail.txt" 13L, 640C

Michael Bayer

unread,
Dec 13, 2012, 3:49:59 PM12/13/12
to sqlal...@googlegroups.com
mixing the metaclasses is difficult, and mixing of standard Python ABCs is not a use case we've tested - ideally we'd provide some simplified system to make it easy to integrate ABCs with declarative.

It would be much simpler if you stick with straight declarative for now, which supports the concept of "abstract bases" in a more rudimental way (via the __abstract__ flag), as well as mixins and custom base classes.

the docs have lots of examples on this:

http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/declarative.html#mixin-and-custom-base-classes

http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/declarative.html#abstract
Reply all
Reply to author
Forward
0 new messages